I have the following google script that will run against 500+ rows of data in the "InputFromHospital" sheet and in the middle of execution, getting the "Exceeded maximum execution time" error.
I was having few more lines of code but after reading the similar questions in StackOverflow, I removed and kept only the needed lines/source code. I am not sure how I could further optimize.
Looking for your suggestions/expertise to optimize and to reduce the execution time to under 5 minutes?
function feedToMasterAndPolice() {
var ssbook = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = ssbook.getSheetByName('MasterPatientData')
var sLastRow = srcSheet.getLastRow() + 1
var inputSheet = ssbook.getSheetByName('InputFromHospital')
var iLastRow = inputSheet.getLastRow();
var pSheet = ssbook.getSheetByName('DataToPolice')
var pLastRow = pSheet.getLastRow() + 1;
var todayDate = Utilities.formatDate(new Date(), "IST", "dd/MMM/yyyy")
//Loop thru all rows in "Input.." sheet
for (var i = 2; i <= iLastRow; i++) {
//Reading contact number from "Input.." sheet
var value = inputSheet.getRange(i, 5).getValue();
var gID = "";
if (value.toString.length > 0) {
//Generating unique patient ID for each COVID Patient
gID = "PID".concat(srcSheet.getLastRow());
srcSheet.getRange(sLastRow, 1).setValue(gID.toString());
//Looping thru all 8 colums in "Input" sheet
for (var colN = 2; colN < 9; colN++) {
var actCellVal = inputSheet.getRange(i, colN).getValue();
//Set value for contact# and date values
if (colN == 5 || colN == 6 || colN == 8) {
srcSheet.getRange(sLastRow, colN).setValue(actCellVal)
} else {
//All String values - upper case
srcSheet.getRange(sLastRow, colN).setValue(actCellVal.toString().toUpperCase())
}
}
var cR = srcSheet.getLastRow();
//Adding formula to calculate "DAYS SINCE ADMISSION" - example =if(A2<>"",TODAY()-F2,"")
srcSheet.getRange(sLastRow, 9).setFormula("=if(A" + cR + "<>\"\",TODAY()-F" + cR + ",\"\")")
//Adding formula to calculate "FOLLOW UP NEEDED" - example =if(I2<=Admin!$C$2,"YES","NO"
srcSheet.getRange(sLastRow, 10).setFormula("=if(I" + cR + "<=\'Admin\'!$C$2,\"YES\",\"NO\")");
//Add current date
srcSheet.getRange(sLastRow, 11).setValue(todayDate);
sLastRow = sLastRow + 1
} else {
//Above logic same when contact number is blank
//Not considerd Patient ID #
var ppID = "NCPID".concat(pSheet.getLastRow());
//Untested
pSheet.getRange(pLastRow, 1).clear();
pSheet.getRange(pLastRow, 1).setValue(ppID.toString());
//till above
for (var colN = 2; colN <= 9; colN++) {
var actCellVal = inputSheet.getRange(i, colN).getValue();
if (colN == 6 || colN == 8) {
pSheet.getRange(pLastRow, colN).setValue(actCellVal)
} else {
pSheet.getRange(pLastRow, colN).setValue(actCellVal.toString().toUpperCase())
pSheet.getRange(pLastRow, 9).setValue(todayDate);
}
}
pLastRow = pLastRow + 1
}
}
//this.ClearAnySheet("InputFromHospital")
};
Sample file available here.
if (value.toString.length > 0)? - Tanaike