Home > Software design >  Copy entire columns including blank rows
Copy entire columns including blank rows

Time:05-31

  //Copy
  var ss = SpreadsheetApp.openById('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
  var sss = ss.getSheetByName('Stats');
  var range = sss.getRange(1,7,ss.getLastRow(),5);
  var data = range.getValues();

  //Main_Event 1
  var ts = SpreadsheetApp.openById('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB');
  var tss = ts.getSheetByName('Main_Event');
  tss.getRange(1,1, data.length, data[0].length).setValues(data);

Current results:

Sheet A Sheet A Sheet A Sheet A Sheet A
one two three four five
new values new values new values new values new values
new values new values new values new values new values
Sheet B Sheet B Sheet B Sheet B Sheet B
one two three four five
new values new values new values new values new values
new values new values new values new values new values
old values old values old values old values old values
old values old values old values old values old values

Expected result:

Sheet B Sheet B Sheet B Sheet B Sheet B
one two three four five
new values new values new values new values new values
new values new values new values new values new values

Sheet A: columns are filled up to row 85 and up to row 1000 they are blank.

Sheet B: there are rows filled up to row 1000.

So when I'm going to copy the values from A to B, I want the blank rows to take the place of the filled rows, being exactly the same as sheet A.

But in my attempts the old values keep appearing from row 86 to 1000 and only the 85 rows filled in A that are modified in B.

What am I doing wrong in my attempt?

Additional information:

When I try to do this same thing using the same code but sending the values within the same ID sheet, it works perfectly modifying the column completely including the blank rows.

But when I try to do it using different ID sheets, it doesn't work.

CodePudding user response:

I'm not sure why this isn't working, but maybe as a workaround you could use clearContents() before pasting? https://developers.google.com/apps-script/reference/spreadsheet/sheet#clearcontents. You can also clear the specific range.

CodePudding user response:

After more research I found the error!

When using ss.getLastRow() it only retrieves the values up to the last row that contains values.

To collect data up to the last row of columns it is necessary to use ss.getMaxRows().

Old code:

var range = sss.getRange(1,7,ss.getLastRow(),5);

New code:

var range = sss.getRange(1,7,ss.getMaxRows(),5);
  • Related