2013-07-15 27 views
0

我被困在某些代碼中的某處,需要一些專家幫助。我想在表單「表單響應」中引用列「E」,它將返回一個人的姓名。相同的名稱可以在表格「電子郵件」的欄「A」中找到。在表格「B」欄中,「電子郵件」將是我想要發送數據的電子郵件地址。我被困在如何產生這個電子郵件地址。這是我到目前爲止...使用Google Apps腳本將電子郵件發送到其他工作表上列出的特定地址

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的道具來幫助我的數組。唉,你能提供的任何幫助都會很棒!

回答

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

有更多優雅的方式來做到這一點,我敢肯定,但這應該適合你。

+0

你搖滾!我非常感謝你的幫助。 – dericcain

相關問題