0

私の例sheetセルのifステートメントの返信に基づいて電子メールを送信します。

列Dのセルが「勝者」である場合、recipient1に電子メールを送信します。また、列Eのセルが「勝者」である場合、recipient2に電子メールを送信したいと思います。私のスクリプトは、列DまたはEの任意のセルに "WINNER"を手動で入力すると機能しますが、if文の結果としてセルが "WINNER"に変更されたときには機能しません。私はスクリプトを "WINNER"の代わりにtrueに変更しようとしましたが、それもうまくいきませんでした。

/** 
* add trigger for onedit - 
    * see menu -> Resouces -> Current project's triggers 
*/ 
function Initialize() { 

    var triggers = ScriptApp.getProjectTriggers(); 

    for(var i in triggers) { 
    ScriptApp.deleteTrigger(triggers[i]); 
    } 


    ScriptApp.newTrigger("sendNotification") 
    .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet()) 
    .onEdit() 
    .create(); 

    ScriptApp.newTrigger("sendNotification2") 
    .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet()) 
    .onEdit() 
    .create(); 

}; 


/** 
* 
*/ 

function sendNotification(f) { 


    if("D" == f.range.getA1Notation().charAt(0)) { 

    if(f.value == "WINNER") { 

//Define Notification Details 
     var recipients = "[email protected]"; 
     var subject = "Update"+f.range.getSheet().getName(); 
     var body = "Adam just won the lottery!"; 

    //Send the Email 
     MailApp.sendEmail(recipients, subject, body); 
    } 
    } 
} 

function sendNotification2(f) { 


    if("E" == f.range.getA1Notation().charAt(0)) { 

    if(f.value == "WINNER") { 



    //Define Notification Details 
     var recipients = "[email protected]"; 
     var subject = "Update"+f.range.getSheet().getName(); 
     var body = "Geo just won the lottery!"; 

    //Send the Email 
     MailApp.sendEmail(recipients, subject, body); 
    } 
    } 
} 
+0

私が読んで覚えているものは、実際に編集でトリガが、私は今のドキュメントを見つけることができませんかについて。あなたは直接あなたにsendNotification関数をトリガーするifロジックを変更しようとしましたか? –

答えて

0

threadでお調べいただけます。タイマートリガで実行される簡単なスクリプトを使用して、シート内の特定の列の変更を確認できます。

サンプルコード:

function checkColumnF() { 
    var sh = SpreadsheetApp.getActiveSheet(); 
    var values = sh.getRange('F1:F').getValues().join('-'); 
    if(PropertiesService.getScriptProperties().getKeys().length==0){ // first time you run the script 
    PropertiesService.getScriptProperties().setProperty('oldValues', values); 
    return; 
    } 
    var oldValues = PropertiesService.getScriptProperties().getProperty('oldValues').split('-'); 
    var valuesArray = values.split('-'); 
    while (valuesArray.length>oldValues.length){ 
    oldValues.push('x'); // if you append some rows since last exec 
    } 
    Logger.log('oldValues = '+oldValues) 
    Logger.log('current values = '+valuesArray) 
    for(var n=0;n<valuesArray.length;n++){ 
    if(oldValues[n] != valuesArray[n]){ // check for any difference 
     sendMail(n+1,valuesArray[n]); 
    } 
    } 
    PropertiesService.getScriptProperties().setProperty('oldValues', values); 
} 

function sendMail(row,val){ 
    Logger.log('value changed on row '+row+' value = '+val+' , mail sent'); 
    // uncomment below when you are sure everything runs fine to avoid sending dozens of emails while you test ! 
    //MailApp.sendEmail(Session.getActiveUser().getEmail(),'value changed in your sheet','Row '+row+' is now '+val); 
} 

追加参照:Email notifications with body/subject containing cell value

関連する問題