在 Google App 脚本中将数据从一张表复制到另一张表并附加一行,一个小问题

问题描述 投票:0回答:1

当然,我已经记下来了,但就是做不到。设置了一个脚本来从一张纸中获取数据并将其放入另一张纸中,我已经这样做了,但在复制时留下了空白,我不知道如何解决它。我确信在代码中,我打问号的地方就是问题所在。

我尝试过放置

i
last
last+1
10
12
,但这些都不起作用,感觉好像我错过了一些小东西来做到这一点。下面是代码,如果需要,可以查看该工作表的链接(该工作表仅供我学习,如果您愿意,可以作为基本示例)。

提前致谢,如果代码可以写得更好,请告诉我,因为我仍在学习这个:)

function copyInfo() {
  var app = SpreadsheetApp;
  var copySheet = app.getActiveSpreadsheet().getSheetByName("Copy");
  for (var i = 2; i <12; i++) {   
  var getInfo = copySheet.getRange(2,2,i,2).getValues();
  //  get the info from range above - start at row 2 on column 2 (b), get number of rows i , number of columns = 2, b,c 
  var last = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Paste").getLastRow();
  var pasteSheet = app.getActiveSpreadsheet().getSheetByName("Paste");
//  Tell it where you want the info to go to 
  
  pasteSheet.getRange(last+1,1,?,2).setValues(getInfo);
  var clearIt = copySheet.getRange(2,2,i,2).clearContent();  
// this clears the copy range aka getInfo
  }}

链接到工作表

google-apps-script google-sheets
1个回答
9
投票

您可以使用

copyTo
一次复制整个范围,因此您的函数可以重写为:

function copyInfo() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var copySheet = ss.getSheetByName("Copy");
  var pasteSheet = ss.getSheetByName("Paste");

  // get source range
  var source = copySheet.getRange(2,2,12,2);
  // get destination range
  var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,2,12,2);

  // copy values to destination range
  source.copyTo(destination);

  // clear source values
  source.clearContent();
}
© www.soinside.com 2019 - 2024. All rights reserved.