Javascript 如何在每次发送电子邮件时标记每行中的单元格
我正在寻找一种方法,让一个细胞来标记,如果电子邮件是从谷歌表发送。例如,如果发送的电子邮件会在该行的P列中显示“email sent”或类似内容。我对这个很陌生,我发现了一些有用的类似问题,但我只是不知道如何设置它。我想我需要使用IF命令,但不知道如何或在哪里设置它Javascript 如何在每次发送电子邮件时标记每行中的单元格,javascript,google-apps-script,google-sheets,email,Javascript,Google Apps Script,Google Sheets,Email,我正在寻找一种方法,让一个细胞来标记,如果电子邮件是从谷歌表发送。例如,如果发送的电子邮件会在该行的P列中显示“email sent”或类似内容。我对这个很陌生,我发现了一些有用的类似问题,但我只是不知道如何设置它。我想我需要使用IF命令,但不知道如何或在哪里设置它 function SendStudentEmails() { SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Homework Hour").activate();/
function SendStudentEmails() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Homework Hour").activate();//use name of active sheet
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DO NOT DELETE! Parent Email Template").getRange(1, 1).getValue();
var quotaLeft = MailApp.getRemainingDailyQuota();
if((lr-1) > quotaLeft) {
Browser.msgBox("You have " + quotaLeft + " left and you're trying to send" (lr-1) + " emails. Emails were not sent")
} else {
for (var i = 2;i<=lr;i++){
var currentEmail = ss.getRange(i,14).getValue(); // 15 is the email column
var currentStudent = ss.getRange(i, 4).getValue(); // 4 is student name column
var currentMessage = ss.getRange(i, 2).getValue(); // 2 is the message column
var currentMissingAssingment = ss.getRange(i, 9).getValue(); // 9 is the missing assingment column
var massageBody = templateText.replace("<<Student Name>>",currentStudent).replace("<<Message>>",currentMessage).replace("<<Missing Assingment>>",currentMissingAssingment);
var subjectLine = currentStudent +" Has Homework Hour";
if (currentEmail.trim() !== '')// checks if email is not blank
MailApp.sendEmail(currentEmail, currentStudent, massageBody);
}
}
}
函数SendStudentEmails(){
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“家庭作业小时”).activate();//使用活动工作表的名称
var ss=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr=ss.getLastRow();
var templateText=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“不要删除!父电子邮件模板”).getRange(1,1).getValue();
var quotaLeft=MailApp.getRemainingDailyQuota();
如果((lr-1)>quotaLeft){
msgBox(“您还剩下“+quotaLeft+”并且您正在尝试发送”(lr-1)+“电子邮件。未发送电子邮件”)
}否则{
对于(var i=2;i我对回答还不熟悉,但我希望这对您有所帮助! 听起来您希望在发送的电子邮件的每一行旁边进行标记,在这种情况下,您已经设置了if/for cases来执行此操作(除了您在最后一次if测试中似乎缺少一组大括号,如下所示)。在发送电子邮件后,您可以在行中添加任何要添加到P列的文本。下面是代码的该部分,其中添加了将文本添加到P列的部分:
if (currentEmail.trim() !== '')// checks if email is not blank {
MailApp.sendEmail(currentEmail, currentStudent, massageBody);
ss.getRange(i,16).setValue("email sent") // writes text (email sent) into column P (col 16), row i (the current row in the for loop)
}
}
如果要更改单元格的背景色,也可以使用
setBackground(“color”)
下面是一个小例子:
ss.getRange(5,15).setBackground(“绿色”)
-它会将单元格N5中的背景色更改为绿色
这是您的代码片段中的情况(在MailApp下面,在MailApp完成工作后触发setBackground):
函数SendStudentEmails(){
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“家庭作业小时”).activate();
var ss=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastRow=ss.getLastRow();
var templateText=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“不要删除!父电子邮件模板”).getRange(1,1).getValue();
var quotaLeft=MailApp.getRemainingDailyQuota();
如果((最后一行-1)>quotaLeft){
msgBox(“您还有“+quotaLeft+”,您正在尝试发送“+(lastRow-1)+”电子邮件。电子邮件未发送”)
}否则{
对于(var i=2;i这太棒了!谢谢你!@NathanGartner如果你认为我的答案对你有帮助,就直接投票吧,其他社区成员会更容易看到它:)好luckI还有一个问题。如果邮件发送过一次,有没有办法让它停止重新发送?@NathanGartner我已经更新了一个代码,请看第27行,如果背景颜色是绿色,请下一步。