google-apps-script - 来自 HTML 的 google.script.run.function() 调用不起作用
问题描述
我正在从绑定到我的谷歌表的对话框 HTML 文件中调用一个函数。我从谷歌开发中复制了代码并想调用一个简单的函数。但是,它什么也没做。我尝试了各种方法,但我就是无法让它发挥作用。脚本和 HTML 都绑定到我的电子表格。
这是我的脚本:
// Use this code for Google Docs, Slides, Forms, or Sheets.
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.createMenu('Dialog')
.addItem('Open', 'openDialog')
.addToUi();
}
function openDialog() {
var html = HtmlService.createHtmlOutputFromFile('Index');
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, 'Dialog title');
}
function doSomething() {
Logger.log('I was called!');
}
这是简单的 HTML 文件
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
google.script.run.doSomething();
</script>
</head>
</html>
解决方案
试试这个:GS:
function openDialog() {
var html = HtmlService.createHtmlOutputFromFile('ah3');
SpreadsheetApp.getUi().showModalDialog(html, 'Dialog title');
}
function doSomething() {
SpreadsheetApp.getActive().toast("I was called.")
}
HTML:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
window.onload=function(){
google.script.run
.withSuccessHandler(function(){google.script.host.close();})
.doSomething();
}
console.log("I am here");
</script>
</head>
<body>
</body>
</html>
推荐阅读
- bash - 基于逗号分隔表的字符串高效重命名
- bash - 制作 bash 脚本以拉入所有 git 子目录。感觉我的 bash 脚本优化得不是很好
- reactjs - 如何处理多“页面”应用程序中的应用程序状态?
- javascript - 我想保持跟随手风琴的过渡效果
- javascript - 从 JS 对象创建的数组在登录控制台时显示圆形对象
- java - NTLM 和 Java Apache httpclient 4.5.6 的多线程问题
- c - 函数引用参数:指针与原型只是样式?
- sql - SQL Server - 如何使部分重复的行从原始行继承值
- .net - 提取 Sustainsys SAML 2.0 断言
- c++ - 基类类型向量中的派生结构