1919 * Sends emails with data from the current spreadsheet.
2020 */
2121function sendEmails ( ) {
22- try {
23- const sheet = SpreadsheetApp . getActiveSheet ( ) ; // Get the active sheet in spreadsheet
24- const startRow = 2 ; // First row of data to process
25- const numRows = 2 ; // Number of rows to process
26- const dataRange = sheet . getRange ( startRow , 1 , numRows , 2 ) ; // Fetch the range of cells A2:B3
27- const data = dataRange . getValues ( ) ; // Fetch values for each row in the Range.
28- for ( const row of data ) {
29- const emailAddress = row [ 0 ] ; // First column
30- const message = row [ 1 ] ; // Second column
31- const subject = 'Sending emails from a Spreadsheet' ;
32- MailApp . sendEmail ( emailAddress , subject , message ) ; // Send emails to emailAddresses which are presents in First column
33- }
34- } catch ( err ) {
35- console . log ( err ) ;
22+ const sheet = SpreadsheetApp . getActiveSheet ( ) ;
23+ const startRow = 2 ;
24+ const numRows = 2 ;
25+ const dataRange = sheet . getRange ( startRow , 1 , numRows , 2 ) ;
26+ const data = dataRange . getValues ( ) ;
27+ for ( const row of data ) {
28+ const emailAddress = row [ 0 ] ;
29+ const message = row [ 1 ] ;
30+ const subject = 'Sending emails from a Spreadsheet' ;
31+ MailApp . sendEmail ( emailAddress , subject , message ) ;
3632 }
3733}
3834// [END gmail_send_emails]
@@ -42,29 +38,25 @@ function sendEmails() {
4238 * Sends non-duplicate emails with data from the current spreadsheet.
4339 */
4440function sendNonDuplicateEmails ( ) {
45- const EMAIL_SENT = 'email sent' ; //This constant is used to write the message in Column C of Sheet
46- try {
47- const sheet = SpreadsheetApp . getActiveSheet ( ) ; // Get the active sheet in spreadsheet
48- const startRow = 2 ; // First row of data to process
49- const numRows = 2 ; // Number of rows to process
50- const dataRange = sheet . getRange ( startRow , 1 , numRows , 3 ) ; // Fetch the range of cells A2:B3
51- const data = dataRange . getValues ( ) ; // Fetch values for each row in the Range.
52- for ( let i = 0 ; i < data . length ; ++ i ) {
53- const row = data [ i ] ;
54- const emailAddress = row [ 0 ] ; // First column
55- const message = row [ 1 ] ; // Second column
56- const emailSent = row [ 2 ] ; // Third column
57- if ( emailSent === EMAIL_SENT ) {
58- console . log ( 'Email already sent' ) ;
59- return ;
60- }
61- const subject = 'Sending emails from a Spreadsheet' ;
62- MailApp . sendEmail ( emailAddress , subject , message ) ; // Send emails to emailAddresses which are presents in First column
63- sheet . getRange ( startRow + i , 3 ) . setValue ( EMAIL_SENT ) ;
64- SpreadsheetApp . flush ( ) ; // Make sure the cell is updated right away in case the script is interrupted
41+ const EMAIL_SENT = 'email sent' ;
42+ const sheet = SpreadsheetApp . getActiveSheet ( ) ;
43+ const startRow = 2 ;
44+ const numRows = 2 ;
45+ const dataRange = sheet . getRange ( startRow , 1 , numRows , 3 ) ;
46+ const data = dataRange . getValues ( ) ;
47+ for ( let i = 0 ; i < data . length ; ++ i ) {
48+ const row = data [ i ] ;
49+ const emailAddress = row [ 0 ] ;
50+ const message = row [ 1 ] ;
51+ const emailSent = row [ 2 ] ;
52+ if ( emailSent === EMAIL_SENT ) {
53+ console . log ( 'Email already sent' ) ;
54+ return ;
6555 }
66- } catch ( err ) {
67- console . log ( err ) ;
56+ const subject = 'Sending emails from a Spreadsheet' ;
57+ MailApp . sendEmail ( emailAddress , subject , message ) ;
58+ sheet . getRange ( startRow + i , 3 ) . setValue ( EMAIL_SENT ) ;
59+ SpreadsheetApp . flush ( ) ;
6860 }
6961}
7062// [END gmail_send_non_duplicate_emails]
0 commit comments