make newSku resiliant to column moves

This commit is contained in:
Ben Miller
2024-11-17 14:20:27 -07:00
parent fa700b622e
commit f43c758fdb
2 changed files with 24 additions and 10 deletions

View File

@ -1,6 +1,8 @@
import {
getColumnByName,
getCellRangeByColumnName,
getCellValueByColumnName,
getColumnValuesByName,
} from "./sheetUtils"
@ -23,6 +25,10 @@ export function newSkuHandler(e: GoogleAppsScript.Events.SheetsOnEdit) {
export function newSku(row: number) {
let sheet = SpreadsheetApp.getActive().getSheetByName("product_inventory")
let skuPrefixCol = getColumnByName(sheet, "sku_prefix")
console.log("skuPrefixCol: " + skuPrefixCol)
let idCol = getColumnByName(sheet, "#")
console.log("idCol: " + idCol)
let idCell = getCellRangeByColumnName(sheet, "#", row)
let safeToOverwrite: string[] = ["?", "n", ""]
let idCellValue = idCell.getValue()
@ -32,20 +38,20 @@ export function newSku(row: number) {
console.log("ID '" + idCellValue + "' is not safe to overwrite, returning")
return
}
var idArray = sheet.getRange(2, 9, sheet.getLastRow(), 1).getValues()
var skuArray = getColumnValuesByName(sheet, "sku")
var regExp = new RegExp(`^` + skuPrefixCellValue + `-0*(\\d+)$`)
console.log("regExp: " + regExp.toString())
var maxId = 0
for (let i = 0; i < idArray.length; i++) {
for (let i = 0; i < skuArray.length; i++) {
console.log("checking row " + (i + 1))
if (null == idArray[i] || String(idArray[i]) == "") {
console.log("ID cell looks null")
if (null == skuArray[i] || String(skuArray[i]) == "") {
console.log("SKU cell looks null")
continue
}
console.log("ID cell: '" + idArray[i] + "'")
var match = regExp.exec(String(idArray[i]))
console.log("SKU cell: '" + skuArray[i] + "'")
var match = regExp.exec(String(skuArray[i]))
if (null === match) {
console.log("ID cell did not match")
console.log("SKU cell did not match")
continue
}
let numId = Number(match[1])