0

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!

1
  • Use something like this: let inputTag = getElementsByTagName("input"); inputTag.setAttribute("value", email); You need to specify. Commented Dec 17, 2020 at 15:59

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your concise answer. I've been looking for this and have found a lot of convoluted answers. BTW, is there a way to pre-fill an <input type="date">? So far I've had no success (perhaps I have to set it to a particular locale...?? Thanks again!
@yago for the date, you will need to put it in format YYYY-MM-DD for it to work. Hope that helps.
Yes, thanks! I had to play around a bit with toISOString() method and was having some problems with weird day-light adjustment but at the end I've come up with a helper function to format dates like that and it works like a charm :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.