如何在 Google Apps Script 中的 HTMLOutput 中使用 scriptlet [英] How to use scriptlets in HTMLOutput in Google Apps Script

查看:22
本文介绍了如何在 Google Apps Script 中的 HTMLOutput 中使用 scriptlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载一个模态对话框:

 var html = HtmlService.createHtmlOutputFromFile('File').setSandboxMode(HtmlService.SandboxMode.IFRAME).setWidth(1000).setHeight(700);电子表格App.getUi().showModalDialog(html, '我的页面');

现在,在 File.HTML 中,我想加载另一个带有 CSS 设置的 HTML 文件,我该怎么做?

我已经尝试使用 scriptlet 将它包含在 HtmlTemplate 中,但它不起作用:

我已经在 code.gs 中定义了包含函数:

函数包含(文件){返回 HtmlService.createTemplateFromFile(file).evaluate().getContent();}

解决方案

问题是你正在使用:

createHtmlOutputFromFile

代替:

createTemplateFromFile

您需要创建一个模板:

这是你看到的:

scriptlet 没有运行,而是被解释为文本.

这就是你想看到的:

代码应该是这样的:

代码.gs

//将此代码用于 Google 文档、表单或新表格.函数 onOpen() {SpreadsheetApp.getUi()//或 DocumentApp 或 FormApp..createMenu('对话框').addItem('打开', 'openDialog').addToUi();}函数 openDialog() {var html = HtmlService.createTemplateFromFile('index').evaluate();//这个是必须的SpreadsheetApp.getUi()//或 DocumentApp 或 FormApp..showModalDialog(html, '对话框标题');}功能包括(文件){返回 HtmlService.createHtmlOutputFromFile(File).getContent();};

index.html

你好世界!<输入类型=按钮"值=关闭"onclick="google.script.host.close()";/>

文件.html

这是一个测验.有效!

基本上,你需要改变:

var html = HtmlService.createHtmlOutputFromFile('index')

到:

var html = HtmlService.createTemplateFromFile('index')

从文件创建模板.

我也把代码改成这样:

function openDialog() {var html = HtmlService.createTemplateFromFile('index').评价().setSandboxMode(HtmlService.SandboxMode.IFRAME);

原答案:

include 不像 keyword 或内置函数.您需要在名为 include.gs 脚本文件中创建一个函数.

 函数包含(文件名){返回 HtmlService.createHtmlOutputFromFile(filename).getContent();};

此外,您不能混合使用 HTML 服务和 UI 服务.我不知道这是否是你想要做的,但我想我会提到它.

您想要完成的内容在此处的文档中进行了描述:

文档 - 最佳做法

I'm loading a modal dialog with:

 var html = HtmlService.createHtmlOutputFromFile('File')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .setWidth(1000)
    .setHeight(700);

 SpreadsheetApp.getUi() 
    .showModalDialog(html, 'My Page');

Now, in File.HTML, I want to load another HTML file with CSS settings, how do I do that?

I've tried including it as in HtmlTemplate using scriptlets but it doesn't work:

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

EDIT:

I have defined the include function in code.gs:

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

解决方案

The problem is that you are using:

createHtmlOutputFromFile

instead of:

createTemplateFromFile

You need to create a template:

This is what you are seeing:

The scriptlet is not running, but being interpreted as text.

This is what you want to see:

Here is how the code should be:

Code.gs

// Use this code for Google Docs, Forms, or new Sheets.
function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .createMenu('Dialog')
      .addItem('Open', 'openDialog')
      .addToUi();
}

function openDialog() {
  var html = HtmlService.createTemplateFromFile('index')
    .evaluate();//This is necessary

    SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
    .showModalDialog(html, 'Dialog title');
}

function include(File) {
  return HtmlService.createHtmlOutputFromFile(File).getContent();
};

index.html

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

Hello, world!
<input type="button" value="Close"
  onclick="google.script.host.close()" />

File.html

<div>
    This is a test. it worked!
</div>

Basically, you need to change:

var html = HtmlService.createHtmlOutputFromFile('index')

to:

var html = HtmlService.createTemplateFromFile('index')

Create a TEMPLATE from file.

And I also changed the code to this:

function openDialog() {
  var html = HtmlService.createTemplateFromFile('index')
    .evaluate()
    .setSandboxMode(HtmlService.SandboxMode.IFRAME);

Original answer:

include is not something like a keyword or a built in function. You need to create a function in a .gs script file named include.

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

Also, you can't mix the HTML Service and the UI Service. I don't know if that's what you are trying to do, but I thought I'd mention it.

What you want to accomplish is describe in the documentation here:

Documentation - Best Practices

这篇关于如何在 Google Apps Script 中的 HTMLOutput 中使用 scriptlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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