From 9d234c77d9aef3e8fd40f88725a76b6eed6dca7c Mon Sep 17 00:00:00 2001 From: Ben Miller Date: Sun, 17 Nov 2024 09:28:53 -0700 Subject: [PATCH] create column-based onedit handler --- src/OnEditHandler.ts | 32 +++++++++++++++++++++++++++++++- src/Product.ts | 14 +++++++------- src/global.ts | 2 ++ src/onEdit.ts | 1 + src/sheetUtils.ts | 9 +++++++++ src/triggers.ts | 3 ++- 6 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/OnEditHandler.ts b/src/OnEditHandler.ts index f8a273f..64441e4 100644 --- a/src/OnEditHandler.ts +++ b/src/OnEditHandler.ts @@ -1,7 +1,8 @@ /// import { newSkuHandler } from "./newSku" 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) { //TODO: process each edited row @@ -28,3 +29,32 @@ function matchProductToShopifyOnEditHandler( console.log("row: " + 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) + } + } +} \ No newline at end of file diff --git a/src/Product.ts b/src/Product.ts index 218bc7d..13dfa30 100644 --- a/src/Product.ts +++ b/src/Product.ts @@ -149,17 +149,17 @@ export class Product { getCellRangeByColumnName(productInventorySheet, "shopify_id", row).setValue( this.shopify_id ) + let item: shopify.InventoryItem + do { + console.log("UpdateShopifyProduct: attempting to get inventory item") + item = shop.GetInventoryItemBySku(this.sku) + } while (item.id == "") + console.log("UpdateShopifyProduct: publishing to online store") + response = this.PublishToShopifyOnlineStore(shop) if (newProduct) { console.log("UpdateShopifyProduct: setting defaults on new product") - let item: shopify.InventoryItem - do { - console.log("UpdateShopifyProduct: attempting to get inventory item") - item = shop.GetInventoryItemBySku(this.sku) - } while (item.id == "") console.log("UpdateShopifyProduct: setting defaults on inventory item") shop.SetInventoryItemDefaults(item, config) - console.log("UpdateShopifyProduct: publishing to online store") - response = this.PublishToShopifyOnlineStore(shop) console.log("UpdateShopifyProduct: adjusting inventory item quantity") shop.UpdateInventoryItemQuantity(item, 1, config) console.log(JSON.stringify(response, null, 2)) diff --git a/src/global.ts b/src/global.ts index 14da9e9..c562271 100644 --- a/src/global.ts +++ b/src/global.ts @@ -11,12 +11,14 @@ import { } from "./initMenu" import { reinstallTriggers } from "./triggers" import { newSkuHandler } from "./newSku" +import { columnOnEditHandler } from "./OnEditHandler" ;(global as any).onOpen = onOpen ;(global as any).initMenu = initMenu ;(global as any).getShopifyProducts = getShopifyProducts ;(global as any).runShopifyOrders = runShopifyOrders ;(global as any).matchProductToShopifyHandler = matchProductToShopifyHandler ;(global as any).updateShopifyProductHandler = updateShopifyProductHandler +;(global as any).columnOnEditHandler = columnOnEditHandler ;(global as any).reauthorizeScript = reauthorizeScript ;(global as any).reinstallTriggers = reinstallTriggers ;(global as any).newSkuHandler = newSkuHandler diff --git a/src/onEdit.ts b/src/onEdit.ts index 5c3ad8e..604fc3d 100644 --- a/src/onEdit.ts +++ b/src/onEdit.ts @@ -1,4 +1,5 @@ import { onEditHandler } from "./OnEditHandler" +import { getColumnRangeByName } from "./sheetUtils" export function onEdit(e: GoogleAppsScript.Events.SheetsOnEdit) { onEditHandler(e) diff --git a/src/sheetUtils.ts b/src/sheetUtils.ts index bed5703..ee6b367 100644 --- a/src/sheetUtils.ts +++ b/src/sheetUtils.ts @@ -85,3 +85,12 @@ export function getRowByColumnValue( console.log("row found: " + 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]) +} \ No newline at end of file diff --git a/src/triggers.ts b/src/triggers.ts index d973f25..8fea39b 100644 --- a/src/triggers.ts +++ b/src/triggers.ts @@ -10,4 +10,5 @@ export function reinstallTriggers() { .forSpreadsheet(ss) .onEdit() .create() -} \ No newline at end of file + ScriptApp.newTrigger("columnOnEditHandler").forSpreadsheet(ss).onEdit().create() +}