将变量从google脚本传递给html对话框 [英] Passing variable from google script to html dialog

查看:116
本文介绍了将变量从google脚本传递给html对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力尝试在有人没有输入值的情况下做出一些错误提示。在这种情况下,我会检查这个值是否为空,并最终检查它是否为日期。



所以当这里是运行时。



1)运行if语句,如果该值为true,则使用字符串参数运行错误提示符

  if(sheetData [4] ===){
errorPrompt(Title);





2)使用字符串参数运行下面的函数。

 函数打开对话框
函数errorPrompt(missvar ){
var missVar =test;
var html = HtmlService.createHtmlOutputFromFile('missingvar')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi()//或DocumentApp或FormApp。
.showModalDialog(html,missVar +'is Missing please please'+ missVar +,然后再试一次。);
}

3)变量应该传递给显示为模态对话。如果字符串等于标题,则对话框应显示为:标题已丢失,请输入标题并重试。



missingvar.html

 <!DOCTYPE html> 
< html>
< head>
< base target =_ top>
< / head>
< body>
< script>
UI.getElementById(missVar)。innerHTML;
< / script>

< var id = missVar>< / var>不见了。请输入< var id = missVar>< / var>然后再试一次。
< input type =buttonvalue =Close
onclick =google.script.host.close()/>

< / body>
< / html>

上面的HTML显示,但是missVar不显示。任何人有任何想法?只是要注意,对话框的标题栏正确显示了变量,但这不在html之内。

解决方案

< a href =https://developers.google.com/apps-script/guides/html/templates =nofollow> HTML服务:模板化HTML



如果您想要执行您所描述的操作,请使用 HtmlService 模板。它删除客户端的JS,并提供完整的对话框, missVar 完好无损。

  var html = HtmlService.createTemplateFromFile(missingVar); 

html.missingVar = missVar;

var htmlOutput = html.evaluate();

然后,您的 missingvar.html 文件将包含

 <?= missingVar?> 

无论您想要显示哪个变量,

I'm working on trying to do some error prompts in the case someone doesn't put in a value. In the case I check if the value is empty, and will eventually check if it's a date.

So when here is the run down.

1) Run the if statement, if the value is true, run the error prompt with the string argument

if (sheetData[4] === ""){
  errorPrompt("Title");
}

2) Run the below function with the string argument. I then want the function to pass the argument to the html iframe.

function to open dialog
function errorPrompt(missvar) {
  var missVar = "test";
  var html = HtmlService.createHtmlOutputFromFile('missingvar')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .showModalDialog(html, missVar + ' is Missing please provide ' + missVar + " and try again.");
}

3) the variable should then pass along to the html file which is displayed as a modal dialogue. Provided the string is equal to "Title", the dialogue should read: "The Title is missing. Please enter Title and try again."

missingvar.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>            
        <script>
        UI.getElementById("missVar").innerHTML;
        </script>

       The <var id=missVar></var> is missing. Please enter <var id=missVar></var> and try again.
    <input type="button" value="Close"
        onclick="google.script.host.close()" />

  </body>
</html>

The HTML above displays, however the missVar doesn't show. Anyone have any thoughts? just to note, the title bar of the dialog box is correctly displaying the variable, but that's outside of the html.

解决方案

HTML Service: Templated HTML

Use HtmlService templating if you want to do what you are describing. It removes the JS on the client side and serves the fully formed dialog, with missVar intact.

var html = HtmlService.createTemplateFromFile(missingVar);

    html.missingVar = missVar;

var htmlOutput = html.evaluate();

Your missingvar.html file would then contain

<?= missingVar ?>

Wherever you want that variable displayed.

这篇关于将变量从google脚本传递给html对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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