create column-based onedit handler
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
/// <reference types="@types/google-apps-script" />
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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))
|
||||
|
||||
@ -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
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import { onEditHandler } from "./OnEditHandler"
|
||||
import { getColumnRangeByName } from "./sheetUtils"
|
||||
|
||||
export function onEdit(e: GoogleAppsScript.Events.SheetsOnEdit) {
|
||||
onEditHandler(e)
|
||||
|
||||
@ -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])
|
||||
}
|
||||
@ -10,4 +10,5 @@ export function reinstallTriggers() {
|
||||
.forSpreadsheet(ss)
|
||||
.onEdit()
|
||||
.create()
|
||||
}
|
||||
ScriptApp.newTrigger("columnOnEditHandler").forSpreadsheet(ss).onEdit().create()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user