Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果需要,如何编写简单计数脚本_Javascript_Google Apps Script_Google Sheets - Fatal编程技术网

Javascript 如果需要,如何编写简单计数脚本

Javascript 如果需要,如何编写简单计数脚本,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我试图找到这个问题,但是我无法找到其他问题的答案。我有两个条件用于对数据进行排序。一年中的月份,以及排名1、2或3的月份 我需要的是在一个摘要页面上,计算每个月有多少具有特定等级的输入 我希望这一切都是有意义的,我试图尽我所能澄清这一点,我真的被这件事弄得不知所措,没有任何类型的编码/脚本编写经验。 谢谢你的帮助 我使用下面的代码返回数据的日期和等级,并提取月份。 然后,它在if语句中使用这些月份放在摘要页面上。我不知道怎么做,从这里开始,在里面放一个计数公式。就像这里的代码一样,我希望它类似于

我试图找到这个问题,但是我无法找到其他问题的答案。我有两个条件用于对数据进行排序。一年中的月份,以及排名1、2或3的月份

我需要的是在一个摘要页面上,计算每个月有多少具有特定等级的输入

我希望这一切都是有意义的,我试图尽我所能澄清这一点,我真的被这件事弄得不知所措,没有任何类型的编码/脚本编写经验。 谢谢你的帮助

我使用下面的代码返回数据的日期和等级,并提取月份。 然后,它在if语句中使用这些月份放在摘要页面上。我不知道怎么做,从这里开始,在里面放一个计数公式。就像这里的代码一样,我希望它类似于公式if=month=1和tiers(ranks)=1,然后计数(它不能添加,因为如果添加,当页面更新时,它将添加到已经计数的数字)


< Value>(var i=8;i而不是循环中的StValk),应该考虑在变量/s内进行计数,而一旦循环完成,只需要设置值。 建议的代码如下:

for(var i =8;i<=j;i++) { //loops through a data table to see dates and ranks
var dates = oppwon.getRange(i,22).getValue();
var tiers = oppwon.getRange(i,14).getValue();
var month = new Date(dates).getMonth()+1;
var countTiers = {}; //count by tiers of months

countTiers[`m${month}`][`t${tiers}`]++;

for(var i=8;i您可以定义一个函数

function countTiersByMonth ( dataTable, firstline, lastline ) {
    var result = [ [0,0,0],[0,0,0],[0,0,0], [0,0,0],[0,0,0],[0,0,0], [0,0,0],[0,0,0],[0,0,0], [0,0,0],[0,0,0],[0,0,0] ];
    var dates;
    var tiers;
    var month;
    for(i = firstline; i<=lastline; i++) { 
        dates = dataTable.getRange(i,22).getValue();
        tiers = dataTable.getRange(i,14).getValue();
        month = new Date(dates).getMonth();

        switch (tiers){ // we filter by tiers because it seems that you only care about
                        // tiers 1, 2, 3 wheras you care about all the months
            case 1: 
            case 2:
            case 3:
                result[month][tiers-1]++; //+1 for the respective tier
                                          //  of the respective month
                break;      //other tiers are ignored
        };
    };
    return result;
};

我不知道这部分到底在做什么?countTiers[
m${month}
][
t${tiers}
]++;我应该用这些符号中的一些来代替其他符号吗?我似乎无法理解。很抱歉,我对这方面没有太多经验…这个[0,0,0]对于每个变量,具体做什么?我知道第一行,最后一行,这只是定位单元格吗?在哪里进行计数?啊,对不起,我想我应该解释得更清楚一点。[0,0,0]是[#第一层,#第二层,#第三层]每个月。在开始计数之前,我们将它们设置为0。在for循环中,我们遍历数据表中的每一行,每当我们找到第1、2或3层时,我们都会计算该层的计数器,并在最后输出数组。在答案中添加了一个示例。希望对您有所帮助。
function countTiersByMonth ( dataTable, firstline, lastline ) {
    var result = [ [0,0,0],[0,0,0],[0,0,0], [0,0,0],[0,0,0],[0,0,0], [0,0,0],[0,0,0],[0,0,0], [0,0,0],[0,0,0],[0,0,0] ];
    var dates;
    var tiers;
    var month;
    for(i = firstline; i<=lastline; i++) { 
        dates = dataTable.getRange(i,22).getValue();
        tiers = dataTable.getRange(i,14).getValue();
        month = new Date(dates).getMonth();

        switch (tiers){ // we filter by tiers because it seems that you only care about
                        // tiers 1, 2, 3 wheras you care about all the months
            case 1: 
            case 2:
            case 3:
                result[month][tiers-1]++; //+1 for the respective tier
                                          //  of the respective month
                break;      //other tiers are ignored
        };
    };
    return result;
};
tierlist = countTiersByMonth(oppwon, 8, j) // We do the counting here
print(tierlist[4][0]) // arrays start at 0, so May -> [4], Tier 1 -> [0]
print(tierlist[4][1]) // May, Tier 2
print(tierlist[4][2]) // May, Tier 3