Google Apps脚本批处理以意外的时间执行 [英] Google Apps Script Batch Executes with Unexpected Timing
问题描述
下面的代码会导致一张空白纸,可能是由于.clear()最后执行了,即使它被列在最前面?
The following code results in a blank sheet, assumably due to .clear() executing last, even though it's listed first?
const ss = SpreadsheetApp.getActiveSpreadsheet();
var contractSheet = ss.getSheetByName(sheetName);
contractSheet.clear();
const body = {requests: [{pasteData: {html: true, data: table[0], coordinate: {sheetId: ss.getSheetByName(sheetName).getSheetId()}}}]};
Sheets.Spreadsheets.batchUpdate(body, ss.getId());
如果我删除.clear(),脚本将运行并很好地填充工作表.但是在从最终的批处理请求中加载新数据之前,我需要清除工作表.
If I remove the .clear() the script runs and populates the sheet fine. But I need to clear the sheet before loading the new data from the final batch request.
任何想法都将不胜感激.:-)
Any ideas would be much appreciated. :-)
推荐答案
在这种情况下,如何在脚本中如下使用 SpreadsheetApp.flush()
?我以为在运行 clear()
之后,需要反映它并请求Sheets API的batchUpdate方法.
In this case, how about using SpreadsheetApp.flush()
in your script as follows? I thought that after clear()
was run, it is required to reflect it and request the batchUpdate method of Sheets API.
const ss = SpreadsheetApp.getActiveSpreadsheet();
var contractSheet = ss.getSheetByName(sheetName);
contractSheet.clear();
SpreadsheetApp.flush(); // Added
const body = {requests: [{pasteData: {html: true, data: table[0], coordinate: {sheetId: ss.getSheetByName(sheetName).getSheetId()}}}]};
Sheets.Spreadsheets.batchUpdate(body, ss.getId());
参考:
- flush()
这篇关于Google Apps脚本批处理以意外的时间执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!