在GSheets中停止Null值的脚本

时间:2017-09-12 01:25:48

标签: google-apps-script

我正在尝试发送状态更改的验证电子邮件,我可以根据列内容将其发送,但是一旦看到空值,我就无法停止。它无论如何都会放入电子邮件确认标签。如果G列中的单元格为空,我需要知道如何使其停止为null。 Screenshot of sheet

var CONFIRMATION_SENT = "CONFIRMATION_SENT";

function sendConfirmation(e){
 var sheet = SpreadsheetApp.getActiveSheet();
  // Fetch the range of cells A1:range
  var dataRange = sheet.getDataRange ();
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 1; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[2];
    var completeMessage = "All set!  The accoutn associate with " + emailAddress + " has been deactivated"; // Confrim account is deactive
    var cancelMessage = "Per your request, the account associated with " + emailAddress + " has NOT been deactivated."; // Request cancelled
    var invalidMessage = "The email address of " + emailAddress + " is not accociated with a brightwheel account."; // Invalid email
    var confirmationSent = row[7];
    var status = row[6];
     var subject = "Brightwheel Deactivation Request For - " + emailAddress;
    if (confirmationSent != CONFIRMATION_SENT) {  // Prevents sending duplicates
      if (status != null) {return;}
      else{
      if (status = "Complete - Account Deactivated") {MailApp.sendEmail(emailAddress, subject, completeMessage);}
      if (status = "Request Canceled - Account Still Active") {MailApp.sendEmail(emailAddress, subject, cancelMessage);}
      if (status = "Not a Valid Email - Account Not Found") {MailApp.sendEmail(emailAddress, subject, invalidMessage);}
      }
      sheet.getRange(i+1, 8).setValue(CONFIRMATION_SENT); //Cell = index array + 1
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}

1 个答案:

答案 0 :(得分:1)

尝试替换此内容:

if (status != null) {return;}
      else{
      if (status = "Complete - Account Deactivated") {MailApp.sendEmail(emailAddress, subject, completeMessage);}
      if (status = "Request Canceled - Account Still Active") {MailApp.sendEmail(emailAddress, subject, cancelMessage);}
      if (status = "Not a Valid Email - Account Not Found") {MailApp.sendEmail(emailAddress, subject, invalidMessage);}
      }

 switch(data[i][6])
  {
    case 'Complete - Account Deactivated':
      MailApp.sendEmail(emailAddress, subject, completeMessage);
      sheet.getRange(i+1, 8).setValue(CONFIRMATION_SENT);
      break;
    case 'Request Canceled - Account Still Active':
      MailApp.sendEmail(emailAddress, subject, cancelMessage);
      sheet.getRange(i+1, 8).setValue(CONFIRMATION_SENT);
      break;
    case 'Not a Valid Email - Account Not Found':
      MailApp.sendEmail(emailAddress, subject, invalidMessage);
      sheet.getRange(i+1, 8).setValue(CONFIRMATION_SENT);
      break;
    default:
      break;//We don't want to return here we want to break so that we can continue on to any remain lines.
  }

我正在查看您的代码,并注意到我应该包括设置阻止重复电子邮件的列。

这有效:

我刚试过它。

function sendConfirmation()
{
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getDataRange ();
  var data=rg.getValues();
  var s=''
  for (var i=1;i<data.length;i++) 
  {
    var completeMessage = "All set!  The accoutn associate with " + data[i][2] + " has been deactivated";
    var cancelMessage = "Per your request, the account associated with " + data[i][2] + " has NOT been deactivated.";
    var invalidMessage = "The email address of " + data[i][2] + " is not accociated with a brightwheel account.";
    var subject = "Brightwheel Deactivation Request For - " + data[i][2];
    if (!data[i][7]) 
    { 
      switch(data[i][6])
      {
        case 'Status1':
          MailApp.sendEmail(data[i][2], subject, completeMessage);
          //s+=Utilities.formatString('<br />i: %s email: %s subject: %s message: %s',i,data[i][2], subject, completeMessage);
          sh.getRange(i+1, 8).setValue('CONFIRMATION_SENT');
          break;
        case 'Status2':
          MailApp.sendEmail(data[i][2], subject, cancelMessage);
          //s+=Utilities.formatString('<br />i: %s email: %s subject: %s message: %s',i,data[i][2], subject, cancelMessage);
          sh.getRange(i+1, 8).setValue('CONFIRMATION_SENT');
          break;
        case 'Status3':
          MailApp.sendEmail(data[i][2], subject, invalidMessage);
          //s+=Utilities.formatString('<br />i %s email: %s subject: %s message: %s',i,data[i][2], subject, invalidMessage);
          sh.getRange(i+1, 8).setValue('CONFIRMATION_SENT');
          break;
        default:
          break;//Dont return just break and go on to next line.
      }
    }
  }
  //var ui=HtmlService.createHtmlOutput(s);
  //SpreadsheetApp.getUi().showModelessDialog(ui, 'Emails Sent');
}

最大的问题是回归。这只是停止了剧本的轨道。我们只是休息一下,所以我们可以继续下一行。我把调试内容留在那里,为了方便起见,我简化了一些状态语句。

这就是我的电子表格:

enter image description here

这是我的调试屏幕:

enter image description here