Google 应用脚本和 Google 表格 - 等同于 VLOOKUP/IMPORTRANGE - 使用多个电子表格

Google App Scripts & Google Sheets - Equivalent of VLOOKUP/IMPORTRANGE - Using multiple spreadsheets

Objective: 我想消除使用公式(数组公式、importranges 和 vlookups)。相反,我想使用 Google App Script 来填充子数据库 Spreadsheet 中的列。 这是因为每次打开 sheet 时都会出现性能问题,并且 Google Data Studio 在提取数据时会超时。


我有2张sheets.

#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(姓名和电子邮件)

A (Manual Input) B (Auto-Populate) C (Auto-Populate)
1 X543210 Sarah Smith SarahSmith@examplecom
2 X123456 John Doe JohnDoe@examplecom


最好的解决方案是什么?

到目前为止,这是我想出的...

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.

}

感谢您的任何意见、指导和帮助:)

修改点:

  • 在您的脚本中,需要添加const mDB = ssh.getRange("A2:A").getValues;const cDB = dsh.getRange("A2:A").getValues;才能执行getValues的功能。
  • 好像函数名的import是保留名。所以请修改函数名。当使用 V8 运行时。

当这些点反映到脚本中,就变成了下面这样

修改后的脚本:

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);
}
  • 为了实现您的目标,我修改了 处的示例脚本。

注:

  • 此脚本与 V8 运行时一起使用。因此,当您禁用 V8 运行时时,请启用它。
  • 如果这不是您期望的结果,您能提供示例电子表格吗?据此,我想修改脚本。

参考文献:

已添加:

关于你新提出的3个问题,我回答如下。

[Question #1] I assume o is just a placeholder and can be any letter I want. Is that true? or does the letter o have some significant?

是的。您可以使用除 o 之外的其他变量名称。在这个脚本中,o的初始值为{}Ref

[Question #2] What do the 3 dots do? [a, ...bc] ?

... 是传播语法。 Ref

[Question #3] How would I skip a returned column? Currently it returns b,c. How would I return c,d instead?

在这种情况下,示例脚本如下。

function Q69818704_myFunction() {
  const sss = SpreadsheetApp.openById('ABC');
  const ssh = sss.getSheetByName("MasterDB");
  const mDB = ssh.getRange("A2:D" + ssh.getLastRow()).getValues();

  const dss = SpreadsheetApp.openById('XYZ');
  const dsh = dss.getSheetByName("ChildDB");
  const cDB = dsh.getRange("A2:A" + dsh.getLastRow()).getValues();

  const obj = mDB.reduce((o, [a,, ...cd]) => ((o[a] = cd), o), {});
  const values = cDB.map(([b]) => obj[b] || ["", ""]);
  dsh.getRange(2, 2, values.length, 2).setValues(values);
}