我有 2 張“客戶(下面的表 1)”和“顏色描述(下面的表 2)”。我想使用 GAS 執行以下操作:
- onEdit 我想比較 2 個范圍然后...
- 查找“Client Sheet - Col2”與“Color Description Sheet - Col1”匹配的每個實體
- 獲取實體所在單元格的地址
- 在“客戶表 - Col3(單元格地址)”中設定該值
所需的輸出如下所示: [John | 紅色 | 顏色說明 A2] 或 [Beth | 粉紅色| 顏色說明A7]
e 由電子表格上的多項觸發
| 客戶 | 顏色 | 單元地址 |
|---|---|---|
| 喬恩 | 紅色的 | |
| 貝絲 | 粉色的 | |
| 湯姆 | 藍色 | |
| Cj | 紅色的 | |
| 奧馬爾 | 綠色 | |
| 麗莎 | 紫色的 |
| 顏色 | 描述 |
|---|---|
| 紅色的 | 關于紅色的一些事 |
| 綠 | 關于綠色的一些事 |
| 藍色 | 關于藍色的一些事 |
| 紫色的 | 關于紫色的一些事 |
| 白色的 | 關于白色的一些事 |
| 粉色的 | 關于粉紅色的一些事 |
我希望這是有道理的。TIA 為您提供幫助。
uj5u.com熱心網友回復:
我相信你的目標如下。
- 您想使用 Google Apps Script 在您的問題中實作 1 到 4 的程序。
示例腳本:
在此示例腳本中,根據您的問題,使用了Client和的作業表名稱Color Description。關于這一點,請根據您的實際情況進行修改。
function myFunction() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const client = ss.getSheetByName("Client");
const colorDescription = ss.getSheetByName("Color Description");
const obj = colorDescription.getRange("A2:B" colorDescription.getLastRow()).getValues().reduce((o, [a, b]) => (o[a.toLowerCase()] = b, o), {});
const values = client.getRange("B2:B" client.getLastRow()).getValues().map(([b]) => [obj[b.toLowerCase()] || ""]);
client.getRange(2, 3, values.length, 1).setValues(values);
}
- 運行此腳本時,通過搜索 的值,
Color Description將作業表“B”列的值放入作業表的“C”列。ClientColor
筆記:
- 在此示例腳本中,它假設每個作業表的第一行是您問題中示例作業表的標題行。
- 我不明白有關
onEdit的onEdit I want to compare 2 ranges then...。在我提議的腳本中,腳本可以通過腳本編輯器、按鈕、自定義選單等來運行。但是如果你想使用OnEdit運行腳本,你能提供執行腳本的條件嗎?通過這個,我想修改它。
參考:
- 降低()
- 地圖()
uj5u.com熱心網友回復:
這就是我想出的。對不起,如果這有點笨重,因為我是新手。
function myFunction() {
var clientColors = SpreadsheetApp.getActiveSpreadsheet();
var clientSheet = clientColors.getSheetByName("Client");
var colorSheet = clientColors.getSheetByName("Color Description");
var lastRownumberClient = clientSheet.getLastRow();
var lastRownumberColor = colorSheet.getLastRow();
var clientSheetVals = clientSheet.getRange(2,2,lastRownumberClient-1,1).getValues();//-1 to remove header row
var colorSheetVals = colorSheet.getRange(2,1,lastRownumberColor-1,1).getValues();//-1 to remove header row
var colorColA = clientSheetVals.map(function(row){//This maps out ColB in the client sheet and returns the matches from ColA in the color sheet
var theItem = row[0];
var lookupColorRangeValues = colorSheetVals.map(function(row){return row[0];});
var index = lookupColorRangeValues.indexOf(theItem) 2;
return ['A' [index]]});//'A' concatenates the the column letter to the returned index number
clientSheet.getRange(2,3,clientSheet.getLastRow()-1,1).setValues(colorColA);//Sets the returned values in ColC of the client sheet -1 to remove header row
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/345202.html
標籤:谷歌应用程序脚本 谷歌表格 谷歌表格公式 数组公式 谷歌查询语言
