在Google表格中追加到过滤范围后,appendRow不起作用

时间:2020-06-18 23:43:20

标签: google-apps-script google-sheets

我编写了一个脚本,该脚本应该将一行附加到Google表格中的过滤范围内。我的脚本在正常情况下有效。但是,如果删除了目标范围的最后一行,并且在其中一个字段上对范围进行了过滤(下图说明了这种情况),则appendRow不起作用。

appendRow起作用/不起作用的情况的屏幕截图:

[{spreadsheet samples] 1

这是我的代码:

function myFunction() {
  outputRowArray = [1,2];
  destinationUrl = "XXX";
  destinationSheet = "Sheet1"

  SpreadsheetApp.openByUrl(destinationUrl).getSheetByName(destinationSheet).appendRow(outputRowArray);  
}

1 个答案:

答案 0 :(得分:1)

您会丢失appendRow在工作表上添加一行的信息,这意味着该行是在工作表中的数据的最后一行之后添加的。

过去,我看到过类似的问题,声称appendRows不起作用,但实际上是该表包含一些值(有时是空字符串),该行在屏幕下方显示了几行。

使用Class Sheets之类的方法来帮助您了解正在发生的事情,记录某些工作表特征

  • getLastRow()
  • getMaxRows()

要记录事件,可以使用Logger.log(something)console.log(something)。有关更多详细信息,请查看https://developers.google.com/apps-script/guides/support/troubleshooting

要尝试的另一种方法是在追加新行后记录最后一行,但请记住,您可能需要首先使用SpreadsheetApp.flush()。示例:

function myFunction() {
  outputRowArray = [1,2];
  destinationUrl = "XXX";
  destinationSheet = "Sheet1"


  var sheet = SpreadsheetApp.openByUrl(destinationUrl).getSheetByName(destinationSheet);
  sheet.appendRow(outputRowArray);  
  SpreadsheetApp.flush();
  var lastRow = sheet.getLastRow();
  console.info(lastRow);
}