Skip to content

Instantly share code, notes, and snippets.

@y56
Created July 23, 2020 10:07
Show Gist options
  • Save y56/f2dcd7913370bccbb31f6a9604d94d88 to your computer and use it in GitHub Desktop.
Save y56/f2dcd7913370bccbb31f6a9604d94d88 to your computer and use it in GitHub Desktop.
get the max in g sheet
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var col_num_of_id = 0;
var col_num_of_val = 1;
var pre_id = -1;
var my_max = -1;
var cur_id = -1;
var cur_val = -1;
var row_index = -1
// sheet.appendRow(['xxxxxxx']);
for (var i = 1; i < data.length; i++) { // 0 is for column name
cur_id = data[i][col_num_of_id];
cur_val = data[i][col_num_of_val];
// sheet.appendRow([pre_id,cur_id,cur_val]) // OK
if (pre_id == cur_id) { // still the same id, update the max
if (cur_val > my_max) {
my_max = cur_val;
row_index = i;
}
} else {
// append previous result
// sheet.appendRow([i,cur_val,row_index,pre_id,cur_id,my_max])
if (row_index!=-1) { // don't append when init
sheet.appendRow(data[row_index]);
}
// update id
my_max = -1;
}
pre_id = cur_id;
}
sheet.appendRow(data[row_index]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment