I have a sheet with a couple of thousand rows and 2 columns. I need to write a script that will increment be row and compare the value in one of the columns to the previous 50 values in the same column and see if it is larger than the maximum value of the 50 previous entries.
I've been trying to use Math.max(), but can't find the correct syntax to use to get that to work on a dynamic range.
when reading / writing in spreadsheets always try to do it at once, avoiding looping over the cells
I am sure there is a better javascript approach but this code will get you the max value within a column
function getMaxInColumn(column) {
var column = 2; // just for testing
var sheet = SpreadsheetApp.getActiveSheet();
var colArray = sheet.getRange(2, column, sheet.getLastRow()).getValues();
var maxInColumn = colArray.sort(function(a,b){return b-a})[0][0];
return maxInColumn;
}
regards, Fausto
var max = Math.max.apply(null, column.getValues());
But the column can't contain any text, only numbers and empty values. Or else you'll need to exclude text from your array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With