在 Google 作業表中,我有物件(行),這些物件(行)可以通過它們的兩個值的組合來明確識別,id并date存盤在不相鄰的列中。現在我需要檢查此作業表中是否存在這種型別的某個物件,如果存在,則在哪一行。我想要一個這樣的陣列object[<index = row no>][<id> <delimiter> <date>],讓我可以通過以下方式找到并定位其中一個:
sheet = SpreadsheetApp.openById('1234567890').getSheetByName('Entries List');
objectsIds = sheet.getRange(1, 5, 100).getDisplayValues(); // with 5 = column containing ids
objectsDates = sheet.getRange(1, 11, 100).getDisplayValues(); // with 11 = column containing dates
// Create array of objects with joint/combined value of id # date:
// <-------ID---------->#<--date-->
consloe.log(objects[0]); // writes e. g. "4e6aa8-8f709d-a208b38#2022-02-19" which is unambiguously identifiable
sampleIdDate = sampleId "#" sampleDate;
rowOfFirstOccurance = objects.findIndex(e => === sampleIdDate);
實際上分隔符可以省略。在一個屬性/欄位中創建此組合值陣列的最佳方法是什么?
uj5u.com熱心網友回復:
在您的腳本中,如何進行以下修改?
修改后的腳本:
var sheet = SpreadsheetApp.openById('1234567890').getSheetByName('Entries List');
var objectsIds = sheet.getRange(1, 5, 100).getDisplayValues(); // with 5 = column containing ids
var objectsDates = sheet.getRange(1, 11, 100).getDisplayValues(); // with 11 = column containing dates
var objects = objectsIds.map(([v], i) => `${v}#${objectsDates[i][0]}`); // Added
// Create array of objects with joint/combined value of id # date:
// <-------ID---------->#<--date-->
console.log(objects[0]); // writes e. g. "4e6aa8-8f709d-a208b38#2022-02-19" which is unambiguously identifiable
var sampleIdDate = sampleId "#" sampleDate;
var rowOfFirstOccurance = objects.findIndex(e => e === sampleIdDate); // Modified
在您的腳本中,請修改
consloe.log(objects[0]);為console.log(objects[0]);.從
Actually the delimiter can be omitted.,如果要洗掉分隔符,請將var objects = objectsIds.map(([v], i) =>${v}#${objectsDates[i][0]}修改);為var objects = objectsIds.map(([v], i) =>${v}${objectsDates[i][0]});。
筆記:
作為附加資訊,例如,在您的腳本中,您使用了 2
getDisplayValues()種方法。我認為這可以減少到一次如下。這樣,可以稍微降低處理成本。從
var sheet = SpreadsheetApp.openById('1234567890').getSheetByName('Entries List'); var objectsIds = sheet.getRange(1, 5, 100).getDisplayValues(); // with 5 = column containing ids var objectsDates = sheet.getRange(1, 11, 100).getDisplayValues(); // with 11 = column containing dates var objects = objectsIds.map(([v], i) => `${v}#${objectsDates[i][0]}`); // Added到
var sheet = SpreadsheetApp.openById('1234567890').getSheetByName('Entries List'); var values = sheet.getRange(1, 5, 100, 7).getDisplayValues(); var objects = values.map(r => `${r[0]}#${r[6]}`);
參考:
- 地圖()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/431152.html
上一篇:如何使用GoogleApps腳本在Google表格中創建過濾器區域?
下一篇:表格腳本-根據背景顏色清除單元格
