简体   繁体   中英

Prefill an HTML input with Google Apps Script variable

I am making a form on a sidebar in Google Apps Script, and I would like to autofill the "mail" input with the user's email.

I know that this variable get's the user email

var email = Session.getEffectiveUser (). getEmail ();

But I don't know how to pass the value obtained from that variable (the user's email) to the input on the form (inputmail2).

HTML form code:

<form action="">
    <div>
      <label for="inputmail2">Mail</label>
      <input type="email" id="inputmail2">
    </div>
    <div>
      <label for="inputproblem">Your Problem</label>
      <textarea id="" ></textarea>
    </div>
    <div>
      <button type="submit">Enviar</button>
    </div>
  </form>

From already thank you very much!

You can use Templated HTML for this. With scriptlets you can pass the variable to the HTML as documented here .

To do this you would need to put this into you .gs code:

var ui = SpreadsheetApp.getUi();
var email = Session.getEffectiveUser().getEmail();
var html = HtmlService.createTemplateFromFile('filename');
html.email = email;
var output = html.evaluate();
ui.showSidebar(output);

Then for your form you need to insert the scriptlet <?= email?> as the value:

<form action="">
    <div>
      <label for="inputmail2">Mail</label>
      <input type="email" id="inputmail2" value="<?= email ?>">
    </div>
    <div>
      <label for="inputproblem">Your Problem</label>
      <textarea id="" ></textarea>
    </div>
    <div>
      <button type="submit">Enviar</button>
    </div>
  </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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