create HTML template from File

39 views
Skip to first unread message

sben...@bbchs.org

unread,
Oct 11, 2021, 6:21:49 PM10/11/21
to Google Apps Script Community
I am trying to create a table in html so that I can send it as an email.  I am passing an array over to a function to create the HTML file.  It is only returning HTMLOutput.  So that is all that I am getting for the body of the email.  I have logged out the array and it is there.  I took all the code for creating the template right from the documentation.
Any help would be appreciated.  

 var ss = SpreadsheetApp.getActive();
  var ui = SpreadsheetApp.getUi();
  var response = ui.alert('Are You Sure?', 'Make sure you are on the sheet you wish to email.', ui.ButtonSet.YES_NO)
  if (response === ui.Button.YES) {
    var teacherSheet = ss.getSheetByName('Teachers');
    var teachers = teacherSheet.getRange(1, 1, teacherSheet.getLastRow(), teacherSheet.getLastColumn()).getValues();
    var currentSheet = ss.getActiveSheet()
    var currentSheetData = currentSheet.getRange(5, 1, currentSheet.getLastRow(), currentSheet.getLastColumn()).getDisplayValues();
    for (var teach = 0; teach < teachers.length; teach++) {
      var teacherArray = [['Date', 'Internal Sub', 'Periods Covered', '# of Periods']];
      var teacherEmail = teachers[teach][1];

      for (var i = 0; i < currentSheetData.length; i++) {
        if (teachers[teach][0] == currentSheetData[i][5]) {
          
          teacherArray.push([currentSheetData[i][1], currentSheetData[i][5], currentSheetData[i][6], currentSheetData[i][7]])
        }
      }
  
      if (teacherArray.length > 1) {   //Everything up to this point is working correctly
        var html = createHTML(teacherArray)
        Utilities.sleep(200);
        GmailApp.createDraft(teacherEmail,'Test',null,{htmlBody:html})
      }
    }
  }
  else if(response===ui.Button.NO){
    ui.alert('Nothing was done.  Run program again to send emails.');
  }
}


function createHTML(array) {
  var t = HtmlService.createTemplateFromFile('index');
  t.data = array
  // Logger.log(t.data)
  // Logger.log(t.getCode())
  return t.evaluate();

}
HTML FILE

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <table>
      <? for (var i = 0; i < data.length; i++) { ?>
        <tr>
          <? for (var j = 0; j < data[i].length; j++) { ?>
            <td><?= data[i][j] ?></td>
          <? } ?>
        </tr>
      <? } ?>
    </table>
  </body>
</html>

sben...@bbchs.org

unread,
Oct 11, 2021, 6:57:36 PM10/11/21
to Google Apps Script Community
I figured it out.  I think since it was not a doGet and not loading a webpage, I had to add getContent() after the evaluate().  

Michael O'Shaughnessy

unread,
Oct 11, 2021, 7:20:17 PM10/11/21
to google-apps-sc...@googlegroups.com
HI, just a quick thing off the top of my head...  The htmlBody for the options objects needs to be a string.  When you do "t.evaluate()" what you are creating is an HTMLOutput.

So, try changing your return statement from createHTML() to this:

return t.evaluate().getContent().toString()

Hopefully this will work for you.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/40fb2c5d-fb21-415d-8893-485af36ad650n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages