In column Q start at row 15 is dynamic cell which goes down the column and contains a %-Nr. Next to it in column R i want to show the recorded max value per row. During my research i found here this code snipped which is where i was looking for.
function recordMax() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B1");
var values = range.getValues()[0];
range.setValues([[values[0], Math.max(values[0], values[1])]]);
}
Now i tried to fit this to my sheet and changed it to onEdit
for autoupdate.
Also i wanted to change the Range "A1:B1" to my Q:R start at row 15.
This is how far i came and didn't work
var mainWsName = "WATCHLIST AKTIEN";
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(mainWsName);
function onEdit(e) {
var activeCell = e.range; // Here i run into the Error - Cannot read property 'range'
var r = activeCell.getRow();
var c = activeCell.getColumn();
var wsName = activeCell.getSheet().getName();
if(wsName == mainWsName && c == 18 && r > 14){ // start in Q15 Nr. to recordMax. show Max in R15
var range = ws.getRange(r, 18,1,2);
var values = range.getValues()[0];
range.setValues([[values[0], Math.max(values[0], values[1])]]);
}
}
Also it should set back to 0 when cell B2 is empty
I guess i would implement another IF
something like this
if(wsName == mainWsName && ws.getRange(r,2).getValue()==''){
ws.getRange(r, 18,1,2).clearContent();
onEdit(e)
is designed to run when you edit a cell. But if you run it from the Apps Script Editor then you will get the error. This answer in StackOverflow provides a more detailed, easy to understand explanation: TypeError: Cannot read property “range” from undefined.function recordMax()
as installable trigger. But my knowledge about scripts is still to basic. I carry on reading thru the ClassSheet. And poste a update when i come closer.onEdit()
.