【问题标题】:Google Apps Script Batch Executes with Unexpected TimingGoogle Apps 脚本批处理执行时间异常
【发布时间】:2021-03-29 22:14:24
【问题描述】:

下面的代码导致一个空白表,可能是由于 .clear() 最后执行,即使它列在最前面?

        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() 脚本会运行并很好地填充工作表。但我需要在从最终批处理请求中加载新数据之前清除工作表。

任何想法将不胜感激。 :-)

【问题讨论】:

    标签: google-apps-script google-sheets google-sheets-api


    【解决方案1】:

    在这种情况下,如何在您的脚本中使用SpreadsheetApp.flush(),如下所示?我以为clear()运行后,需要反映并请求Sheets API的batchUpdate方法。

    修改脚本:

    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());
    

    参考:

    【讨论】:

    • 完美!谢谢@Tanaike
    • @Kim Miller 欢迎。谢谢你让我知道。我很高兴你的问题得到了解决。如果您的问题已解决,请按接受按钮。与您有相同问题的其他人也可以将您的问题作为可以解决的问题。我认为您的问题和解决方案将对他们有用。如果找不到按钮,请随时告诉我。 stackoverflow.com/help/accepted-answer
    猜你喜欢
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多