【发布时间】:2021-12-17 07:43:04
【问题描述】:
是否有使用类似于 VLOOKUP 的 Google App Scripts 的选项?
我希望找到一个解决方案,将电子表格 2 上的 ??? 替换为电子表格 1 上的电子邮件,使用 GAS 触发功能在预定的基础上发送。
目前我正在使用公式,但它们会降低工作表的性能。我不需要他们每 24 小时多次获取数据,因此我想使用 GAS 来代替触发器。
- IMPORTRANGE & QUERY 将电子表格 #1(ID、电子邮件 1、电子邮件 2)导入电子表格 #2
=QUERY(IMPORTRANGE(spreadsheet_url, A1:E), "SELECT Col1,Col4,Col5 WHERE A Col1 is not null", 1) - 然后在电子表格 #2 上使用 ARRAYFORMULA 和 VLOOKUP 填充所有电子邮件。
=ARRAYFORMULA(VLOOKUP(Sheet1!A2:A,ImportedData!A2:C},{2,3},0)
这是我的 2 个电子表格的样子...
电子表格 #1(包含约 200 万个单元格)
| ID | Something | Something | Email1 | Email2 |
|---|---|---|---|---|
| 111111 | * | * | Bob@hotmail.com | Bob@gmail.com |
| 222222 | * | * | James@gmail.com | James@yahoo.com |
电子表格 #2(包含约 20 万个单元格)
| ID | Email 1 | Email 2 |
|---|---|---|
| 111111 | ??? | ??? |
| 222222 | ??? | ??? |
更新
我问了一个类似的问题HERE 并得到了答复。
答案是:
function myFunction() {
const sss = SpreadsheetApp.openById('ABC');
const ssh = sss.getSheetByName("MasterDB");
const mDB = ssh.getRange("A2:C" + ssh.getLastRow()).getValues(); //Get's ID's from Master Spreadsheet
const dss = SpreadsheetApp.openById('XYZ');
const dsh = dss.getSheetByName("ChildDB");
const cDB = dsh.getRange("A2:A" + dsh.getLastRow()).getValues(); //Get's ID's from Child Spreadsheet
// Create an object for searching the values of column "A".
const obj = mDB.reduce((o, [a, ...bc]) => ((o[a] = bc), o), {});
// Create an array for putting to the Spreadsheet.
const values = cDB.map(([b]) => obj[b] || ["", ""]);
// Put the array to the Spreadsheet.
dsh.getRange(2, 2, values.length, 2).setValues(values);
}
我的新问题(针对此线程)
如何修改脚本以排除 B 列和 C 列?
【问题讨论】:
-
我认为您的目标可能可以使用 Google Apps 脚本实现。但是,在您的问题中,我也认为,当您提供您期望作为图像的示例输入和输出情况时,它将帮助用户思考解决方案。
-
你有没有尝试过?
-
感谢您回复并添加更多信息。正如TheMaster的评论,我也认为在你的情况下stackoverflow.com/a/69819156的示例脚本如下,你的目标也许可以实现。从
const mDB = ssh.getRange("A2:C" + ssh.getLastRow()).getValues();到const mDB = ssh.getRange("A2:E" + ssh.getLastRow()).getValues(),从const obj = mDB.reduce((o, [a, ...bc]) => ((o[a] = bc), o), {})到const obj = mDB.reduce((o, [a,,,...de]) => ((o[a] = de), o), {})。这个怎么样?如果这不是直接的解决方案,我深表歉意。 -
@JamesReed68 新问题还可以,但要清楚地区分。最重要的是,展示你尝试过的东西。 SO 不是免费的代码编写服务。不要将社区成员视为免费的代码编写者。
标签: google-apps-script google-sheets