The important part is that sometimes the code runs without an issue, and sometimes it fails with this exception. I run this code every 1 minute (time-driven trigger) because it tries to fill in gaps in the data sheet.
If it always failed - it would mean my code is wrong, but it works fine some of the time, and some of the time it fails. Also - this only started lately. The same code ran many times without an issue before. My only guess is that my spreadsheet is large with a lot of data (9000 rows), and this causes an issue somehow?
function fill_in_agent_group_names() {
var main_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("income");
var last_row = main_sheet.getLastRow();
var last_row_str = last_row.toString();
console.log("Last row: " + last_row_str);
while(main_sheet.getRange(last_row, AGENT_GROUP_COL).getValue()===""){
var income = main_sheet.getRange(last_row, INCOME_COL).getValue();
var expenses = main_sheet.getRange(last_row, EXPENSES_COL).getValue();
var currency = main_sheet.getRange(last_row, CURRENCY_COL).getValue();
if(income || expenses){
main_sheet.getRange(last_row, AGENT_GROUP_COL).setFormula("=IFERROR(VLOOKUP($Q" + last_row_str + ", 'info'!A:B, 2, FALSE), " + '"")');
main_sheet.getRange(last_row, AGENT_SN_COL).setFormula("=IFERROR(VLOOKUP($Q" + last_row_str + ", 'info'!A:C, 3, FALSE), " + '"")');
if (currency !== "ILS"){
var now = new Date();
main_sheet.getRange(last_row, ILS_COL).setFormula('=INDEX(GOOGLEFINANCE("CURRENCY:' + currency + 'ILS", "price", DATE(' + now.getFullYear().toString() + ',' + (now.getMonth()+1).toString() + ',' + now.getDate().toString() + ')), 2, 2) * E' + last_row_str);
}
else{
main_sheet.getRange(last_row, ILS_COL).setValue(income);
}
var epoch = main_sheet.getRange(last_row, ORIGINAL_DATE_TIME_COL).getValue();
if(Number.isInteger(epoch)){
var timestamp = new Date(parseInt(epoch));
main_sheet.getRange(last_row, ORIGINAL_DATE_TIME_COL).setValue(timestamp);
main_sheet.getRange(last_row, ORIGINAL_DATE_TIME_COL).setNumberFormat("dd/MM/yyyy hh:mm:ss");
}
}
last_row--;
}
}