0

我被困在一些代码中,需要一些专家的帮助。我想引用“表单响应”表上的“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 提供道具以帮助我处理阵列。唉,你能提供的任何帮助都会很棒!

4

1 回答 1

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);

There are more elegant ways to do this, I'm sure, but this should work for you.

于 2013-07-15T19:59:00.170 回答