簡體   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