且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从自定义Google小工具调用Google Apps脚本小工具?

更新时间:2022-10-29 17:17:57

你不能像这样混合它们,它们是不同和断开连接(例如,xml小工具如何知道哪些应用程序脚本在您的尝试中调用?)。
最简单的方法是仅将apps脚本用作doGet中contentService的json等数据提供者。从xml小工具中,您可以使用ajax进入apps脚本发布的url&任何需要的参数。
为了避免认证麻烦,发布脚本以匿名访问的方式运行。


Using a Google Site with an embedded Google Apps Script, I'm displaying some data from a database using JDBC. The Google Apps Script uses doGet to load an HTML page:

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

...the index.html page in turn calls a function in my Apps Script to get some database data:

google.script.run.withSuccessHandler(mySuccessHandler).getSomeDBData();

When the mySuccesshandler is called, I render the data with the JQuery.

This works. However, embedded Google Apps Scripts have a statically defined height. A post on SO suggested developing a custom Google Gadget which dynamically resizes when content changes.

But I can't find any examples, or confirmation that I can port my current Google Apps Script to my own Gadget. The documentation on working with remote content doesn't mention databases.

I tried placing the call to my App Script in the Gadget XML file:

 google.script.run.withSuccessHandler(mySuccessHandler).getSomeDBData();

However, this failed with cannot call run of undefined. So is there any way I can call a GAS from a custom Google Widget?

You cant mix them like that, they are different and disconnected (for example how would the xml gadget know which apps script to call in your attempt?). The easiest would be to use the apps script solely as a data provider like json from a contentService in doGet. From the xml gadget you do an ajax get to the apps script published url & any needed parameters. To avoid auth hassles publish script to run as you with anonymous access.