diff --git a/src/Product.ts b/src/Product.ts index fde4358..331fb98 100644 --- a/src/Product.ts +++ b/src/Product.ts @@ -85,6 +85,7 @@ export class Product { } MatchToShopifyProduct(shop: Shop): shopify.Product { + // TODO: Look for and match based on known gid before SKU lookup let product = shop.GetProductBySku(this.sku) if (product == undefined || product.id == undefined || product.id == "") { console.log("MatchToShopifyProduct: no product matched") @@ -171,10 +172,10 @@ export class Product { } while (item.id == "") console.log("UpdateShopifyProduct: publishing to online store") response = this.PublishToShopifyOnlineStore(shop) + console.log("UpdateShopifyProduct: setting defaults on inventory item") + shop.SetInventoryItemDefaults(item, config) 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") shop.UpdateInventoryItemQuantity(item, 1, config) console.log(JSON.stringify(response, null, 2)) diff --git a/src/initMenu.ts b/src/initMenu.ts index 107b57c..85299b1 100644 --- a/src/initMenu.ts +++ b/src/initMenu.ts @@ -3,6 +3,7 @@ import { fillProductFromTemplate } from "./fillProductFromTemplate" import { createMissingPhotoFolders } from "./createMissingPhotoFolders" import { matchProductToShopify, updateProductToShopify } from "./match" import { reinstallTriggers } from "./triggers" +import { toastAndLog } from "./sheetUtils" export function initMenu() { let ui = SpreadsheetApp.getUi() @@ -48,8 +49,19 @@ export function updateShopifyProductHandler() { console.log("skipping edit on sheet " + sheet.getName()) return } - let row = SpreadsheetApp.getCurrentCell().getRow() - updateProductToShopify(row) + let selectedRanges = SpreadsheetApp.getActiveRangeList().getRanges() + if (selectedRanges == null || selectedRanges.length == 0) { + toastAndLog("Select ranges to be updated") + return + } + for (let i = 0; i < selectedRanges.length; i++) { + let range = selectedRanges[i] + let firstRow = range.getRow() + let lastRow = range.getLastRow() + for (let row = firstRow; row <= lastRow; row++) { + updateProductToShopify(row) + } + } } export function reauthorizeScript() { diff --git a/src/match.ts b/src/match.ts index 0182658..5d5789f 100644 --- a/src/match.ts +++ b/src/match.ts @@ -1,6 +1,6 @@ import { Shop } from "./shopifyApi" import { Product } from "./Product" -import { toastAndLog } from "./sheetUtils" +import { getCellValueByColumnName, toastAndLog } from "./sheetUtils" import { getCellRangeByColumnName } from "./sheetUtils" export function matchProductToShopify(row: number) { @@ -15,19 +15,19 @@ export function matchProductToShopify(row: number) { export function updateProductToShopify(row: number) { console.log("row: " + row) - let product = new Product() - let shop = new Shop() - console.log(product) - product.ImportFromInventory(row) - console.log(product) - if (product.sku == "") { + let sheet = SpreadsheetApp.getActive().getSheetByName("product_inventory") + let sku = getCellValueByColumnName(sheet, "sku", row) + console.log("sku: " + sku) + if (sku == "" || sku.match(`\\?`)) { toastAndLog("No SKU defined for the product on row " + row + "!") - let productInventorySheet = - SpreadsheetApp.getActiveSpreadsheet().getSheetByName("product_inventory") - getCellRangeByColumnName(productInventorySheet, "shopify_id", row).setValue( + getCellRangeByColumnName(sheet, "shopify_id", row).setValue( "" ) return } + let product = new Product() + product.ImportFromInventory(row) + console.log(product) + let shop = new Shop() product.UpdateShopifyProduct(shop) } \ No newline at end of file