目標: 我想消除公式(陣列公式、匯入范圍和查找)的使用。相反,我想使用 Google App Script 來填充子資料庫電子表格中的列。這是因為每次打開作業表時都會出現當前的性能問題,以及在拉取資料時 Google Data Studio 超時的問題。
我有 2 個電子表格。
#1 - 主資料庫(~1,000,000 行) - 100% 手動輸入
| A(手動輸入) | B(手動輸入) | C(手動輸入) | |
|---|---|---|---|
| 1 | X123456 | 約翰·多伊 | JohnDoe@examplecom |
| 2 | X987654 | 簡·史密斯 | JaneSmith@examplecom |
| 3 | X543210 | 莎拉·史密斯 | 莎拉史密斯@examplecom |
#2 - 子資料庫(~10,000 行)
其目的:在 Col A 中手動輸入 ID,公式將自動填充 Col B:C(姓名和電子郵件)
- 這是使用 GAS 而不是當前公式的預期結果。
| A(手動輸入) | B(自動填充) | C(自動填充) | |
|---|---|---|---|
| 1 | X543210 | 莎拉·史密斯 | 莎拉史密斯@examplecom |
| 2 | X123456 | 約翰·多伊 | JohnDoe@examplecom |
- Col A - 手動輸入 ID。
- Col B1 包含從 Col A 獲取 ID、搜索主資料庫電子表格并回傳姓名和電子郵件的公式
=ARRAYFORMULA(VLOOKUP(A2:A,IMPORTRANGE("URL","MasterDB!A2:C"),{2,3},FALSE))。
最好的解決方案是什么?
這是我想出的,到目前為止......
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.
}
感謝您的任何意見、指導和幫助:)
uj5u.com熱心網友回復:
改裝要點:
- 在您的腳本中,
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個問題,我回答如下。
[問題 #1] 我認為 o 只是一個占位符,可以是我想要的任何字母。真的嗎?或者字母o有什么意義嗎?
是的。您可以使用除o. 在這個腳本中,初始值o是{}。參考
[問題#2] 3 個點有什么作用?[a, ... bc] ?
...是傳播語法。參考
[問題 #3] 如何跳過回傳的列?目前它回傳 b,c。我將如何回傳 c,d 呢?
在這種情況下,示例腳本如下。
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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/348861.html
標籤:javascript 谷歌应用程序脚本 谷歌表格 谷歌表格公式 查询
