我正在创建一个Google表格,用他们的名字,姓氏,职位和部门输入新员工,然后在添加新员工时通过该信息生成一封电子邮件。
我已创建此代码,以便在编辑行(dept)中的最后一个单元格时触发电子邮件,并且我想生成一封电子邮件,其中包含前两个单元格的值(名字,姓氏)在受试者和身体中的所有4个细胞(第一个,最后一个,标题,部门)。
但有些事情不太正确,我为Rowdata收到了“未定义”的错误。有什么建议吗?
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
}
答案 0 :(得分:2)
因此,您的代码中存在一些导致未定义错误的错误。首先,您只使用单个单元格的内容填充rowData,而不是您可能想要的整行。其次,在设置主题和正文变量的值时,你是错误地处理了rowData变量。
假设您的行数据被放置在电子表格的AD列中,而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);
}
}
代码将避免您看到的错误。但是,这种解决方案可能不是最好的设计。您没有指定如何调用此函数(您可能正在使用菜单控件或onEdit触发器或其他函数) - 取决于当前选择的单元格位于最新人员行并且位于列中的假设5可能不正确。
以下是您可能需要考虑的其他一些设计: