如何修复Google Apps脚本电子邮件查找中的空字段

时间:2019-07-02 10:37:30

标签: google-apps-script

我正在建立电子表格轮换系统。我在网上找到了一些脚本,这些脚本似乎可以满足我的大部分需求here

这是我正在使用的工作表(带有伪数据)-here

但是我想将其调整为与格式和布局配合使用时,我发现它不适用于任何空白单元格。

我尝试使用if语句,但它不喜欢它-语法错误(来自“ e-mailaddress =“部分。

 emailAddress=if(getEmailFromName(row[1])!="") {getEmailFromName(row[1]) + ",";} else{"";}

这是我用来发送电子邮件的当前代码。具有查找电子邮件地址的单独功能。

        // Send an email to the first person

      emailAddress=getEmailFromName(row[1]) + "," +

                      getEmailFromName(row[2]) + "," +

                      getEmailFromName(row[3]) + "," +

                      getEmailFromName(row[4]);


        MailApp.sendEmail(emailAddress, subject, message);



  // to use this function, don’t put anything in the first column (A) or row (1).

  // Put the name (i.e. the key, or what we’re looking for) in column B.

  // Put what we want to return in column C. 



  var columnToSearch = 1; //column B



  // Set the active sheet to our email lookup

  var ss1 = SpreadsheetApp.getActiveSpreadsheet();

  var sh1 = ss1.getSheetByName("EmailContactList")

  ss1.setActiveSheet(sh1);



  var data = SpreadsheetApp.getActiveSheet().getDataRange().getValues();



  var line = -1;

  for( var i = 0; i < data.length; i++ ) {

    if( data[i][columnToSearch] == sKey ) {

      line = i;

      break;

    }

  }



  if( line != -1 ) {

    //do what you want with the data on "line"

    return data[line][2]; //value on column C of the matched line

  } else {

  return "";

  // if criteria is not found

  }

}

我希望我的轮换不必在第一列中填满,如果它是空的,则跳过该列,仅发送到找到匹配项的电子邮件地址。

1 个答案:

答案 0 :(得分:0)

假设电子邮件地址在第1至4列中,其中一些可能为空白:

for (var x = 1; x < 5; x++) { // 5 because emails are till col4

    var emailAddress = []; // Start by collecting the non-blank emails in an array

    if (getEmailFromName(row[1]) != "") {

      emailAddress.push(getEmailFromName(row[x]))

    }

  }

  emailAddress = emailAddress.join();  // Join the array to get a comma separated string

  MailApp.sendEmail(emailAddress, subject, message);