如何在onEdit函数中调用mailApp函数?

时间:2019-02-19 05:52:29

标签: google-apps-script google-sheets

我有一个onEdit函数,如果执行该函数应发送一封电子邮件。但是,一些研究表明mailApp函数不能与onEdit一起使用。我目前正在考虑解决方法,但想知道其他人是否有自己想出的解决方案。

function onEdit(e) {
  var sheet = e.source.getActiveSheet();
  var sheetName = sheet.getName();
  //if editing specific sheet
  if (sheetName == "draft_test" ) {
    //if editing 6th column
    var r = e.source.getActiveRange();
    if (r.getColumn() == 6 ) {
      var player = sheet.getActiveCell().getValue();
      // Display a dialog box with a message and "Yes" and "No" buttons.
      var ui = SpreadsheetApp.getUi();
      var response = ui.alert('Do you want to draft '+player+'?', ui.ButtonSet.YES_NO);
      // Process the user's response.
      if (response == ui.Button.YES) {
        //***********FOR SOME REASON EXECUTING THIS FUNCTION ISNT WORKING******************
        emailLeague();
        //sortAutoRoster();
      } else {
      }
    }
  }
}

function emailLeague() {
  var draftSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("draft_test");
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("test_email");
  var emailSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Manager Info");

  //actual email address omitted
  var recipient = 'emailaddress@gmail.com';
  //get array of all picks on "draft test" tab, and find latest one
  var picks = draftSheet.getRange(3,6,146,1).getValues();

    for (var i=0; i<picks.length; i++){
    if (picks[i] == ""){
      var latestPick = i;
      break;
    }
   }  
  var subject = sheet.getRange(i+1, 2).getValue();
  var body = sheet.getRange(i+1, 1).getValue();
  MailApp.sendEmail(recipient, subject, body);
}

0 个答案:

没有答案