Google script email scheduling logic error: all the emails are sent at one designated time, not at their designated time -
i making email scheduler using googlescript. each mail gets specific date&time sent. however, mails sent @ same time, @ first date&time entered. seems second date&time not recognized.
spreadsheet
as see time, 2 emails sent @ same time
please advice me how fix problem. below source. thank in advance.
function scheduleemails() { var sheet = spreadsheetapp.getactivesheet(); var startrow = 2; // first row of data process var datarange = sheet.getrange(startrow, 1, sheet.getlastrow(), 6) // fetch values each row in range. var data = datarange.getvalues(); (var = 0; < data.length; ++i) { var row = data[i]; var time1 = new date(row[5]); var time2 = time1.gettime(); var status = row[4]; var triggers = []; sheet.getrange(startrow + i, 5).setvalue("scheduled"); if (status === "scheduled") { triggers.push(time1); scriptapp.newtrigger("sendmails") .timebased() .at(new date(time2)) .create(); } } } function deletetriggers() { var alltriggers = scriptapp.getprojecttriggers(); (var = 0; < alltriggers.length; i++) { if (alltriggers[i].gethandlerfunction() === "sendmails") { scriptapp.deletetrigger(alltriggers[i]); } } } function sendmails() { var id1 = row[0]; execute(id1); } function execute(id1) { var sheet = spreadsheetapp.getactivesheet(); var startrow = 2; // first row of data process var datarange = sheet.getrange(startrow, 1, sheet.getlastrow(), 5) //start row&column, process row&column @ 1 time(not whole range) // fetch values each row in range. var data = datarange.getvalues(); //for (var = 0; < data.length; ++i) { // logger.log("i"+ i); var row = data[i]; var emailaddress = row[1]; // first column var title = row[2]; //second column var contents = row[3]; // third column var status = row[4]; // fourth column logger.log(emailaddress); logger.log(contents); if (status = "scheduled") { // prevents sending duplicates mailapp.sendemail(emailaddress, title, contents); sheet.getrange(startrow + i, 5).setvalue("email_sent"); // make sure cell updated right away in case script interrupted spreadsheetapp.flush(); } if (status = "email_sent") { deletetriggers(); } // } }
Comments
Post a Comment