仅在一个特定工作表中完成更改后才发送邮件

时间:2018-12-14 11:36:30

标签: google-apps-script google-sheets

我想生成一个代码,根据发生的更改的位置(同一电子表格中其他工作表中的当前工作表)向不同的电子邮件地址发送推送通知。例如,我可以将工作表命名为“ firstsheet”和“ secondsheet”。不幸的是,我没有编码经验,发现的只有以下几点:

function 1() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
if (ss.getName() == "firstsheet") return;
MailApp.sendEmail("emailaddress1","Title","Content");

}
function 2() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
if (ss.getName() == "secondsheet") return;
MailApp.sendEmail("emailaddress2","Title","Content");

}

但这会导致在任何时间都向这两个地址发送电子邮件,两个工作表中都发生了更改,这意味着根本没有分隔。你能帮我吗?

1 个答案:

答案 0 :(得分:0)

看看,让我知道是否需要任何澄清/这不能解决您的问题。

function change_made_to_sheet(event_object) {
    var sheet = event_object.range.getSheet(); // Get the sheet in which changes have been made
    if (sheet.getName() == "firstsheet") { // Check if the sheet name is matching with firstsheet
        // If the sheet name matched then execute this block when the sheet
        MailApp.sendEmail("emailaddress1","Title","Content"); // Send email
    } else if (sheet.getName() == "secondsheet") { // Check if sheet name matches secondsheet
        // If matched then execute this block
        MailApp.sendEmail("emailaddress2","Title2","Content2"); // Send email
    }
}