将数组内容与预先输入的数据匹配

时间:2016-09-03 22:03:57

标签: google-apps-script

我有一个数组,其中的内容是来自Google表单中的复选框问题的名称列表的子集。我需要通过电子邮件发送名字在数组中的人,我想从硬编码列表(multi-dim array?)。我无法弄清楚如何进行搜索/比较/无论如何。显然我假设使用对象文字,如下面的代码所示:

var formNames = ["Name One", "Name Three"];  // one possibility for example

var objectMatchingNamesToEmails{
   "Name One":"nameone@work.com",
   "Name Two":"nametwo@work.com",
   "Name Three":"namethree@work.com",
};

1 个答案:

答案 0 :(得分:0)

你可以遍历数组:

var arrayOfEmails,arrayOfNames,L,thisEmail,thisName;

arrayOfNames = ["NameOne","NameTwo"];

arrayOfEmails = [];
L = arrayOfNames.length;//The number of names in the array

for (var i = 0;i<L;i++) {
  thisName = arrayOfNames[i];
  thisEmail = objectMatchingNamesToEmails[thisName];
  arrayOfEmails.push(thisEmail);
};

创建一个对象文字:

var objectMatchingNamesToEmails;

objectMatchingNamesToEmails = {
  "NameOne":"exampleOne@gmail.com",
  "NameTwo":"exampleTwo@gmail.com",
  "NameThree":"exampleThree@gmail.com",
};

然后,在您获得名称后,代码可以查找正确的电子邮件:

var userName,userEmail;

userName = code here to get user name;

userEmail = objectMatchingNamesToEmails[userName];

MailApp.sendEmail(userEmail,subject,body);