【问题标题】:Appending Multiple Rows while updating from one spreadsheet to another从一个电子表格更新到另一个电子表格时附加多行
【发布时间】:2019-07-22 20:30:05
【问题描述】:
function clearData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var source_sheet = ss.getSheetByName("sheet");
  var source_range = source_sheet.getRange("2:21");
  var values = source_range.getValues();


  var target = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1kNzG4IQrI9TSlGzpIjp_84uWzoraVDSLPyAS-RIJPh4/edit#gid=1047694951");
  var target_sheet = target.getSheetByName("Deposits");
  var target_range = target_sheet.getRange(2,1,values.length,values[19].length);// use the array size to define the range because the target column A is not necessarily the same size at this time.
  target_range.setValues(values);
}

我已经为我的 google 表格创建了这个函数,但是它更新了相同的 20 行。我需要它做的是更新行,然后我有一个清除一个电子表格的 clear 函数,然后在前 20 行下添加另外 20 行,依此类推,一次有 20 行的追加行函数。

但是,我尝试的任何代码都不起作用。

我目前拥有的代码已附上。

有人可以帮忙吗?

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    您的问题是您的代码总是更新相同的 20 行。原因在这一行中找到: target_sheet.getRange(2,1,values.length,values[19].length)

    您的目标范围始终从第 2 行开始,因此它将覆盖第 2 行及以下已存在的任何内容。相反,您希望将数据复制到“LastRow”之后的行(无论是什么)。

    试试这个:

    var targetLR = target.getLastRow();
    var target_range = target_sheet.getRange(targetLR+1,1,values.length,values[19].length);
    

    这里有两个变化:

    1. getLastRow() 返回有内容的最后一行的位置。

    2. 所以我们只需将“1”添加到该值(“targetLR+1”)以在下一行定义 target_range 的开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      • 2013-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多