且构网

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

Google Apps脚本 - 包含外部文件无效的模板语句

更新时间:2023-12-04 21:27:40

此功能

 函数openSidebar(){
var htmlOutput = HtmlService.createHtmlOutputFromFile('Index');
htmlOutput.setTitle('客户查询表');
SpreadsheetApp.getUi()。showSidebar(htmlOutput);
}

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



为了执行脚本,您需要创建一个 HtmlTemplate ,然后调用

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

同时移除' stylesheet.html 中:
$ b

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


I'm trying to use external CSS stylesheet. However what I get is the '* include stylesheet *' rendering in my html sidebar. I created the include function in my code.gs file. Why is this not working?

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>

Code.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>

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.

In order to execute the scriptlets you need to create an HtmlTemplate and then call evaluate() method, your function should look like this:

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:

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