Google Spreadsheet - 如何编写正确的If语句和Browser.msgBox返回函数

时间:2014-03-19 09:06:34

标签: google-apps-script google-sheets

我试图找出是,如何编写if语句,以便它将根据它将在电子表格中检查的两个值发送单独的电子邮件。

以下是N列中的两个值:

“进行中”

“已完成”

以下是代码部分:

var sheetNameToWatch = "Active Discs"; // Active spreadsheet that contains the data
var columnNumberToWatch = 14; // Column N
var valueToWatch1 = "In Progress"; // First value to watch for in column N
var valueToWatch2 = "Completed"; // Second value to watch for in column N
/*var valueToWatch3 = "Rejected"; // Third value to watch for in columns for labeled PM         Review and Date Discs Shipped*/
var timestamp = new Date(); // Timestamp
var activeSessionuser = Session.getEffectiveUser(); //Get the current user in the     spreadsheet
var replyTo = 'test@test.com';

function sendEmailNotifications(e) {
//Logger.log(JSON.stringify(e));
    try{
var ss = e.source;
var sheet = ss.getActiveSheet();
var range = e.range;

//Checks the variables sheetNameToWatch + columnNumberToWatch + valueToWatch1 ("in progress")
//Sends Email to Venue Colombo Team
  if (sheet.getName() == sheetNameToWatch && range.columnStart == columnNumberToWatch && (e.value == valueToWatch1)){

    var confirm = Browser.msgBox('Email will be sent to the Venue Colombo Team. Do you want to sent this email?', Browser.Buttons.YES_NO); 
if(confirm.toLowerCase()=='yes'){sendColomboMessage(e.range.rowStart)};
    SpreadsheetApp.getActiveSpreadsheet().toast('Email Sent to Venue Colombo Team')
}

本节我试图编写“else”或“else if”语句,以便注意“完成并发送不同的电子邮件。

 else (sheet.getName() == sheetNameToWatch && range.columnStart == columnNumberToWatch && (e.value == valueToWatch2)){

    var confirm = Browser.msgBox('Email will be sent to the Venue Colombo Team. Do you want to sent this email?', Browser.Buttons.YES_NO); 
if(confirm.toLowerCase()=='yes'){sendVCSMessage(e.range.rowStart)};
    SpreadsheetApp.getActiveSpreadsheet().toast('Email Sent to VCS Team')*/

}catch(err){Logger.log('catch err = '+err)}
}

最后,当我添加“return;”对于Browser.msgbox的代码,它不会停止代码运行或整个功能停止工作。我在哪里或如何添加回报;如果用户单击“否”。然后它应该关闭消息框而不执行任何操作。

if(confirm.toLowerCase()=='yes'){sendColomboMessage(e.range.rowStart)};
    SpreadsheetApp.getActiveSpreadsheet().toast('Email Sent to Venue Colombo Team')

1 个答案:

答案 0 :(得分:0)

这应该做你想做的事情(有很多重复的功能可以分解成另一个功能):

var sheetNameToWatch = "Active Discs"; // Active spreadsheet that contains the data
var columnNumberToWatch = 14; // Column N
var valueToWatch1 = "In Progress"; // First value to watch for in column N
var valueToWatch2 = "Completed"; // Second value to watch for in column N
/*var valueToWatch3 = "Rejected"; // Third value to watch for in columns for labeled PM         Review and Date Discs Shipped*/
var timestamp = new Date(); // Timestamp
var activeSessionuser = Session.getEffectiveUser(); //Get the current user in the     spreadsheet
var replyTo = 'venueclientservices@rrd.com';

function sendEmailNotifications(e) {

  Logger.log(JSON.stringify(e));

  Logger.log('sendEmailNotifications()');

  try {

    var ss = e.source;
    var sheet = ss.getActiveSheet();
    var range = e.range;
    var ass = SpreadsheetApp.getActiveSpreadsheet();
    var confirm;
    var getName = sheet.getName();
    var columnStart = range.columnStart;

    Logger.log("getName: " + sheet.getName());
    Logger.log("columnStart: " + range.columnStart);
    Logger.log("e.value: " + e.value);

    if (getName == sheetNameToWatch && 
        columnStart == columnNumberToWatch) {

      if (e.value == valueToWatch1) {

        Logger.log("Value 1");

        confirm = Browser.msgBox('Email will be sent to the Venue Colombo Team. Do you want to sent this email?', 
                                 Browser.Buttons.YES_NO); 

        if (confirm.toLowerCase() == 'yes') {

          sendColomboMessage(e.range.rowStart) 

          ass.toast('Email Sent to Venue Colombo Team')

        } else {

          ass.toast('Edit In Progress but no email sent');

        }

      } else if (e.value == valueToWatch2) {

        Logger.log("Value 2");

        confirm = Browser.msgBox('Email will be sent to the VCS Team. Do you want to sent this email?', 
                                 Browser.Buttons.YES_NO);

        if (confirm.toLowerCase() == 'yes') { 

          sendVCSMessage(e.range.rowStart);
          ass.toast('Email Sent to VCS Team');

        } else {

          ass.toast('Edit Value 2 but no email sent'); 

        }

      } else {

        ass.toast('Edit, but no email sent.');

      }

    } // matching sheet and col

  } // try

  catch (err) {

    Logger.log('catch err = ' + err);

  }

  function sendColomboMessage(row) {

    Logger.log("sendColomboMessage, row: " + row);
  };

  function sendVCSMessage(row) {

    Logger.log("sendVCSMessage, row: " + row);
  };

} // sendEmailNotifications()