initial working version
This commit is contained in:
42
src/newSku.ts
Normal file
42
src/newSku.ts
Normal file
@ -0,0 +1,42 @@
|
||||
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
|
||||
}
|
||||
var row = e.range.getRow()
|
||||
var idCell = sheet.getRange(row, 16).getCell(1, 1)
|
||||
var idCellValue = idCell.getValue()
|
||||
var skuPrefixCell = sheet.getRange(row, 15).getCell(1, 1)
|
||||
var skuPrefixCellValue = skuPrefixCell.getValue()
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user