我正在尝试在Google表格中设置我的下拉选项,以在从下拉菜单中进行选择后触发电子邮件回复。例如,当从下拉列表的第I列中选择“准备进行质量检查”时,应触发一封电子邮件并将其发送到H列的电子邮件地址,其中包括主题行:“行项目已准备好进行质量检查”,其中包含所有单元格的信息在A到P行中,其中包括每列的标题。我在下面的代码中缺少任何内容吗?
function onOpen() {//you have to change this name when you make it an installable trigger
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
var range = sheet.getRange(8, 9, sheet.getLastRow()-7, 1);//third parameter is the number of rows
var list = SpreadsheetApp.newDataValidation().requireValueInList(["Ready for QA", "Approved", "Error Found"], true).build();
range.setDataValidation(list);
}
function onEdit() {//you have to change this name when you make it an installable trigger
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
var ac = sheet.getActiveCell(sheet.getLastRow(), 1);//what does sheet.getLastRow have to do with the active cell
//the value of the active cell for an onedit is e.value
if (ac.getValue() == "Ready for QA") {
data = sheet.getRange(ac.getRowIndex(),9,8,7).getValues();
Logger.log(data)
MailApp.sendEmail({
to: data[0][0],
subject: "sample mail",
body: data[0][1]
});
}
}
要发送符合您想要的条件和格式的电子邮件,您必须设置Installable Trigger并使用我准备的代码:
//Installable Trigger
function sendEmail(e) {
// Get sheet, row and column where the event was triggered
var sheet = e.source.getSheetByName("YOUR-SHEET-NAME");
var range = e.range;
var col = range.getColumn();
var row = range.getRow();
// Check if the event meets your conditions
if (e.value === "Ready for QA" && col === 9) {
// Get headers and data where the "Ready for QA" was set
var headers = sheet.getRange("A1:P1").getValues()[0];
var data = sheet.getRange(row, 1, 1, 16).getValues()[0];
MailApp.sendEmail({
to: data[7], // user's email Also
subject: `Line item is ${e.value}`,
// Combine the right header with the right data and send them as the body
body: headers.map((el, index) => `${el}: ${data[index]}`).join("\n")
});
}
}
注意我如何使用e
参数,它是Event Object事件触发器的onEdit以获取工作表值。另外,我建议您将列[Order ID]的格式设置为Plain text
,因为Apps Script会将其视为一个巨大的数字,因此将其设置为科学计数法。
现在,要设置可安装的触发器,请执行以下操作:
1)转到您的Apps脚本项目。
2)单击编辑->当前项目的触发器。
3)单击“ +添加触发器”。
4)选择:
选择要运行的功能-> 功能名称(在本例中为sendEmail)。
选择事件源->从电子表格。
选择事件类型->编辑时。
function onThisOpen() {
var sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(*************Neeed a sheet name***************);
var range=sheet.getRange(8, 9, sheet.getLastRow()-7, 1);
var list=SpreadsheetApp.newDataValidation().requireValueInList(["Ready for QA", "Approved", "Error Found"], true).build();
range.setDataValidation(list);
}
function onMyEdit(e) {
var sh=e.range.getSheet();
if (e.value=="Ready for QA") {
var data=sh.getRange(e.range.rowStart,9,8,7).getValues();
Logger.log(data)
MailApp.sendEmail({to: data[0][0],subject: "sample mail",body: data[0][1]});
}
}
//this will create an onEdit trigger
function createOnEditTrigger() {
if(!isTrigger('onMyEdit')) {
ScriptApp.newTrigger('onMyEdit').forSpreadsheet(SpreadsheetApp.getActive().getId()).onEdit().create();
}
}
//this will create an onOpen trigger
function createOnOpenTriggere() {
if(!isTrigger('onThisOpen')) {
ScriptApp.newTrigger('onThisOpen').forSpreadsheet(SpreadsheetApp.getActive().getId()).onOpen().create();
}
}
function isTrigger(funcName){
var r=false;
if(funcName){
var allTriggers=ScriptApp.getProjectTriggers();
for(var i=0;i<allTriggers.length;i++){
if(funcName==allTriggers[i].getHandlerFunction()){
r=true;
break;
}
}
}
return r;
}