Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 475
flamingslaptrap Avatar asked May 05 '16 20:05

flamingslaptrap


People also ask

Can we pass parameters in HTML?

An (HTML) web resource page can only accept a single custom parameter called data . To pass more than one value in the data parameter, you need to encode the parameters and decode the parameters in your page.

How do I embed a Google script in HTML?

You can simply insert the Web App to pages using the Embed option. To embed Google Apps Script Web Apps in Google Sites, Copy the published Web App URL (For testing, you can directly copy the above live form link) Go to Google Site, choose the Embed option, paste the URL you copied, and then click Insert.


1 Answers

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.

like image 180
JSDBroughton Avatar answered Oct 05 '22 13:10

JSDBroughton