gpt4 book ai didi

Javascript 谷歌电子表格 : how do I find the max value within a group

转载 作者:行者123 更新时间:2023-11-30 17:30:39 25 4
gpt4 key购买 nike

在谷歌电子表格中,我有一列重复的年份行和一列值。我想编写一个脚本来查找每年的最大值。我已经使用下面的脚本找到了所有年份的最大值,但是很难找到每年的最大值并将其记录到新列中。

function findmax(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var column = 9;
var colArray = sheet.getRange(2, column, sheet.getLastRow()).getValues();
var maxi = Math.max.apply(Math, colArray)
Logger.log(maxi)

有什么想法吗?

最佳答案

function maxGroupBy() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var yearCol = 1; // <-assumption that the two column are not adjacent.
var column = 9;

var yearArray = sheet.getRange(1, yearCol, sheet.getLastRow()).getValues();
var colArray = sheet.getRange(1, column, sheet.getLastRow()).getValues();

var group = {};
for (var i = 0; i< yearArray.length; i++) {
group[yearArray[i]] = Math.max((group[yearArray[i]] || colArray[i]), colArray[i]);
}
for (key in group) {
Logger.log(key + "::" + group[key]);
}
}

关于Javascript 谷歌电子表格 : how do I find the max value within a group,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23135072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com