SpreadsheetApp如何從陣列中回傳唯一值
在這篇文章中如何計算重復值并在 sheetname('test2') Range ('B2:B7') 中顯示主題在這里我的谷歌表 https://docs.google.com/spreadsheets/d/1HN0XCLrEzlRkInIv6xFnhCFnhZabu7Y-Tz1dvYvsGQM/edit ?usp=分享
uj5u.com熱心網友回復:
在您的情況下,以下示例腳本怎么樣?
示例腳本:
function myFunction() {
const srcSheetName = "test"; // Please set the source sheet name.
const dstSheetName = "test2"; // Please set the destination sheet name.
// Retrieve source and destination sheets.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const [srcSheet, dstSheet] = [srcSheetName, dstSheetName].map(s => ss.getSheetByName(s));
// Retrieve source values and create an object for putting to the destination sheet.
const srcValues = srcSheet.getRange("A2:A" srcSheet.getLastRow()).getValues();
const obj = srcValues.reduce((o, [a]) => (o[a] = o[a] ? o[a] 1 : 1, o), {});
// Retrieve the values of column "A" from the destination sheet and create an array for putting to Spreadsheet.
const dstRange = dstSheet.getRange("A2:A" dstSheet.getLastRow());
const dstValues = dstRange.getDisplayValues().map(([a]) => [obj[a] || 0]);
// Put the result values to the column "B" of the destination sheet.
dstRange.offset(0, 1).setValues(dstValues);
}
- 從您提供的電子表格中,樣本表名稱為
test和test2。請根據您的實際情況進行修改。 - 運行此腳本時,將從源作業表中檢索值并計算每個值的計數。并且,結果值被放入目標作業表的“B”列。
筆記:
- 此示例腳本適用于您提供的電子表格。當您更改電子表格的結構時,此腳本可能無法使用。請注意這一點。
參考:
- 減少()
- 地圖()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/477109.html
