简体   繁体   English

将变量从谷歌脚本传递到HTML对话框

[英]Passing variable from google script to html dialog

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 1)运行if语句,如果值为true,则使用字符串参数运行错误提示

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

2) Run the below function with the string argument. 2)使用字符串参数运行以下函数。 I then want the function to pass the argument to the html iframe. 然后我希望函数将参数传递给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. 3)然后该变量应传递给html文件,该文件显示为模态对话框。 Provided the string is equal to "Title", the dialogue should read: "The Title is missing. Please enter Title and try again." 如果字符串等于“标题”,则对话框应显示为:“ 标题丢失。请输入标题并重试。”

missingvar.html 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. 上面的HTML显示,但missVar没有显示。 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之外。

HTML Service: Templated HTML HTML服务:模板化HTML

Use HtmlService templating if you want to do what you are describing. 如果要执行所描述的操作,请使用HtmlService模板。 It removes the JS on the client side and serves the fully formed dialog, with missVar intact. 它删除了客户端的JS,并提供完整形成的对话框, missVar完好无损。

var html = HtmlService.createTemplateFromFile(missingVar);

    html.missingVar = missVar;

var htmlOutput = html.evaluate();

Your missingvar.html file would then contain 然后,您的missingvar.html文件将包含

<?= missingVar ?>

Wherever you want that variable displayed. 您想要显示该变量的任何地方。

I've altered the way the prompt is created. 我改变了创建提示的方式。 the html file is no longer needed as the js creates it on the fly. js文件不再需要,因为js动态创建它。

From

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.");
}

To

function errorPrompt(missvar) {
  var missVar = "test";
   var htmlOutput = HtmlService
     .createHtmlOutput('<p>' + missVar + ' is missing, please enter a ' +  missVar + ' and try again.</p>')
     .setSandboxMode(HtmlService.SandboxMode.IFRAME)
     .setWidth(250)
     .setHeight(300);
 SpreadsheetApp.getUi().showModalDialog(htmlOutput, missVar + ' is missing. Please provide a ' + missVar + ' and try again.');
}

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

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