create column-based onedit handler

This commit is contained in:
Ben Miller
2024-11-17 09:28:53 -07:00
parent d6d6af3c44
commit 9d234c77d9
6 changed files with 52 additions and 9 deletions

View File

@ -1,7 +1,8 @@
/// <reference types="@types/google-apps-script" /> /// <reference types="@types/google-apps-script" />
import { newSkuHandler } from "./newSku" import { newSkuHandler } from "./newSku"
import { getCellRangeByColumnName } from "./sheetUtils" import { getCellRangeByColumnName } from "./sheetUtils"
import { matchProductToShopify } from "./match" import { matchProductToShopify, updateProductToShopify } from "./match"
import { getColumnName, toastAndLog } from "./sheetUtils"
export function onEditHandler(e: GoogleAppsScript.Events.SheetsOnEdit) { export function onEditHandler(e: GoogleAppsScript.Events.SheetsOnEdit) {
//TODO: process each edited row //TODO: process each edited row
@ -28,3 +29,32 @@ function matchProductToShopifyOnEditHandler(
console.log("row: " + row) console.log("row: " + row)
matchProductToShopify(row) matchProductToShopify(row)
} }
export function columnOnEditHandler(e: GoogleAppsScript.Events.SheetsOnEdit) {
let sheetName = e.range.getSheet().getSheetName()
if (sheetName !== "product_inventory") {
console.log("skipping edit on sheet " + sheetName)
return
}
for (let col = e.range.getColumn(); col <= e.range.getLastColumn(); col++) {
console.log("col: " + col)
let header = getColumnName(sheetName, col)
console.log("header: " + header)
for (let row = e.range.getRow(); row <= e.range.getLastRow(); row++) {
console.log("row: " + row)
let updateString = "updating " + header + " on row " + row
toastAndLog(updateString)
switch (header) {
case "shopify_status":
updateProductToShopify(row)
break
case "price":
updateProductToShopify(row)
break
default:
continue
}
toastAndLog("completed " + updateString)
}
}
}

View File

@ -149,17 +149,17 @@ export class Product {
getCellRangeByColumnName(productInventorySheet, "shopify_id", row).setValue( getCellRangeByColumnName(productInventorySheet, "shopify_id", row).setValue(
this.shopify_id this.shopify_id
) )
if (newProduct) {
console.log("UpdateShopifyProduct: setting defaults on new product")
let item: shopify.InventoryItem let item: shopify.InventoryItem
do { do {
console.log("UpdateShopifyProduct: attempting to get inventory item") console.log("UpdateShopifyProduct: attempting to get inventory item")
item = shop.GetInventoryItemBySku(this.sku) item = shop.GetInventoryItemBySku(this.sku)
} while (item.id == "") } while (item.id == "")
console.log("UpdateShopifyProduct: setting defaults on inventory item")
shop.SetInventoryItemDefaults(item, config)
console.log("UpdateShopifyProduct: publishing to online store") console.log("UpdateShopifyProduct: publishing to online store")
response = this.PublishToShopifyOnlineStore(shop) response = this.PublishToShopifyOnlineStore(shop)
if (newProduct) {
console.log("UpdateShopifyProduct: setting defaults on new product")
console.log("UpdateShopifyProduct: setting defaults on inventory item")
shop.SetInventoryItemDefaults(item, config)
console.log("UpdateShopifyProduct: adjusting inventory item quantity") console.log("UpdateShopifyProduct: adjusting inventory item quantity")
shop.UpdateInventoryItemQuantity(item, 1, config) shop.UpdateInventoryItemQuantity(item, 1, config)
console.log(JSON.stringify(response, null, 2)) console.log(JSON.stringify(response, null, 2))

View File

@ -11,12 +11,14 @@ import {
} from "./initMenu" } from "./initMenu"
import { reinstallTriggers } from "./triggers" import { reinstallTriggers } from "./triggers"
import { newSkuHandler } from "./newSku" import { newSkuHandler } from "./newSku"
import { columnOnEditHandler } from "./OnEditHandler"
;(global as any).onOpen = onOpen ;(global as any).onOpen = onOpen
;(global as any).initMenu = initMenu ;(global as any).initMenu = initMenu
;(global as any).getShopifyProducts = getShopifyProducts ;(global as any).getShopifyProducts = getShopifyProducts
;(global as any).runShopifyOrders = runShopifyOrders ;(global as any).runShopifyOrders = runShopifyOrders
;(global as any).matchProductToShopifyHandler = matchProductToShopifyHandler ;(global as any).matchProductToShopifyHandler = matchProductToShopifyHandler
;(global as any).updateShopifyProductHandler = updateShopifyProductHandler ;(global as any).updateShopifyProductHandler = updateShopifyProductHandler
;(global as any).columnOnEditHandler = columnOnEditHandler
;(global as any).reauthorizeScript = reauthorizeScript ;(global as any).reauthorizeScript = reauthorizeScript
;(global as any).reinstallTriggers = reinstallTriggers ;(global as any).reinstallTriggers = reinstallTriggers
;(global as any).newSkuHandler = newSkuHandler ;(global as any).newSkuHandler = newSkuHandler

View File

@ -1,4 +1,5 @@
import { onEditHandler } from "./OnEditHandler" import { onEditHandler } from "./OnEditHandler"
import { getColumnRangeByName } from "./sheetUtils"
export function onEdit(e: GoogleAppsScript.Events.SheetsOnEdit) { export function onEdit(e: GoogleAppsScript.Events.SheetsOnEdit) {
onEditHandler(e) onEditHandler(e)

View File

@ -85,3 +85,12 @@ export function getRowByColumnValue(
console.log("row found: " + resultRow) console.log("row found: " + resultRow)
return resultRow return resultRow
} }
export function getColumnName(sheetName: string, col: number) {
let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName)
if (!sheet) {
return
}
let headers = sheet.getRange("A1:1").getValues()
return String(headers[0][col-1])
}

View File

@ -10,4 +10,5 @@ export function reinstallTriggers() {
.forSpreadsheet(ss) .forSpreadsheet(ss)
.onEdit() .onEdit()
.create() .create()
ScriptApp.newTrigger("columnOnEditHandler").forSpreadsheet(ss).onEdit().create()
} }