简体   繁体   中英

How do I populate an HTML table using a table from a spreadsheet?

So I have a simple 4 column table with rows that will be added by a function. I'd like to create and html body that will include a table and be emailed out 3 times per week using google script. I am able to get the data out of the spreadsheet and into a variable. The problem is that I can't make it into a darn table, I know this is probably a simple solution but I am quite new to google script. This is my script;

 function emailsLeanKitchen() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var Sheet = ss.getSheetByName("Sheet1"); var dataTable = Sheet.getRange(1,1,Sheet.getLastRow(),4).getValues(); Logger.log(dataTable); GmailApp.sendEmail("my email","subject", dataTable);// }

And if Anyone Knows of a good way to set this script on a timer, that would be cool too because that's my next goal. I've attached a link to my spreadsheet. I appreciate any help!
https://docs.google.com/spreadsheets/d/1X_UcqyXXRMyjZ2j46TymMrIMWvt19HOZTTaEUlIVqwE/edit?usp=sharing

  1. You want to create a HTML table from the values retrieved from the active Spreadsheet, and want to send it as the HTML body using Google Apps Script.
  2. You want to run the script by the time-driven trigger.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Answer for Q1:

When your script is modified, it becomes as follows.

Modified script:

From:
 GmailApp.sendEmail("my email","subject", dataTable);//
To:
 var htmlTable = dataTable.reduce(function(s, e) { return s += "<tr><th>" + e.join("</th><th>") + "</th></tr>" }, "<table border=\\"1\\">") + "</table>"; GmailApp.sendEmail("my email","subject", "sample text body", {htmlBody: htmlTable});
  • In this case, htmlTable is sent as the HTML body. "sample text body" is sent as the text body. In this case, when the mailer cannot read the HTML body, the text body is shown.

Answer for Q2:

You can run the function emailsLeanKitchen() with the time-driven event trigger. You can see how to set the trigger at here . In this case, from 3 times per week of your question, "Week timer" might be suitable for "Select type of time based trigger". About this, please set this for your actual situation.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

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