In my situation, I have a spreadsheet that contains a script responsible for checking another linked spreadsheet for a customer's name and then returning the associated code. Everything works smoothly when the customer name is found in the "CustomerCodes" sheet that the script references. However, if the customer name does not exist in that sheet, I want to ensure that the variable value "customerCode" is set to "No match found." Currently, the script throws an error and stops running if the customer name cannot be located in the referenced sheet. Below is the snippet of the code in question:
var customerName = sheet.getRange('I2').getValue();
var ccsheet = ss.getSheetByName("CustomerCodes");
var lastRow = ccsheet.getLastRow();
Logger.log("lastRow: " + lastRow);
var lookUp = ccsheet.getRange(2, 1, lastRow, 3).getValues();
for (nn=0; nn<lookUp.length; ++nn) {
if (lookUp[nn][0] == customerName) {break}
}
//This is where I am having the trouble
var customerCode = lookUp[nn][1];
Logger.log("customerCode: " + customerCode);
If the "for" loop successfully finds a matching customer name, it sets the "customerCode" variable to that specific match. In cases where no match is found, I aim to have the "customerCode" variable indicate "No match found," providing clarity to users as to why the customer's code was not returned. This value stored in the "customerCode" variable is later delivered to the user within the function.
Being relatively new to scripting, I lack a strong understanding of error handling and couldn't find comprehensive Google Apps Script documentation on the topic. Any assistance you can provide would be greatly appreciated!