【问题标题】:Google Apps Script - template statements to include external files not workingGoogle Apps 脚本 - 包含外部文件的模板语句不起作用
【发布时间】:2016-09-13 18:52:33
【问题描述】:

我正在尝试使用外部 CSS 样式表。然而,我得到的是 '* include stylesheet *' 在我的 html 侧边栏中呈现。我在我的 code.gs 文件中创建了包含函数。为什么这不起作用?

Index.html

<!DOCTYPE html>
<html>
 <head>
 <base target="_top">
 <?!= include('stylesheet'); ?>
 </head>
 <body>
  <script
   src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
  </script>
 </body>
</html>

代码.gs

function onOpen() {
 SpreadsheetApp.getUi()
 .createMenu('Form')
 .addItem('Open', 'openSidebar')
 .addToUi();
}
function include(filename) {
 return HtmlService.createHtmlOutputFromFile(filename)
  .getContent();
}
function openSidebar(){
 var htmlOutput = HtmlService.createHtmlOutputFromFile('Index');
 htmlOutput.setTitle('Customer Inquiry Form');
 SpreadsheetApp.getUi().showSidebar(htmlOutput);
}

stylesheet.html

<style>
 body{
  background-color:'gray';
 }
</style>

【问题讨论】:

    标签: javascript html css google-apps-script


    【解决方案1】:

    在这个函数中:

    function openSidebar(){
     var htmlOutput = HtmlService.createHtmlOutputFromFile('Index');
     htmlOutput.setTitle('Customer Inquiry Form');
     SpreadsheetApp.getUi().showSidebar(htmlOutput);
    }
    

    您正在使用createHtmlOutputFromFile,它将提供您的Index.html 的内容,但不会执行内部的scriptlets

    为了执行脚本,您需要创建一个HtmlTemplate,然后调用evaluate() 方法,您的函数应该如下所示:

    function openSidebar(){
      var template = HtmlService.createTemplateFromFile('Index')
          .evaluate()
          .setTitle('Customer Inquiry Form')
     SpreadsheetApp.getUi().showSidebar(template);
    }
    

    同时删除 stylesheet.html 中值 gray'

    <style>
    html{
      background-color: gray;
     }
    </style>
    

    【讨论】:

    • 还没有时间进行修复,但我阅读了模板文件下的参考资料,我知道你说的是真的。起初我不明白,还以为你误解了我,因为你重组了我的索引文件而不是样式表。但是你说得对,我需要使用模板才能使用 Scriptlet。再次感谢,2 换 2 :)。
    • 好的。我正式实施了它并且它有效。谢谢。
    猜你喜欢
    • 2022-11-16
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多