简体   繁体   English

将列值从一个电子表格复制到另一个电子表格,仅添加必要的行

[英]Copy column values from one spreadsheet to another spreadsheet, adding only the necessary rows

Sheet 1 / Sheet 2 Sheet 1 / Sheet 2
在此处输入图像描述 在此处输入图像描述

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.在这种情况下,我想从第12行收集值,如果需要在另一个电子表格中添加行,请使用ss.getMaxRows()+1-12 ,它只会根据需要添加尽可能多的行。

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:如果我使用getlastrow() ,则会添加更多不必要的行:

在此处输入图像描述

If I use the getMaxRows() without calculating +1-NumberOfRows , it always copies more lines than necessary too:如果我在不计算+1-NumberOfRows的情况下使用getMaxRows() ,它也总是复制不必要的行数:

在此处输入图像描述

Is there a fixed model that is not always necessary to place the calc +1-NumberOfRows ?是否有一个固定的 model 并不总是需要放置 calc +1-NumberOfRows I am afraid to forget to change the value for the calculation and to add erroneous amounts of lines.我害怕忘记更改计算值并添加错误的行数。

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. setValues function 将添加绝对必要的行数,以便数据可以放入。它不会删除错误,也不会添加超出需要的行数。

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.假设您要从目标工作表中的第 1 行开始粘贴,并且要粘贴 10 行数据,但目标工作表只有 6 行。 The setValues function will create more rows so your data can fit in exactly. setValues function 将创建更多行,以便您的数据可以完全适合。

Minimal Reproducible Example:最小可重现示例:

Sheet1表 1

Let's say you have 10 rows of data in Sheet1 :假设您在Sheet1有 10 行数据:

在此处输入图像描述

Sheet2表 2

Sheet2 has only 6 rows available: Sheet2只有 6 行可用:

在此处输入图像描述

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. setValues将完成这项工作并添加适合数据所需的绝对必要的行。

Sheet2 (after the script is executed) Sheet2(脚本执行后)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM