如何根据if语句查找单元格值?

时间:2019-07-01 20:06:13

标签: google-apps-script

我正在使用Google表格构建徽章发送程序,到目前为止一切进展顺利。有一个控制页面,一旦标记为“是”,脚本便会运行,然后将电子邮件发送到列出的电子邮件,并插入该页面中的信息,包括收件人的姓名。我希望能够找到向其发送徽章电子邮件的人的姓名,使用该姓名激活工作表,然后在该工作表上的某个单元格上加上日期戳。我在查找名称值时遇到麻烦。

电子邮件和日期戳正在控制页面上工作,但是我在弄清楚确定名称值来打开每个人的个人页面的逻辑上遇到了麻烦。记录器仅显示该范围内的所有名称,而我只希望显示接收电子邮件/徽章的名称。

function sendEmails3() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

 var sheet = ss.getSheetByName("Badge Issuing Central")

  var startRow = 2; // First row of data to process

  // Fetch the range of cells
  var dataRange = sheet.getRange(startRow, 1, sheet.getLastRow()-1, 7);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  data.forEach(function(row,i) {

    var name = row[0]; // First column
    var badge = row[1]; // Second column
    var recomendations = row[2]//third column
    var awarded = row [3] // fourth column
    var email = row [4] // fifth column
    var dateEmailed = row [5] // 6th column



    var emailSent = row[6]; // Seventh column
    if (emailSent != EMAIL_SENT && awarded == 'Yes') { // Prevents sending duplicates

        var body = 'Hi ' + name + '<br><br>' +
          'Congratulations! You have recieved a badge!<br><br>' +
            'Your teammates think you are doing great lately.<br><br>' +
              'You were nominated at least twice by those around you, and you are being awarded:<br><br>' +
                "<em>" + badge + '<br><br>' + 
                  "Make sure you check out your Badge Pathway to see how you're doing!<br><br>" +
                  'Thanks, <br>' +
                    "Jordan, Mark, and Jarrod";





      var subject = 'Congratulations! You have earned a badge!';
       GmailApp.sendEmail(email, subject, "", { htmlBody: body } );
      sheet.getRange(startRow + i, 7).setValue(EMAIL_SENT);
      sheet.getRange(startRow + i, 6).setValue(d);


      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
    if (emailSent != EMAIL_SENT && awarded == 'Yes') {


      var nameTab = sheet.getRange(2,1,sheet.getLastRow()-1,1).getValues(); 
      Logger.log(nameTab);}

  });}


1 个答案:

答案 0 :(得分:0)

尝试一下:

function sendEmails3() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName("Badge Issuing Central")  
  var data=sh.getRange(2, 1, sh.getLastRow()-1, 7).getValues();
  for(var i=0;i<data.length;i++) {
    var name=data[i][0]; // First column
    var badge=data[i][1]; // Second column
    var recomendations=data[i][2]//third column
    var awarded=data[i][3] // fourth column
    var email=data[i][4] // fifth column
    var dateEmailed=data[i][5] // 6th column
    var emailSent = data[i][6]; // Seventh column
    if(emailSent != "EMAIL_SENT" && awarded=='Yes') { 
      var body=Utilities.formatString('Hi %s <br><br>Congratulations! You have recieved a badge!<br><br>Your teammates think you are doing great lately.<br><br>You were nominated at least twice by those around you, and you are being awarded:<br><br><em> %s<br><br>Make sure you check out your Badge Pathway to see how you\'re doing!<br><br>Thanks, <br>Jordan, Mark, and Jarrod', name,badge);
      var subject = 'Congratulations! You have earned a badge!';
      GmailApp.sendEmail(email, subject, "", { htmlBody: body } );
      sh.getRange(i + 2, 7).setValue("EMAIL_SENT");
      sh.getRange(i + 2, 6).setValue(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MMddyyyy HH:mm:ss"));
    }    
  }
}