如何从Google App脚本到Web App创建消息框

发布时间:2020-07-07 09:35

我正在尝试创建一个从.gs到Web应用程序的消息框,我尝试了这些,但是消息框无法显示:

var html = HtmlService.createHtmlOutputFromFile('View');
SpreadsheetApp.getUi().showModalDialog(html, 'Dialog title');

Browser.msgBox('Hello!', Browser.Buttons.OK);

HtmlService.createHtmlOutput("Hello!");

这些方法是否正确?您的回复将不胜感激:)

回答1

您尝试使用的方法是为use in the context of a Google Spreadsheet设计的

  • 即使您的WebApp已绑定到电子表格,您也不能在WebApp中使用它们。
  • 无论如何,这毫无意义,因为部署WebApp时,您是与WebApp界面进行交互,而不是与电子表格UI进行交互。
  • 相反,您需要使用可从WebApp客户端访问的Javascript方法,请参见here

示例:

Code.js

function doGet() {
  return HtmlService.createHtmlOutputFromFile("index");
}


function called(){
  
  Logger.log("I was called");
  //the rest of your code
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <script>
    if (confirm("Please confirm, are you sure you want to continue?")) {
     alert( "google.script.run will be called");
     google.script.run.called();
   } else {
    alert( "You cancelled!");
   } 
    </script>
  </body>
</html>