Google apps script 使用google sheets中的单元格值发送电子邮件通知
我正在创建一个谷歌工作表,输入新员工的名字、姓氏、头衔和部门,然后在添加新员工时向不同的人发送一封包含这些信息的电子邮件 我创建此代码是为了在编辑行(dept)中的最后一个单元格时触发电子邮件,我希望生成一封电子邮件,其中包括主题中前两个单元格(名字、姓氏)的值以及正文中所有4个单元格(第一、最后、标题、dept)的值 但有些地方不太正确,我得到了一个“未定义”的Rowdata错误。有什么建议吗Google apps script 使用google sheets中的单元格值发送电子邮件通知,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我正在创建一个谷歌工作表,输入新员工的名字、姓氏、头衔和部门,然后在添加新员工时向不同的人发送一封包含这些信息的电子邮件 我创建此代码是为了在编辑行(dept)中的最后一个单元格时触发电子邮件,我希望生成一封电子邮件,其中包括主题中前两个单元格(名字、姓氏)的值以及正文中所有4个单元格(第一、最后、标题、dept)的值 但有些地方不太正确,我得到了一个“未定义”的Rowdata错误。有什么建议吗 function sendNotificationNEWpersons() { var ss =
function sendNotificationNEWpersons() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Get Active cell
var mycell = ss.getActiveSelection();
var cellcol = mycell.getColumn();
var cellrow = mycell.getRow();
var rowData = sheet.getRange(cellrow, sheet.getLastColumn()).getValues();
//Defined Notification Details
var recipients = "xxxx@gmail.com";
var subject = "NEW EMPLOYEE: " + rowData[0][4] + rowData[0][5] + "(Row " + mycell.getRow() + ")";
var body = rowData[4] + rowData[5] + "has just been added to the NewPersons sheet on Row " + mycell.getRow() + " To view them, visit: " + ss.getUrl();
//If column 5 isn't empty in NewPersons sheet
if (cellcol == 5 && sheet.getName() == "NewPersons" && mycell.getValue() !=-1)
{
//Send the Email
MailApp.sendEmail(recipients, subject, body);
}
//End sendNotification
}
因此,上面的代码中有一些错误导致了未定义的错误。首先,您只使用单个单元格的内容填充rowData,而不是您可能希望的整行内容。其次,在设置主体和主体变量的值时,您错误地处理了rowData变量 假设您的行数据被放置在电子表格的A-D列中,并且rowData变量只应包含一行4列(对应于该顺序中的名字、姓氏、标题和部门),下面是代码的固定版本:
function sendNotificationNEWpersons() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var mycell = ss.getActiveSelection();
var cellcol = mycell.getColumn();
var cellrow = mycell.getRow();
// Get the full row of data, not just one cell
var rowData = sheet.getSheetValues(cellrow, 1, 1, sheet.getLastColumn());
// rowData is now a 2D array of Object (cell values), with a single row
// and sheet.getLastColumn() columns
// Here, only including the first and last names in the email
var recipients = "xxxx@gmail.com";
var subject = "NEW EMPLOYEE: " + rowData[0][0] + " " + rowData[0][1] +
"(Row " + mycell.getRow() + ")";
var body = rowData[0][0] + " " + rowData[0][1] +
"has just been added to the NewPersons sheet on Row " +
mycell.getRow() + " To view them, visit: " + ss.getUrl();
//If column 5 isn't empty in NewPersons sheet
if (cellcol == 5 && sheet.getName() == "NewPersons" &&
mycell.getValue() !=-1) {
//Send the Email
MailApp.sendEmail(recipients, subject, body);
}
}
代码将避免您看到的错误。然而,这个解决方案可能不是最好的设计。您不需要指定如何调用此函数(可以使用菜单控件、OneEdit触发器或其他函数)——这取决于当前选定的单元格位于最新用户行且位于第5列的假设可能不正确
下面是一些你可能需要考虑的其他设计: