[英]How mark a cell in each row every time an email is sent
I am looking for a way to get a cell to mark if the email gets sent from a google sheet.如果电子邮件是从 Google 表格发送的,我正在寻找一种方法来标记单元格。 For an example if the email sent it would say in column P "email sent" on that row or something like that.例如,如果发送的电子邮件会在该行的 P 列中显示“已发送电子邮件”或类似内容。 I'm pretty new to this I found some helpful similar questions but I just don't know how to set it up.我对此很陌生,我发现了一些有用的类似问题,但我只是不知道如何设置。 I think I need to use an IF command but don't know how or where to set it我想我需要使用 IF 命令,但不知道如何或在哪里设置它
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);
}
}
}
I'm new to answering, but I hope this helps you out!我是新来的回答,但我希望这可以帮助你! It sounds like you want to mark next to each row of a sent email, in which case, you already have your if/for cases setup to do this (except that you appear to be missing one set of braces for the last if test, as below).听起来您想在已发送电子邮件的每一行旁边进行标记,在这种情况下,您已经设置了 if/for 案例来执行此操作(除了最后一个 if 测试似乎缺少一组大括号,如下)。 You can just add whatever text you want to column P in the row after the email gets sent.发送电子邮件后,您可以在行中的 P 列中添加任何您想要的文本。 Here is that section of the code with the added part to add text to column 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)
}
}
You could use setBackground("color")
too if you want to change background color of your cell.如果您想更改单元格的背景颜色,也可以使用setBackground("color")
。
Here is a small example:这是一个小例子:
ss.getRange(5, 15).setBackground("green")
- it will change background color to green in cell N5 ss.getRange(5, 15).setBackground("green")
- 它将单元格 N5 中的背景颜色更改为绿色
This is how it looks like in your snippet, (below MailApp, trigger setBackground after MailApp does the work):这就是您的代码段中的样子(在 MailApp 下方,在 MailApp 完成工作后触发 setBackground):
function SendStudentEmails() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Homework Hour").activate();
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastRow = ss.getLastRow();
var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DO NOT DELETE! Parent Email Template").getRange(1, 1).getValue();
var quotaLeft = MailApp.getRemainingDailyQuota();
if ((lastRow - 1) > quotaLeft) {
Browser.msgBox("You have " + quotaLeft + " left and you're trying to send " + (lastRow - 1) + " emails. Emails were not sent")
} else {
for (var i = 2; i <= lastRow; i++) {
var currentEmail = ss.getRange(i, 15).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() !== '' && ss.getRange(i,15).getBackground !== 'green') { // checks if email is not blank
MailApp.sendEmail(currentEmail, currentStudent, massageBody);
ss.getRange(i, 15).setBackground("green") // change cell background to green
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.