我在一列中有多个值,我想返回出现5次的值。样本数据:
Date Column
Row1: July 1, 2019
Row2: July 1, 2019
Row3: July 1, 2019
Row4: July 1, 2019
Row5: July 1, 2019
Row6: July 5, 2019
Row7: July 5, 2019
Row8: July 5, 2019
Row9: July 5, 2019
Row10: July 5, 2019
Row11: July 10, 2019
Row12: July 12, 2019
我已经尝试了从该站点获得的一些解决方案。一位(Amit Agarwal)几乎回答了我的问题,不幸的是,他的代码仅显示了出现次数最多的值。使用我的代码和(大部分)他的代码,结果如下:
function countDate() {
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("Test_Data");
var dateRg = ws.getRange(1, 1, ws.getLastRow(), 1).getDisplayValues();
var data = {}
for (var i = 0; i < dateRg.length; i++) {
data[dateRg[i]] = data[dateRg[i]] || 0;
data[dateRg[i]]++;
}
var max = Object.keys(data).reduce(function(a, b) {
return data[a] > data[b] ? a : b
});
ws.getRange(2, 4).setValue(max);
}
我想从列表或列(Google工作表)中获得5次出现的所有值。老实说,我对此一无所知。我是编程方面的新手。我已经在这里提出了这个问题,但是没有人再回答了,所以我最终再次提出了这个问题,我真的需要一个解决方案。我谦虚地向任何人寻求帮助。
答案 0 :(得分:2)
你在这里
var Date_Field = [
'July 1, 2019',
'July 1, 2019',
'July 1, 2019',
'July 1, 2019',
'July 1, 2019',
'July 5, 2019',
'July 5, 2019',
'July 5, 2019',
'July 5, 2019',
'July 5, 2019',
'July 10, 2019',
'July 12, 2019'
];
var CheckLimitReached = function (T) {
var records = {};
T.forEach(function (x) { records[x] = (records[x] || 0) + 1; });
var limit_reached = Object.keys(records).filter(function (R) {
return records[R] >= 5;
});
return limit_reached;
};
console.log(CheckLimitReached(Date_Field));