使用Google Apps脚本返回多次出现的值 [英] Return the values that occurred multiple times using google apps script

查看:54
本文介绍了使用Google Apps脚本返回多次出现的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一列中有多个值,我希望返回出现5次的值.样本数据:

I have multiple values in a column and I wan to return the values which occurred 5 times. sample data:

      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)几乎回答了我的问题,不幸的是,他的代码仅显示了出现次数最多的值.使用我的代码和(大部分)他的代码,结果如下:

I have tried some of the solutions I got from this site. One (Amit Agarwal) almost answered my question, unfortunately, his code was only to display the values with the maximum occurrence. Using my code and (mostly) his code, this is the result:

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次出现的所有值.老实说,我对此一无所知.我是编程方面的新手.我已经在这里提出了这个问题,但是没有人再回答了,所以我最终再次提出了这个问题,我真的需要一个解决方案.我谦虚地向任何人寻求帮助.

I want to get all the values that occurred 5 times from my list or column (google sheet). I honestly have no knowledge on how to do it. I'm so newbie in programming. I asked this question here already but no one is answering anymore, so I ended up asking it again and I really need a solution soon. I humbly ask anyone for help.

推荐答案

去这里

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));

这篇关于使用Google Apps脚本返回多次出现的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆