1

I have an array of email drafts that I got in apps script. I want to show them in a html file in a select element, but it shows up blank when I run it. Code below

code.gs

function doGet(e) {
  return HtmlService.createHtmlOutput("index");
}


function doSomething() {

  var drafts = GmailApp.getDrafts();
  var drafty = [];
  for(var i = 0; i < drafts.length; i++){
    drafty.push(drafts[i].getMessage().getSubject());
  }
  Logger.log(drafty);
  return drafty;

  var select = document.getElementById("select"),
      arr = drafty;

  for(var i = 0; i < arr.length; i++)
  {
       var option = document.createElement("OPTION"),
          txt = document.createTextNode(arr[i]);

        option.setAttribute("value", arr[i]);
        option.appendChild(txt);
        document.getElementById("select").appendChild(option);
  }

index.html

<!DOCTYPE html>
<html>
  <head>
    <script>
      google.script.run.doSomething();
    </script>
  </head>
  <body>
    <select id="select" class="addon-select addon-form-input"></select>
  </body>
5
  • 1
    You might wish to change onSelect to onChange. Commented Jan 5, 2020 at 22:10
  • You currently have the JavaScript for setting up the select tag on the server. It's not going to work there. Commented Jan 5, 2020 at 22:14
  • Thanks @Cooper - can you suggest how to change that? Commented Jan 5, 2020 at 22:16
  • Move it to the client. Commented Jan 5, 2020 at 22:17
  • @Cooper how does one do that? Do you mean add the gs to the html? If so, I tried that and when that's done, the GmailApp function doesn't work Commented Jan 5, 2020 at 22:17

1 Answer 1

2

Something like this: gs:

function doSomething() {
  var drafts=GmailApp.getDrafts();
  var drafty=[];
  for(var i=0;i<drafts.length;i++){
    drafty.push(drafts[i].getMessage().getSubject());
  }
  Logger.log(drafty);
  return drafty;
}

html:

<!DOCTYPE html>
<html>
  <head>
    <script>
      google.script.run
      .withSuccessHandler(function(A){
        var id='select';
        var select = document.getElementById(id);
        select.options.length = 0; 
        for(var i=0;i<A.length;i++) {
          select.options[i] = new Option(A[i],A[i]);
        }
      })
      .doSomething();
    </script>
  </head>
  <body>
    <select id="select"></select>
  </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

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.