如何在我的Google应用程序脚本/ Google表格html中包含脚本? [英] How do I include scripts in my Google App Script/ Google sheets html?

查看:234
本文介绍了如何在我的Google应用程序脚本/ Google表格html中包含脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的HtmlService的doGet函数:

 函数doGet(){
return HtmlService.createHtmlOutputFromFile('myhtmlfilename');
}

这很好。



我似乎得到了以下内容,无论我的html文件是否有任何脚本标记:

错误



我曾尝试将所有的javaScript存储在带有标记的单独html文件中(称为JavaScript.html),然后使用强制打印scriptlet将它们包括回我的html。



包含我的脚本标记的html文件

 < script> 
function transferItems(){
google.script.run.test();
}

< / script>

我的函数将script html包含为scriptlet,我也尝试将其作为getRawContent评估':

$ p $ 函数包括(文件名){
返回HtmlService.createTemplateFromFile(文件名).evaluate() .getContent();

我的scriptlet:

 <?!= include('JavaScript'); ?> 

我的code.gs中的函数名为test():

  function test(){
alert(This is a test。);
}

我正在尝试调用测试的主页面中的按钮()函数:

 < button onClick =transferItems()>传输< / button> 

无论我尝试什么,我的页面都会在浏览器中显示这些scriptlet作为文本。



显示结果



我错过了什么?



我已阅读的所有答案和教程均为2015年或更早版本。
我觉得我跟随谷歌的文档。
任何帮助表示赞赏。我承认在这方面很新颖,可能错过了一些非常简单的事情,但它让我发疯。

如果你有当您使用需要的HTMLService进行评估时,HTML文件中的<?...?> scriptlet使用 createTemplateFromFile()否则它们将被视为纯文本。 createOutputFromFile()不适用于评估脚本标记。

  function doGet(){
return HtmlService.createTemplateFromFile('myhtmlfilename')。evaluate();
}

编辑:您不需要评估那些< ;? ?> scriptlets。所以它只需要像这样:

 函数include(文件名){
返回HtmlService.createHtmlOuputFromFile(文件名) .getContent();
}

使用我的手机来回答问题可能并不完美。

I cannot get my App Script-based html to include any scripts.

My doGet function for the HtmlService:

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

This works fine.

I seem to get the following whether I have any script tags on my html file or not:

errors

I have tried storing all of my javaScript in a separate html file with tags (called JavaScript.html )and then including them back into my html using force print scriptlets.

My html file containing my script tags:

<script>
function transferItems(){
google.script.run.test();
}

</script>

My function to include the script html as a scriptlet, which I have also tried as 'getRawContent' instead of 'evaluate':

function include(filename) {
return HtmlService.createTemplateFromFile(filename).evaluate().getContent();
}

My scriptlet:

<?!= include('JavaScript'); ?>

My function in my code.gs, called test():

function test() {
alert("This is a test."); 
}

My button in my main html page through which I am trying to call the test() function:

<button onClick="transferItems()">Transfer</button>

No matter what I try my page ends up displaying those scriptlets as text in the browser.

displayed result

What am I missing?

All of the answers and tutorials I have read are from 2015ish or earlier. I feel like I am following google's documentation. Any help is appreciated. I'm admittedly very new at this and am probably missing something very simple, but it's making me crazy.

解决方案

If you have the <?...?> scriptlets in your HTML file when you evaluate with the HTMLService you need to use createTemplateFromFile() otherwise they will be treated as plain text. The createOutputFromFile() won't work for evaluating the script tags.

function doGet() {
 return HtmlService.createTemplateFromFile('myhtmlfilename').evaluate();
}

edit: you include doesn't need to evaluate those <? ?> scriptlets. So it just needs the be like this:

function include(filename) { 
return HtmlService.createHtmlOuputFromFile(filename).getContent();
}

Using my phone to answer so the code might not be perfect.

这篇关于如何在我的Google应用程序脚本/ Google表格html中包含脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆