Digital Farmer
Digital Farmer

Reputation: 2107

Add rows at the end of the spreadsheet

I set up this script in order to copy data from one spreadsheet to another and if there were missing lines to copy the data, he would add the ones that were missing, but when there are missing lines he adds them at the beginning of the spreadsheet instead of adding at the end, I would like to know what I need to adjust so that it doesn't happen anymore:

  var sss = SpreadsheetApp.openById('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
  var ss = sss.getSheetByName('Weight');
  var range = ss.getRange('U2:U');
  var data = range.getValues();
  var tss = SpreadsheetApp.openById('YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY');
  var ts = tss.getSheetByName('Registros');
  if(ts.getMaxRows() <= data.length) ts.insertRows(2, data.length);
  ts.getRange(2, 1, data.length, data[0].length).setValues(data);

Upvotes: 0

Views: 83

Answers (1)

Cooper
Cooper

Reputation: 64032

Try it this way. It should get rid of those pesky null rows.

function whoknows() {
   var sss = SpreadsheetApp.openById('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
  var ss = sss.getSheetByName('Weight');
  var range = ss.getRange(2,21,ss.getLastRow()-1,1);
  var data = range.getValues();
  var tss = SpreadsheetApp.openById('YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY');
  var ts = tss.getSheetByName('Registros');
  ts.getRange(2, 1, data.length, data[0].length).setValues(data);
}

Upvotes: 2

Related Questions