使用Google Apps脚本将电子邮件发送到另一张表中列出的特定地址

时间:2013-07-15 19:41:56

标签: google-apps-script google-sheets

我遇到了一些代码,需要一些专家帮助。我想在表单“表单响应”中引用列“E”,它将返回一个人的姓名。在“电子邮件”表的“A”栏中可以找到相同的名称。在“电子邮件”表单的“B”列中,将是我要向其发送数据的电子邮件地址。我被困在如何生成这个电子邮件地址。这是我到目前为止所拥有的......

function emailData(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var responses = ss.getSheetByName("Form Responses");
var lastRow = responses.getLastRow();
var values = responses.getRange("A"+(lastRow)+":AK"+(lastRow)).getValues();// get the range and values in one step
var headers = responses.getRange("A1:AK1").getValues();// do the same for headers
var recipient = responses.getRange("E"+(lastRow)).getValues();

var emailSheet = ss.getSheetByName("Email");
var names = emailSheet.getRange("A2:A20").getValues();
var emails = emailSheet.getRange("B2:B20").getValues();


var subject = "Capacity Campaign Form";
var message = composeMessage(headers,values);// call the function with 2 arrays as arguments

Logger.log(message);// check the result and then send the email with message as text body
MailApp.sendEmail(recipient,subject,message);
}

function composeMessage(headers,values){
var message = 'Here is the data from the form submission:'
for(var c=0;c<values[0].length;++c){
message+='\n'+headers[0][c]+' : '+values[0][c]
}
return message;
}

我必须给@Serge提供道具来帮助我使用数组。唉,你能提供的任何帮助都很棒!

1 个答案:

答案 0 :(得分:0)

您有两个二维数组中需要搜索的名称和电子邮件地址。每个阵列都是行数组。一个简单的搜索就是:

var email = ''; // If we don't find a match, we'll fail the send
for (var row=0; row < names.length; row++) {
  if (names[row][0] == recipient) {
    email = emails[row][0];
    break; // end the search, we're done
  }
}

...
MailApp.sendEmail(email,subject,message);

有更优雅的方法可以做到这一点,我敢肯定,但这应该适合你。