【问题标题】:How to automatically close window created by Google Script Content Service/HTML Service?如何自动关闭由 Google Script Content Service/HTML Service 创建的窗口?
【发布时间】:2017-10-04 12:55:02
【问题描述】:

我在 Google 脚本中有一个 doGet(e) 函数,它写入电子表格,然后返回使用内容服务创建的简单“成功”消息。我的问题是,有没有办法自动关闭此消息创建的选项卡/窗口?用户在写入电子表格时将“批准”不同的项目,使用此功能连续批准多个项目可能会很烦人,然后必须关闭该函数创建的每个连续选项卡。

这是 GS:

function doGet(e) {  
  var id = e.parameter.id;
  var fundnumber = e.parameter.fundnumber;
  var date = Utilities.formatDate(new Date(), "America/New_York", "MM/dd/yyyy");

  var sh = SpreadsheetApp.openById("12jWGJWHCLoiLVoA0TlBQ0QgOMxBU9gVj9HQQveiNg0w").getSheetByName("Purchase Order Meta");
  var data = sh.getDataRange().getValues();

  for(n=0;n<data.length;++n){
    if( data[n][1].toString().match(id)==id ){ 
      data[n][8] = 'Approved by Linda on ' + date;
      data[n][16] = 'Approved by Linda on ' + date + '. Awaiting order from Trish.';
      data[n][13] = fundnumber
    }; 
    sh.getRange(1,1,data.length,data[0].length).setValues(data); // write back to the sheet
  }

  return ContentService.createTextOutput("success");
}

我的理解是,我必须使用内容服务或 HTML 服务返回一些东西,以便编写一个 doGet。我试过使用 HTML 服务并将 JS 添加到页面以关闭窗口,但它似乎不起作用。 :\

【问题讨论】:

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


    【解决方案1】:

    使用HTMLService 输出,您可以通过将google.script.host.close() 包装在超时函数中来关闭弹出窗口或HTML 页面,如下所示:

    setTimeout( function() { google.script.host.close(); }, 3000);

    将您的退货更改为:

    var output = "<body><p>Success!</p><script>setTimeout( function() { google.script.host.close(); }, 3000);</script></body>"
    
    return HtmlService.createHtmlOutput(output);
    

    【讨论】:

    • 感谢您的回复!我收到成功消息,但不幸的是该选项卡仍未关闭。 :\
    • 您可以在弹出窗口中打开 devtools 以在控制台中查找错误消息。有什么吗?
    • 对不起,呃。 :) 这是错误:userCodeAppPanel:1 Uncaught TypeError: google.script.host.close is not a function at userCodeAppPanel:1
    • 您有可以分享的示例文档吗?我没有遇到同样的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 2011-04-26
    • 1970-01-01
    相关资源
    最近更新 更多