function newSku(e: GoogleAppsScript.Events.SheetsOnEdit) { var sheet = SpreadsheetApp.getActive().getActiveSheet() if (sheet.getName() !== "product_inventory") { console.log("skipping edit on sheet " + sheet.getName()) return } let row = e.range.getRow() let idCell = getCellRangeByColumnName(sheet, "#", row) let idCellValue = idCell.getValue() let skuPrefixCellValue = getCellValueByColumnName(sheet, "sku_prefix", row) console.log("skuPrefixCellValue = '" + skuPrefixCellValue + "'") console.log("idCellValue = '" + idCellValue + "'") if (idCellValue != "?" && idCellValue != "n") { console.log("new ID was not requested, returning") return } var idArray = sheet.getRange(2, 9, sheet.getLastRow(), 1).getValues() var regExp = new RegExp(`^` + skuPrefixCellValue + `-0*(\\d+)$`) console.log("regExp: " + regExp.toString()) var maxId = 0 for (let i = 0; i < idArray.length; i++) { console.log("checking row " + (i + 1)) if (null == idArray[i] || String(idArray[i]) == "") { console.log("ID cell looks null") continue } console.log("ID cell: '" + idArray[i] + "'") var match = regExp.exec(String(idArray[i])) if (null === match) { console.log("ID cell did not match") continue } let numId = Number(match[1]) console.log("match: '" + match + "', numId: " + numId) maxId = Math.max(numId, maxId) console.log("numId: " + numId + ", maxId: " + maxId) } let newId = maxId + 1 console.log("newId: " + newId) idCell.setValue(newId) }