Each time you call getValues
and setValues
in your script there is a considerable overhead cost involved and slows your script down. (Google app script timeout ~ 5 minutes?) I modified your above script to make that 1 call for getValues
and 1 Call to setValues
. The code applies all the replacement to the array in memory before pasting your modified timetable back to the sheet.
function runReplaceInSheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("StudentTimetableEntry");
// get the current data range values as an array
// Fewer calls to access the sheet -> lower overhead
var values = sheet.getDataRange().getValues();
// Replace Subject Names
replaceInSheet(values, /\d\dART\d\d/g, "Art");
replaceInSheet(values, /\d\dCCL\d\d/g, "Communication & Culture");
replaceInSheet(values, /\d\dDLT\d\d/g, "Digital Technology");
replaceInSheet(values, /\d\dDRA\d\d/g, "Drama");
// Replace Staff Names
replaceInSheet(values, 'TED', 'Tahlee Edward');
replaceInSheet(values, 'TLL', 'Tyrone LLoyd');
replaceInSheet(values, 'TMA', 'Timothy Mahone');
replaceInSheet(values, 'TQU', 'Tom Quebec');
// Write all updated values to the sheet, at once
sheet.getDataRange().setValues(values);
}
function replaceInSheet(values, to_replace, replace_with) {
//loop over the rows in the array
for(var row in values){
//use Array.map to execute a replace call on each of the cells in the row.
var replaced_values = values[row].map(function(original_value) {
return original_value.toString().replace(to_replace,replace_with);
});
//replace the original row values with the replaced values
values[row] = replaced_values;
}
}
Give this code a try, if you still have issues with timeouts, my suggestion is setup triggers to help chain functions.