简体   繁体   English

Google Apps Script - 包含外部文件的模板语句不起作用

[英]Google Apps Script - template statements to include external files not working

I'm trying to use external CSS stylesheet.我正在尝试使用外部 CSS 样式表。 However what I get is the ' * include stylesheet * ' rendering in my html sidebar.然而,我得到的是 ' * include stylesheet * ' 在我的 html 侧栏中呈现。 I created the include function in my code.gs file.我在 code.gs 文件中创建了包含函数。 Why is this not working?为什么这不起作用?

Index.html索引.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>

Code.gs代码.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样式表.html

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

At this function:在这个函数中:

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

You're using the createHtmlOutputFromFile and it will serve the content of your Index.html , but it won't execute the scriptlets inside.您正在使用createHtmlOutputFromFile ,它将提供您的Index.html的内容,但它不会执行内部的脚本

In order to execute the scriptlets you need to create an HtmlTemplate and then call evaluate() method, your function should look like this:为了执行脚本,您需要创建一个HtmlTemplate然后调用evaluate()方法,您的函数应如下所示:

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

Also remove the ' of the value gray in your stylesheet.html :还要删除stylesheet.html 中gray'

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM