Advertisement
jabela

SheetsEmailTable

Oct 11th, 2019
2,559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function onOpen() {
  2.   var ui = SpreadsheetApp.getUi();
  3.   ui.createMenu('Send Email')
  4.       .addItem('Send Points', 'sendEmailToStudent')
  5.       .addToUi();
  6. }
  7.  
  8. function sendEmailToStudent()
  9. {
  10.  
  11.   //capture the sheet with this identifier
  12.   var sheet = SpreadsheetApp.getActive().getSheetByName("ChCh");
  13.  
  14.   //get the email address of the student
  15.   var email = sheet.getRange(1, 2).getValue(); //loads the current email address into variable "email". Notice columns are referred to as numbers.
  16.  
  17.   //get student data for this kid
  18.   var dataGrid = sheet.getRange(2, 1, sheet.getLastRow(), 3).getValues(); //loads the data in this range at the time
  19.   var message = ""; //container to hold final email content
  20.  
  21.   //set up the HTML to be rendered by sendEmail eventually.
  22.   //adding a table for the column headings and content inside it.
  23.  
  24.   message = "  <table>";
  25.   message += "<tr>"
  26.   message += "<td><b>Challenge</td>"
  27.   message += "<td><b>Points</td>"
  28.   message += "<td><b>Challenge Name</td>"
  29.   message += "</b> </tr>"
  30.  
  31.   //Loop through the content in data grid and set them up in each column with new row for each.
  32.   for(var i=1;i<dataGrid.length; i++)
  33.   {
  34.     message+= "<tr>"
  35.     message+= "<td>" + dataGrid[i][0] + "</td>"
  36.     message+= "<td>" + dataGrid[i][1] + "</td>"
  37.     message+= "<td>" + dataGrid[i][2] + "</td>"
  38.     message+= "</tr>"
  39.   }
  40.   //end of table tag
  41.   message +="</table>"
  42.  
  43.  
  44.   //send email. Done!
  45.  
  46. MailApp.sendEmail(email, "Your Points Update", "", {htmlBody:message});
  47.   Browser.msgBox("Email sent!");
  48.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement