我正在尝试编写一个脚本,当Google表格电子表格中的某个单元格包含该人的名字时,该脚本将向特定人生成电子邮件。它被构建为任务跟踪器。基本上,有五个人可以分配任务。当将该名称输入到该项目的单元格中时(我的作业名称单元格在C列中),那么我想向他们发送电子邮件。我在同一文档的单独工作表中有电子邮件地址。我知道一旦完成,如何设置触发器。我编写了以下代码(请记住,需要再添加两个名称,但是您会从中得到灵感)。我不想发生的四件事。首先,它忽略了名称值。我在单元格中输入什么信息都没有关系,它会发送一封电子邮件。其次,无论分配给谁,它都会向我添加到此列表的每个人发送电子邮件。第三,它在同一作业中发送多封电子邮件。我只希望在分配作业后将电子邮件发送一次。最后,如果整个行都被删除(我有一个脚本,完成后将其移到另一张纸上),它将再次生成电子邮件。我的代码如下。如果我完全偏离了轨道,请告诉我如何写一些可行的方法。非常感谢!
function sendEmail() {
// Sends an email based on assignment
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Ongoing Projects").getRange("C");
var emailAddress = emailRange.getValue();
// Check if assignment made
if (emailRange = 'Name here'){
// Fetch the email address
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data Validation").getRange("D3");
var emailAddress = emailRange.getValues();
// Send Alert Email.
var message = 'You have a new assignment waiting on the Content Project Management Sheet. Please visit the sheet to see your new assignment. '; // Second column
var subject = 'New Content Development Assignment';
MailApp.sendEmail(emailAddress, subject, message);}
if (emailRange = 'Second Name Here'){
// Fetch the email address
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data Validation").getRange("D2");
var emailAddress = emailRange.getValues();
// Send Alert Email.
var message = 'You have a new assignment waiting on the Content Project Management Sheet. Please visit the sheet to see your new assignment. '; // Second column
var subject = 'New Content Development Assignment';
MailApp.sendEmail(emailAddress, subject, message);}
if (emailRange = 'Third Name Here'){
// Fetch the email address
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data Validation").getRange("D4");
var emailAddress = emailRange.getValues();}
// Send Alert Email.
var message = 'You have a new assignment waiting on the Content Project Management Sheet. Please visit the sheet to see your new assignment. '; // Second column
var subject = 'New Content Development Assignment';
MailApp.sendEmail(emailAddress, subject, message);
}
答案 0 :(得分:0)
问题可能是您多次使用“ emailRange”定义。 “ var emailRange = .....”可能会相互覆盖。尽管您可以在脚本中的任何位置定义变量,但是使用“ var”定义的所有变量都必须在对它们运行任何代码之前立即进行定义。因此,emailRange永远不会为空,并且始终会通过您的if语句。
我将使用多个变量名,例如emailRange,emailRange2,emailRange3等。或者,您可以使用“ let”代替“ var”来创建一些临时变量
答案 1 :(得分:0)
您尚未使用固定单元格范围
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Ongoing
Projects").getRange("C");
需要成为
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Ongoing
Projects").getRange("C3");
条件检查也需要==
if (emailRange = 'Name here'){
应该是
if (emailRange == 'Name here'){