将名称数组转换为电子邮件地址

时间:2018-09-21 17:25:59

标签: google-apps-script google-sheets gmail

我有一个名字数组。我正在使用GmailApp发送电子邮件,但是在提取名称数组和添加域名时遇到了麻烦。例如

  

名称=约翰
  domain = @ gmail.com
  返回john@gmail.com

var ss = SpreadsheetApp.getActiveSpreadsheet();  
var emails = []; 
var contacts = ss.getRange('A1:A7').getValues().toString();
 for(var i in contacts){
   var conc = [contacts[i] + "@gmail.com"].concat();
   emails = emails.concat(conc);
   return(emails);
 }

1 个答案:

答案 0 :(得分:0)

Apps Script有几种有趣的Array方法。即,Array#map在这里有用:

const contacts = ss.getRange('A1:A7').getValues(); // 2D array
const emails = contacts.map(function (nameRow) { return nameRow[0] + "@gmail.com"; });
// Do stuff with the array of emails

如果您的域也以相同形状和大小但不连续的数组排列:

const contacts = ss.getRange('A1:A7').getValues();
const domains = ss.getRange('ZZ1:ZZ7').getValues();
const emails = contacts.map(function (nameRow, i) { return nameRow[0] + domains[i][0]; });
相关问题