簡體   English   中英

使用Google Apps腳本將電子郵件發送到另一個工作表中列出的特定地址

[英]Send email to specific address listed on another sheet using Google Apps Script

我在某些代碼中停留在某個時刻,需要一些專家幫助。 我想在表格“ Form Responses”上引用“ 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提供道具以幫助我處理數組。 las,您所能提供的任何幫助都將非常棒!

您已經獲得了需要搜索的名稱和電子郵件地址,它們是二維二維數組。 這些數組中的每一個都是行的數組。 一個簡單的搜索將是這樣的:

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

我敢肯定,還有更優雅的方法可以做到這一點,但這應該對您有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM