【发布时间】:2021-11-03 01:30:49
【问题描述】:
目标: 我想避免使用公式(数组公式、importranges 和 vlookups)。相反,我想使用 Google App Script 来填充子数据库电子表格中的列。 这是因为当前我每次打开工作表时都会出现性能问题,以及 Google Data Studio 在提取数据时超时的问题。
我有 2 个电子表格。
#1 - 主数据库(~1,000,000 行)- 100% 手动输入
| A (Manual Input) | B (Manual Input) | C (Manual Input) | |
|---|---|---|---|
| 1 | X123456 | John Doe | JohnDoe@examplecom |
| 2 | X987654 | Jane Smith | JaneSmith@examplecom |
| 3 | X543210 | Sarah Smith | SarahSmith@examplecom |
#2 - 子数据库(约 10,000 行)
其用途:在 Col A 中手动输入 ID,公式将自动填充 Col B:C(姓名和电子邮件)
- 这是使用 GAS 而不是当前公式的预期结果。
| A (Manual Input) | B (Auto-Populate) | C (Auto-Populate) | |
|---|---|---|---|
| 1 | X543210 | Sarah Smith | SarahSmith@examplecom |
| 2 | X123456 | John Doe | JohnDoe@examplecom |
- Col A - 手动输入 ID。
- Col B1 包含公式
=ARRAYFORMULA(VLOOKUP(A2:A,IMPORTRANGE("URL","MasterDB!A2:C"),{2,3},FALSE))从 Col A 获取 ID,搜索主数据库电子表格,并返回名称和电子邮件。
什么是最好的解决方案?
这是我想出的,到目前为止......
function myFunction() {
//Source Info.
const sss = SpreadsheetApp.openById('ABC');
const ssh = sss.getSheetByName("MasterDB");
const mDB = ssh.getRange("A2:A").getValues; //Get's ID's from Master Spreadsheet
//Destination Info.
const dss = SpreadsheetApp.openById('XYZ');
const dsh = dss.getSheetByName("ChildDB");
const cDB = dsh.getRange("A2:A").getValues; //Get's ID's from Child Spreadsheet
[Some Code Here]
- Return Col B,C from Master Sheet, if Col A matches in both Master & Child Sheet.
}
感谢您的任何意见、指导和帮助 :)
【问题讨论】:
-
"A2:A"这样的范围会在数据末尾生成大量空值,从而导致问题。替换为getRange(row,col,numrow,numcols)
标签: javascript google-apps-script google-sheets google-sheets-formula vlookup