javascript - Google Apps 脚本:库中的 HTML 服务无法执行任何功能(潜在的浏览器安全问题?)
问题描述
我正在尝试开发一个生成 HTML 侧边栏并允许用户单击按钮来调用某些库函数的库。这将链接到许多电子表格,因此最终用户可以打开 HTML 侧边栏并执行库中定义的各种功能。我知道要从 HTML 服务调用库函数,您必须首先执行在同一个项目中定义的函数,然后调用外部库。但是,HTML 服务只能在我在父库环境中运行时调用函数。当我在另一个电子表格中打开 HTML 侧边栏并单击按钮时,它无法执行任何功能。
图书馆计划
在这里部署和引用为“testLibrary”
图书馆.gs
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('Page')
.setTitle('My custom sidebar');
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showSidebar(html);
}
Page.html(在库项目中)
<!DOCTYPE html>
<html>
Sample sidebar
<input type="button" value="Execute client function" onclick='someClientFunction()' />
<script>
function someClientFunction(){
google.script.run.clientFunction1();
}
</script>
</html>
带脚本的单独电子表格
电子表格脚本- 链接到库
// This function successfully opens the HTML sidebar
function showSidebar(){
testLibrary.showSidebar();
}
// This function works when called on its own. I want to call this from the library's HTML sidebar.
function clientFunction1(){
var ui = SpreadsheetApp.getUi();
ui.alert("Client function executed");
}
解决方案
推荐阅读
- php - PHP ML 可以有多个标签值吗?
- jenkins-pipeline - 詹金斯插件没有安装
- python - sklearn随机森林查找所选特征的分数
- css - React Native Align Self Flex End 未按预期工作
- powershell - 如何使用 powershell 将此文本文件输出为表格格式?
- reactjs - 您可以使用 this.props.history.push 将道具传递给与路径名不同的组件吗?
- c - 为什么使用指向结构的指针而不是直接使用结构(C)
- docker - Docker镜像rstudio/rstudio-server-pro挂载持久化配置目录时无法运行容器实例
- c++ - 如何进行矩阵*向量乘法并将此结果传递给 R 中 TMB 中的向量?
- java - 如何使用 Java API 列出 Kafka 集群中的所有可用代理?