From a893cd326f277a82ae1b9c850f03c239a5fc7904 Mon Sep 17 00:00:00 2001 From: Ben Miller Date: Tue, 30 Sep 2025 00:10:40 -0600 Subject: [PATCH] automatically create photo folder --- src/Product.ts | 43 ++++++++++++++++++++++ src/createMissingPhotoFolders.ts | 61 ++++++++------------------------ 2 files changed, 57 insertions(+), 47 deletions(-) diff --git a/src/Product.ts b/src/Product.ts index 9abff61..0ccdece 100644 --- a/src/Product.ts +++ b/src/Product.ts @@ -219,6 +219,8 @@ export class Product { } // update all metafields this.UpdateAllMetafields(shop); + // create product photo folder + this.CreatePhotoFolder(); } UpdateAllMetafields(shop: Shop) { @@ -327,6 +329,47 @@ export class Product { console.log("metafieldsSet response: " + JSON.stringify(response, null, 2)) } + CreatePhotoFolder() { + console.log("CreatePhotoFolder()"); + const config = new Config(); + if (!config.productPhotosFolderId) { + console.log( + "productPhotoFolderId not set in config. Skipping folder creation." + ); + return; + } + + const productInventorySheet = + SpreadsheetApp.getActiveSpreadsheet().getSheetByName("product_inventory"); + const row = getRowByColumnValue("product_inventory", "sku", this.sku); + const photosCell = getCellRangeByColumnName( + productInventorySheet, + "photos", + row + ); + const folderUrl = photosCell.getValue(); + + if (folderUrl && folderUrl.includes("drive.google.com")) { + console.log(`Photo folder already exists: ${folderUrl}`); + return; + } + + const parentFolder = DriveApp.getFolderById(config.productPhotosFolderId); + const folderName = this.sku + + // DriveApp.createFolder is idempotent if a folder with the same name exists in the parent + const newFolder = parentFolder.createFolder(folderName); + console.log(`Created or found photo folder: '${folderName}'`); + let url = newFolder.getUrl(); + console.log(`Folder URL: ${url}`); + + let linkValue = SpreadsheetApp.newRichTextValue() + .setText(folderName) + .setLinkUrl(url) + .build() + photosCell.setRichTextValue(linkValue) + } + PublishToShopifyOnlineStore(shop: Shop) { console.log("PublishToShopifyOnlineStore") let config = new Config() diff --git a/src/createMissingPhotoFolders.ts b/src/createMissingPhotoFolders.ts index f4a2607..bcf1c14 100644 --- a/src/createMissingPhotoFolders.ts +++ b/src/createMissingPhotoFolders.ts @@ -1,56 +1,23 @@ -import { Config } from "./config" -import { - getCellRangeByColumnName, - getColumnValuesByName, - toastAndLog, -} from "./sheetUtils" +import { Product } from "./Product" +import { getColumnValuesByName, toastAndLog } from "./sheetUtils" export function createMissingPhotoFolders() { - let ss = SpreadsheetApp.getActive() - let s = ss.getSheetByName("product_inventory") - let config = new Config() - let photoParent = DriveApp.getFolderById(config.productPhotosFolderId) - let skus = getColumnValuesByName(s, "sku") - let photoLinks = getColumnValuesByName(s, "photos") - let created: string[] = [] - - let folderItr = photoParent.getFolders() - let folderNames: string[] = [] - console.log("getting list of existing folders...") - while (folderItr.hasNext()) { - let folder = folderItr.next() - folderNames.push(folder.getName()) + const ss = SpreadsheetApp.getActive() + const s = ss.getSheetByName("product_inventory") + if (!s) { + toastAndLog("Could not find 'product_inventory' sheet.") + return } - console.log("existing folders: " + folderNames.join(", ")) + + let skus = getColumnValuesByName(s, "sku") for (let i = 0; i < skus.length; i++) { - let sku = String(skus[i][0]) - let updateLink: boolean = false - if (null == sku || sku == "") { + const sku = String(skus[i][0]) + if (!sku) { continue } - if (folderNames.includes(sku)) { - console.log("folder '" + sku + "' already exists") - } else { - console.log("creating folder '" + skus[i] + "'") - photoParent.createFolder(sku) - created.push(sku) - updateLink = true - } - - // Update photos cell - if (photoLinks[i][0] != "" && !updateLink) { - continue - } - console.log("updating photos cell for '" + sku + "'") - let photosCell = getCellRangeByColumnName(s, "photos", i + 2) - let folder = photoParent.getFoldersByName(sku).next() - let url = folder.getUrl() - let linkValue = SpreadsheetApp.newRichTextValue() - .setText(sku) - .setLinkUrl(url) - .build() - photosCell.setRichTextValue(linkValue) + const product = new Product(sku) + product.CreatePhotoFolder() } - toastAndLog("created " + created.length + " folders: " + created.join(", ")) + toastAndLog("Finished creating missing photo folders.") }