google apps script - How do I modify this sending email code to expand the range to multiple columns? -
i want send emails spreadsheet multiple people email them information in columns , not 1. example, want information b2:e2 emailed a2.
i using basic template provided in google support have attached below.
all changes code have made have not worked:
function sendemails() { var sheet = spreadsheetapp.getactivesheet(); var startrow = 2; // first row of data process var numrows = 2; // number of rows process // fetch range of cells a2:b3 var datarange = sheet.getrange(startrow, 1, numrows, 2) // fetch values each row in range. var data = datarange.getvalues(); (i in data) { var row = data[i]; var emailaddress = row[0]; // first column var message = row[1]; // second column var subject = "sending emails spreadsheet"; mailapp.sendemail(emailaddress, subject, message); } }
there quite few changes make original script, html our friend in context, nicer.
code :
function sendemails() { var sheet = spreadsheetapp.getactivesheet(); var startrow = 2; // first row of data process var numrows = 2; // number of rows process var colwidth = 5; // column width, including first 1 // fetch range of cells a2:e3, 1 email per row var datarange = sheet.getrange(startrow, 1, numrows, colwidth) // fetch values each row in range. var data = datarange.getvalues(); var subject = "sending emails spreadsheet"; (i in data) { var row = data[i]; var emailaddress = row[0]; // first column var message = '<body><div style="font-family:arial,sans;font-size:10pt"><p>welcome message</p>'; message+= '<table style="border-collapse:collapse;" border = 1 cellpadding = 4><tr>'; for(var n=1 ; n<row.length ; n++){ message += '<td bgcolor="#eef">'+row[n]+'</td>' } message += '</tr></table></div></body>'; mailapp.sendemail(emailaddress, subject, 'html only',{htmlbody:message}); } }
sheet example :
email result , third row :
Comments
Post a Comment