Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

google apps script - Copy column values from one spreadsheet to another spreadsheet, adding only the necessary rows

Sheet 1 / Sheet 2
enter image description here enter image description here

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-12in 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
enter image description here

If I use getlastrow(), several more unnecessary lines are added:

enter image description here

If I use the getMaxRows() without calculating +1-NumberOfRows, it always copies more lines than necessary too:

enter image description here

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.

question from:https://stackoverflow.com/questions/66049629/copy-column-values-from-one-spreadsheet-to-another-spreadsheet-adding-only-the

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Explanation:

The 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.

Let's say you want to start pasting from row 1 in the target sheet and you want to paste 10 rows of data but the target sheet has only 6 rows. The setValues function will create more rows so your data can fit in exactly.

Minimal Reproducible Example:

Sheet1

Let's say you have 10 rows of data in Sheet1:

enter image description here

Sheet2

Sheet2 has only 6 rows available:

enter image description here

if you run this code:

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const sh1 = ss.getSheetByName('Sheet1');
  const sh2 = ss.getSheetByName('Sheet2');
  const data = sh1.getRange('A1:A10').getValues();
  sh2.getRange(1,1,data.length,data[0].length).setValues(data);
}

setValues will do the job and add the absolute necessary rows needed to fit the data in.

Sheet2 (after the script is executed)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...