Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. // create menu item across top of spreadsheet
  2. function onOpen() {
  3.  
  4. SpreadsheetApp.getUi()
  5. .createMenu('Email')
  6. .addItem('Send Email', 'sendEmailsConfirm') // label for menu item, name of function to run.
  7. .addToUi();
  8.  
  9. }
  10.  
  11.  
  12. // create popup box for user confirmation
  13. function sendEmailsConfirm() {
  14.  
  15. var ui = SpreadsheetApp.getUi();
  16.  
  17. var result = ui.alert(
  18. 'Send Emails?',
  19. 'Are you sure you want to continue?',
  20. ui.ButtonSet.YES_NO);
  21.  
  22. // Process the user's response.
  23. if (result == ui.Button.YES) {
  24. // User clicked "Yes".
  25. sendEmail() // run this Function to send emails
  26. }
  27. else {
  28. // User clicked "No" or X in the title bar so do nothing further
  29. }
  30.  
  31. }
  32.  
  33.  
  34. function sendEmail() {
  35.  
  36. // get active spreadsheet
  37. var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  38.  
  39. // get active sheet from spreadsheet
  40. var ss = spreadsheet.getActiveSheet();
  41.  
  42. // get last row of data from sheet for later loop
  43. var lastRow = ss.getLastRow();
  44. Logger.log('Last row of data is: ' + lastRow);
  45.  
  46.  
  47. // loop through each row in turn to get specific data, send email and write timestamp ***********
  48. for (var i=2; i<lastRow+1; i++) {
  49.  
  50. // get specific spreadsheet data ************************************
  51. var emailAddress = ss.getRange(i, 2).getValue();
  52. Logger.log('Email Address is: ' + emailAddress);
  53.  
  54. var firstName = ss.getRange(i, 3).getValue();
  55. Logger.log('First Name is: ' + firstName);
  56.  
  57. var lastName = ss.getRange(i, 4).getValue();
  58. Logger.log('Last Name is: ' + lastName);
  59.  
  60. var awardStatus = ss.getRange(i, 5).getValue();
  61. Logger.log('Award Status is: ' + awardStatus);
  62.  
  63. // get the 'Email Sent' cell and value separately for later use
  64. var emailSent = ss.getRange(i, 1);
  65. var emailSentValue = emailSent.getValue();
  66. // end of get specific spreadsheet data *****************************
  67.  
  68.  
  69.  
  70. /*
  71. check 'Email Sent' column IS blank and Email Address column IS NOT blank before proceeding,
  72. to avoid duplicate email sending and skip where no email address given
  73. */
  74. if ( (emailSentValue == '') && (emailAddress != '') ) {
  75.  
  76. // compose email *****************************************************
  77. var subject = 'Congratulations here is your result';
  78.  
  79. var body = 'Dear ' + firstName + ' ' + lastName + '\n\n';
  80. body+= 'You have been awarded a: ' + awardStatus + '\n\n';
  81. body+= 'Thank you for participating.'
  82. Logger.log(body);
  83.  
  84. var options = {replyTo:'reply@example.com'};
  85. // end of compose email **********************************************
  86.  
  87.  
  88.  
  89. // use try/catch to send email to prevent error if problem with email address ****************
  90. try {
  91. // send email
  92. MailApp.sendEmail(emailAddress, subject, body, options);
  93.  
  94. // set flag to true as send was successful
  95. var sendSuccessful = true;
  96. }
  97.  
  98. catch(e) {
  99. Logger.log('Failed to send email: ' + e);
  100.  
  101. // set flag to false as send was unsuccessful
  102. var sendSuccessful = false;
  103. }
  104. // end of use try/catch to send email to prevent error if problem with email address *********
  105.  
  106.  
  107.  
  108. // check flag status before writing the relevant text back to the 'Email Sent' column *********
  109. // if flag is true - meaning email has been sent:
  110. if (sendSuccessful) {
  111.  
  112. // get current date/time, timeZone and format nicely
  113. var date = new Date();
  114. var timeZone = spreadsheet.getSpreadsheetTimeZone();
  115. var timestamp = Utilities.formatDate(date, timeZone, "dd/MM/yy @ HH:mm:ss");
  116.  
  117. // insert the timestamp value
  118. emailSent.setValue(timestamp);
  119.  
  120. }
  121. // else if flag is not true - meaning email has not successfully sent:
  122. else {
  123.  
  124. // insert error message
  125. emailSent.setValue('Error sending email');
  126.  
  127. }
  128. // end of check flag status before writing the relevant text back to the 'Email Sent' column **
  129.  
  130.  
  131. }
  132. else {
  133. /*
  134. do nothing as either 'Email Sent' column is not blank or Email Address column is blank, so no
  135. attempts at sending email should be made
  136. */
  137. }
  138.  
  139.  
  140.  
  141. }
  142. // end of loop through each row in turn to get specific data, send email and write timestamp ****
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement