Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 在工作表界面中向编辑器显示授权弹出窗口,而不是在google脚本代码编辑器中手动运行任何功能_Google Apps Script - Fatal编程技术网

Google apps script 在工作表界面中向编辑器显示授权弹出窗口,而不是在google脚本代码编辑器中手动运行任何功能

Google apps script 在工作表界面中向编辑器显示授权弹出窗口,而不是在google脚本代码编辑器中手动运行任何功能,google-apps-script,Google Apps Script,我正在编写谷歌电子表格的脚本,情况是当一个编辑触发器被触发时,会生成一个html对话框模板 ScriptApp.newTrigger('onEditSimulation') .forSpreadsheet(SpreadsheetApp.getActive()) .onEdit() .create(); html模板上有一个按钮,onclick上的绑定是: onclick = "google.script.run .withSuc

我正在编写谷歌电子表格的脚本,情况是当一个编辑触发器被触发时,会生成一个html对话框模板

  ScriptApp.newTrigger('onEditSimulation')
    .forSpreadsheet(SpreadsheetApp.getActive())
    .onEdit()
    .create();
html模板上有一个按钮,onclick上的绑定是:

onclick = "google.script.run
                   .withSuccessHandler(sended)
                   .withUserObject(this)
                   .resendSpreadSheet(<?=newMail?>)
onclick=“google.script.run
.withSuccessHandler(已发送)
.withUserObject(此)
.resendSpreadSheet()
服务器端功能绑定on按钮在代码编辑器中运行,授权过程完成后,将正常工作。

该电子表格将与其他用户共享,但上述方式对我的目标用户来说确实不友好且不流畅,我强烈希望避免这种情况。有没有解决方案使授权弹出窗口仅显示在工作表界面上?

您可以实现一个小脚本,询问“未经授权的用户”“手动运行将触发授权过程的菜单选项

这个函数当然只运行一次

代码示例如下:

function onOpen() {
  if(! PropertiesService.getUserProperties().getProperty("authorized")){
    SpreadsheetApp.getUi().createMenu('Authorize').addItem('Authorize',authorize).addToUi();
    var ui = HtmlService.createTemplateFromFile('index')
    .evaluate()
    .setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .setTitle('Authorization request');
    SpreadsheetApp.getUi().showModelessDialog(ui,'Authorization request');
  }
}

function authorize(){
  PropertiesService.getUserProperties().setProperty("authorized", "done");
}
index.html:

<html>
  <head>
    <base target="_top">
  </head>
  <body>
Please select the "Authorize " option from the menu<br><br>
<input type="button" value="then close this window"
  onclick="google.script.host.close()" />
 </body>
</html>

请从菜单中选择“授权”选项


如果您调用一个工作表函数
onEdit
,但它们尚未进行身份验证,默认情况下,它们将被推送到身份验证窗口。@Brian不幸的是,在我的情况下,installabe触发器是必需的,在我的应用程序中,有一些API调用,如potection类中的
addEditor
。@insas,感谢您的回答,看来这是我实现目标的唯一好方法。您能否进一步解释一下
setSandboxMode()
的用途?我已经回顾了好几次,但缺乏经验来理解真正的影响会是什么。如果您能更新此部分以回答^^^“再次感谢!沙盒模式确实不再必要,请原谅我使用它,但这是一种旧习惯……不久前HTML服务还有两种其他模式:原生模式和模拟模式,它们使用了一种“代码转换器”“名为CAJA,旨在防止谷歌应用程序脚本代码中的恶意软件,但现在这一切都已成为历史,你可以忽略这一点!顺便说一句,谢谢你接受。