繁体   English   中英

Google App Scripts & Google Sheets - 相当于 VLOOKUP/IMPORTRANGE - 使用多个电子表格

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

目标:我想消除数组公式、导入范围和查找。 相反,我想使用 Google App Script 来填充子数据库电子表格中的 Col B,C。 这是因为每次打开工作表时都会出现当前的性能问题,以及在拉取数据时 Google Data Studio 超时的问题。


我有 2 个电子表格。


主数据库(~1,000,000 行)- 100% 手动输入

|        A          |          B          |           C          |

| ID (Manual Input) | Name (Manual Input) | Email (Manual Input) |

子数据库(~10,000 行) - 手册和公式

=ARRAYFORMULA(VLOOKUP(A2:A,IMPORTRANGE("URL","MasterDB!A2:C"),{2,3},FALSE))

(从 Col A 获取 ID,搜索主数据库电子表格,并返回姓名和电子邮件。)

|        A          |        B       |        C        |

| ID (Manual Input) | Name (Formula) | Email (Formula) |

最好的解决方案是什么?

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


function import() {
  //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 运行时,请启用它。
  • 如果这不是您期望的结果,您能否提供示例电子表格? 通过这个,我想修改脚本。

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM