I use this formula to copy values from one column to another spreadsheet column:
function SheetToSheet() {
var sss = SpreadsheetApp.openById('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
var ss = sss.getSheetByName('Sheet 1');
var range = ss.getRange(12,1,ss.getMaxRows()+1-12,1);
var data = range.getValues();
var tss = SpreadsheetApp.openById('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB');
var ts = tss.getSheetByName('Sheet 2');
ts.getRange(12,1, data.length, data[0].length).setValues(data);
}
In this case where I want to collect the values from the line 12
, use ss.getMaxRows()+1-12
in case it is necessary to add rows in the other spreadsheet, it will add literally only as many rows as necessary.
Result:Add Row 11, 12, 13, 14 and 15
Add Values in Row 12, 13, 14 and 15
If I use getlastrow()
, several more unnecessary lines are added:
If I use the getMaxRows()
without calculating +1-NumberOfRows
, it always copies more lines than necessary too:
Is there a fixed model that is not always necessary to place the calc +1-NumberOfRows
? I am afraid to forget to change the value for the calculation and to add erroneous amounts of lines.
setValues
function will add the absolute necessary number of rows so the data can fit in. It won't drop an error nor add more rows than needed. In other words, the data in the target sheet will expand automatically to 17. You don't need to do anything. – soMarioss.getMaxRows()+1-12
, but this option I always need to calculate+1-NumberRow
, it was this calculation that I didn't want to need to do every time, but I'm not finding an option, onlyss.getLastRow()
andss.getMaxRows()
add more lines than necessary. – Brondby IFvar range = ss.getRange(12,1,ss.getMaxRows()+1-12,1);
is there any option that is not necessary to putss.getMaxRows()+1-12
to define how many total lines will be copied? – Brondby IFdata.length
is the total number of line to be copied. Please read Sheet.getRange(1,1,1,12) what does the numbers in bracket specify? – soMario