首页 > 解决方案 > 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");
}

当我单击按钮时检查器显示的图像

标签: javascripthtmlgoogle-apps-scriptgoogle-sheetsgoogle-cloud-platform

解决方案


推荐阅读