Google 应用程序脚本 - 自动发送电子邮件(特定问题删除行和 appendRowL)
Google App Scripts - Auto Sending Emails (specific issue delete row and appendRowL)
我正在尝试使用 Google 应用程序脚本为我发送我写的 HTML 电子邮件。我的代码工作到可以发送电子邮件的程度,但是正如您将在我的代码中看到的那样,我有 4 个不同的模板要发送。 .正如您将看到的,我有一些代码可以将模板更改 1 以找到正确的模板和 dateMath 来增加发送下一封电子邮件的日期。
我现在 运行 遇到的问题是 deleteRow 和 appendRow。我有 2 个用户正在尝试通过电子邮件发送,当我 运行 代码 deleteRow 和 appendRow 将其中一个用户替换为另一个用户。我的屏幕截图会更有意义。这里有什么见解吗?
Before Script
After Script
function sendEmails(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
var today = Utilities.formatDate(new Date(), "MDT", "dd/MM/yyyy");
for(var i = 1; i<data.length; i++){
if(data[i][3] !== ""){ //skip if is empty
if(isValidDate(data[i][3])){ //skip if isn't a valid date
var formattedDate = Utilities.formatDate(data[i][3], "MDT", "dd/MM/yyyy");
if(formattedDate == today){ //send email if checkin date is today
if(data[i][4] == 1) {
var template = HtmlService.createTemplateFromFile('Template1');
} else if(data[i][4] == 2){
var template = HtmlService.createTemplateFromFile('Template2');
} else if(data[i][4] == 3){
var template = HtmlService.createTemplateFromFile('Template3');
} else {
var template = HtmlService.createTemplateFromFile('Template4');
}
var email = data[i][2];
var firstName = data[i][0];
var lastName = data[i][1];
template.firstName = firstName;
var subject = "Your Next Steps ";
var bcc = "spencer@kwwestfield.com";
var message = template.evaluate();
GmailApp.sendEmail(email,
subject,
message.getContent(), {
htmlBody: message.getContent(),
bcc: bcc
});
//change the template
//delete the row
sheet.deleteRow(i+1);
//add the row again
var newTemplate = data[i][4] + 1;
var newSend = dateMath(data[i][3], 8);
sheet.appendRow([firstName, lastName, email, newSend, newTemplate]);
}
}
}
}
}
/**
* Does math on dates
* Triggered from functions
* Input: date = the orginal date, d = +- number of days
* Output: a new date
*/
function dateMath(date,d){
var result = new Date(date.getTime()+d*(24*3600*1000));
return result
}
/**
* Figures out if is a data
* Triggered from functions
* Input: d: any
* Output: boolean
*/
function isValidDate(d) {
if ( Object.prototype.toString.call(d) !== "[object Date]" ){
return false;
} else {
return true;
}
}
因为您要删除一行,所以 i
值不再对应于同一范围。例如,您有一个包含 5 个元素的数组:
[A, B, C, D, E]
如果删除元素 0
,您将得到
[B, C, D, E]
现在你递增你的迭代器,所以 i++; // i = 1
所以你要操作的下一个 value 不是 "B",而是 [=32] =].
你可以通过运行这个
自己试试
function test() {
var letters = ["A", "B", "C", "D", "E"];
for (var i=0; i<letters.length; i++) {
Logger.log("i : " + i + " || Letter: " + letters[i]);
letters.shift();
}
}
要修复您的代码,请添加一个独立于您的数组迭代器 i
的 row
变量。
function sendEmails(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
var today = Utilities.formatDate(new Date(), "MDT", "dd/MM/yyyy");
var row = 2; // Starting at row 2
for(var i = 1; i<data.length; i++){
if(data[i][3] !== ""){ //skip if is empty
if(isValidDate(data[i][3])){ //skip if isn't a valid date
var formattedDate = Utilities.formatDate(data[i][3], "MDT", "dd/MM/yyyy");
if(formattedDate == today){ //send email if checkin date is today
if(data[i][4] == 1) {
var template = HtmlService.createTemplateFromFile('Template1');
} else if(data[i][4] == 2){
var template = HtmlService.createTemplateFromFile('Template2');
} else if(data[i][4] == 3){
var template = HtmlService.createTemplateFromFile('Template3');
} else {
var template = HtmlService.createTemplateFromFile('Template4');
}
var email = data[i][2];
var firstName = data[i][0];
var lastName = data[i][1];
template.firstName = firstName;
var subject = "Your Next Steps ";
var bcc = "spencer@kwwestfield.com";
var message = template.evaluate();
GmailApp.sendEmail(email,
subject,
message.getContent(), {
htmlBody: message.getContent(),
bcc: bcc
});
//change the template
//delete the row
sheet.deleteRow(row);
row--; // Deleted a row
//add the row again
var newTemplate = data[i][4] + 1;
var newSend = dateMath(data[i][3], 8);
sheet.appendRow([firstName, lastName, email, newSend, newTemplate]);
}
}
}
row++; // Go to the next row
}
}
我正在尝试使用 Google 应用程序脚本为我发送我写的 HTML 电子邮件。我的代码工作到可以发送电子邮件的程度,但是正如您将在我的代码中看到的那样,我有 4 个不同的模板要发送。 .正如您将看到的,我有一些代码可以将模板更改 1 以找到正确的模板和 dateMath 来增加发送下一封电子邮件的日期。
我现在 运行 遇到的问题是 deleteRow 和 appendRow。我有 2 个用户正在尝试通过电子邮件发送,当我 运行 代码 deleteRow 和 appendRow 将其中一个用户替换为另一个用户。我的屏幕截图会更有意义。这里有什么见解吗? Before Script
After Script
function sendEmails(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
var today = Utilities.formatDate(new Date(), "MDT", "dd/MM/yyyy");
for(var i = 1; i<data.length; i++){
if(data[i][3] !== ""){ //skip if is empty
if(isValidDate(data[i][3])){ //skip if isn't a valid date
var formattedDate = Utilities.formatDate(data[i][3], "MDT", "dd/MM/yyyy");
if(formattedDate == today){ //send email if checkin date is today
if(data[i][4] == 1) {
var template = HtmlService.createTemplateFromFile('Template1');
} else if(data[i][4] == 2){
var template = HtmlService.createTemplateFromFile('Template2');
} else if(data[i][4] == 3){
var template = HtmlService.createTemplateFromFile('Template3');
} else {
var template = HtmlService.createTemplateFromFile('Template4');
}
var email = data[i][2];
var firstName = data[i][0];
var lastName = data[i][1];
template.firstName = firstName;
var subject = "Your Next Steps ";
var bcc = "spencer@kwwestfield.com";
var message = template.evaluate();
GmailApp.sendEmail(email,
subject,
message.getContent(), {
htmlBody: message.getContent(),
bcc: bcc
});
//change the template
//delete the row
sheet.deleteRow(i+1);
//add the row again
var newTemplate = data[i][4] + 1;
var newSend = dateMath(data[i][3], 8);
sheet.appendRow([firstName, lastName, email, newSend, newTemplate]);
}
}
}
}
}
/**
* Does math on dates
* Triggered from functions
* Input: date = the orginal date, d = +- number of days
* Output: a new date
*/
function dateMath(date,d){
var result = new Date(date.getTime()+d*(24*3600*1000));
return result
}
/**
* Figures out if is a data
* Triggered from functions
* Input: d: any
* Output: boolean
*/
function isValidDate(d) {
if ( Object.prototype.toString.call(d) !== "[object Date]" ){
return false;
} else {
return true;
}
}
因为您要删除一行,所以 i
值不再对应于同一范围。例如,您有一个包含 5 个元素的数组:
[A, B, C, D, E]
如果删除元素 0
,您将得到
[B, C, D, E]
现在你递增你的迭代器,所以 i++; // i = 1
所以你要操作的下一个 value 不是 "B",而是 [=32] =].
你可以通过运行这个
自己试试function test() {
var letters = ["A", "B", "C", "D", "E"];
for (var i=0; i<letters.length; i++) {
Logger.log("i : " + i + " || Letter: " + letters[i]);
letters.shift();
}
}
要修复您的代码,请添加一个独立于您的数组迭代器 i
的 row
变量。
function sendEmails(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
var today = Utilities.formatDate(new Date(), "MDT", "dd/MM/yyyy");
var row = 2; // Starting at row 2
for(var i = 1; i<data.length; i++){
if(data[i][3] !== ""){ //skip if is empty
if(isValidDate(data[i][3])){ //skip if isn't a valid date
var formattedDate = Utilities.formatDate(data[i][3], "MDT", "dd/MM/yyyy");
if(formattedDate == today){ //send email if checkin date is today
if(data[i][4] == 1) {
var template = HtmlService.createTemplateFromFile('Template1');
} else if(data[i][4] == 2){
var template = HtmlService.createTemplateFromFile('Template2');
} else if(data[i][4] == 3){
var template = HtmlService.createTemplateFromFile('Template3');
} else {
var template = HtmlService.createTemplateFromFile('Template4');
}
var email = data[i][2];
var firstName = data[i][0];
var lastName = data[i][1];
template.firstName = firstName;
var subject = "Your Next Steps ";
var bcc = "spencer@kwwestfield.com";
var message = template.evaluate();
GmailApp.sendEmail(email,
subject,
message.getContent(), {
htmlBody: message.getContent(),
bcc: bcc
});
//change the template
//delete the row
sheet.deleteRow(row);
row--; // Deleted a row
//add the row again
var newTemplate = data[i][4] + 1;
var newSend = dateMath(data[i][3], 8);
sheet.appendRow([firstName, lastName, email, newSend, newTemplate]);
}
}
}
row++; // Go to the next row
}
}