I have a custom function that inserts a new row under the last non-empty row in a sheet and copies a specific range to this new row. This functions works fine when I trigger it with a custom menu item.
However, when I add this custom function to the onOpen(e) trigger the script does not work. I troubleshooted and added an alert in my function - the alert triggers so therefore the function is read in the right place.
Please advise how to have my script work onOpen?
Here's my code for my custom function:
function onOpen(e) {
autoAddRow();
}
function autoAddRow() {
var updateFrequency = 3;
var lRow = getLastRow('A');
var lDate = all.getRange(lRow,1).getValue();
var today = new Date();
Date.prototype.addDays = function(days) {
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
}
var newDate = lDate.addDays(updateFrequency);
if (today >= newDate) {
var lCol = all.getLastColumn();
var src = all.getRange(3,1,1,6);
var dest = all.getRange(lRow+1,1,1,6);
if(lRow > 20) {
all.insertRowsAfter(lRow, 1);
}
src.copyTo(dest, {contentsOnly:false});
} else {
return;
}
}