将多行附加到电子表格 - Google Apps 脚本
Appending multiple rows to Spreadsheet - Google Apps Script
我希望通过 GAS 将多行附加到 google sheet,同时考虑到性能和意外的可能性。
为了在单行中实现这一点,我会使用 appendRow
,因为这可以解决干预突变的问题,并在一个函数中完成所有操作。
简单示例:
var sheet= SpreadsheetApp.openById(ssId).getSheetByName(sheetName);
sheet.appendRow(["foo", "bar", "foobar"]);
当然,要将其扩展到多行,我可以简单地为每一行循环此函数,尽管 GAS 最佳实践建议不要这样做。
尝试使用 appendRow
通过二维数组添加多行未成功,导致 API 使用对辅助数组的引用作为进入行的值。
因此我问,有没有一种方法可以将多行附加到 spreadsheet 中,它仍然可以解决 appendRow
所做的干预可变性并尽可能避免循环?
可以使用Range.setValues() method which will set the values at once, and a script lock (or another lock, depends on your use case) to prevent other instances of the script from doing appends at the same time. You just need to get the good range (with array length) and the good position (with sheet.getLastRow()+1方法)。这是一个例子:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var example = [[1,2,3],[4,5,6],[7,8,9]];
LockService.getScriptLock().waitLock(60000);
sheet
.getRange(
sheet.getLastRow() + 1,
1,
example.length,
example[0].length
)
.setValues(example);
警告:这不能防止人类或其他脚本。
您应该查看 Sheet Class, there are a number of bulk row insert methods listed, one of which may fit your criteria. If that proves insufficient you may want to look into the Advanced Sheet Service 的官方文档。
编辑
针对您的评论,我认为没有一种方法可以将行和数据作为纯原子操作批量添加。但是有一种方法可以通过 LockService API 解决您对干预突变的担忧。此服务允许您防止并发访问代码块,因此对于您的用例,您可以利用批量插入方法来创建新行并填充它们,而不必担心突变。
我会确保还为这段代码创建一个行和列变量。有时,如果您尝试在 getrange 函数中直接添加 object.length,GAS 会抛出错误。即
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var example = [[1,2,3],[4,5,6],[7,8,9]];
var row = example.length;
var column = example[0].length;
sheet.getRange(sheet.getLastRow()+1, 1, row, column).setValues(example);
我希望通过 GAS 将多行附加到 google sheet,同时考虑到性能和意外的可能性。
为了在单行中实现这一点,我会使用 appendRow
,因为这可以解决干预突变的问题,并在一个函数中完成所有操作。
简单示例:
var sheet= SpreadsheetApp.openById(ssId).getSheetByName(sheetName);
sheet.appendRow(["foo", "bar", "foobar"]);
当然,要将其扩展到多行,我可以简单地为每一行循环此函数,尽管 GAS 最佳实践建议不要这样做。
尝试使用 appendRow
通过二维数组添加多行未成功,导致 API 使用对辅助数组的引用作为进入行的值。
因此我问,有没有一种方法可以将多行附加到 spreadsheet 中,它仍然可以解决 appendRow
所做的干预可变性并尽可能避免循环?
可以使用Range.setValues() method which will set the values at once, and a script lock (or another lock, depends on your use case) to prevent other instances of the script from doing appends at the same time. You just need to get the good range (with array length) and the good position (with sheet.getLastRow()+1方法)。这是一个例子:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var example = [[1,2,3],[4,5,6],[7,8,9]];
LockService.getScriptLock().waitLock(60000);
sheet
.getRange(
sheet.getLastRow() + 1,
1,
example.length,
example[0].length
)
.setValues(example);
警告:这不能防止人类或其他脚本。
您应该查看 Sheet Class, there are a number of bulk row insert methods listed, one of which may fit your criteria. If that proves insufficient you may want to look into the Advanced Sheet Service 的官方文档。
编辑
针对您的评论,我认为没有一种方法可以将行和数据作为纯原子操作批量添加。但是有一种方法可以通过 LockService API 解决您对干预突变的担忧。此服务允许您防止并发访问代码块,因此对于您的用例,您可以利用批量插入方法来创建新行并填充它们,而不必担心突变。
我会确保还为这段代码创建一个行和列变量。有时,如果您尝试在 getrange 函数中直接添加 object.length,GAS 会抛出错误。即
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var example = [[1,2,3],[4,5,6],[7,8,9]];
var row = example.length;
var column = example[0].length;
sheet.getRange(sheet.getLastRow()+1, 1, row, column).setValues(example);