automatically create photo folder

This commit is contained in:
Ben Miller
2025-09-30 00:10:40 -06:00
parent 92f636f247
commit a893cd326f
2 changed files with 57 additions and 47 deletions

View File

@ -219,6 +219,8 @@ export class Product {
} }
// update all metafields // update all metafields
this.UpdateAllMetafields(shop); this.UpdateAllMetafields(shop);
// create product photo folder
this.CreatePhotoFolder();
} }
UpdateAllMetafields(shop: Shop) { UpdateAllMetafields(shop: Shop) {
@ -327,6 +329,47 @@ export class Product {
console.log("metafieldsSet response: " + JSON.stringify(response, null, 2)) 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) { PublishToShopifyOnlineStore(shop: Shop) {
console.log("PublishToShopifyOnlineStore") console.log("PublishToShopifyOnlineStore")
let config = new Config() let config = new Config()

View File

@ -1,56 +1,23 @@
import { Config } from "./config" import { Product } from "./Product"
import { import { getColumnValuesByName, toastAndLog } from "./sheetUtils"
getCellRangeByColumnName,
getColumnValuesByName,
toastAndLog,
} from "./sheetUtils"
export function createMissingPhotoFolders() { export function createMissingPhotoFolders() {
let ss = SpreadsheetApp.getActive() const ss = SpreadsheetApp.getActive()
let s = ss.getSheetByName("product_inventory") const s = ss.getSheetByName("product_inventory")
let config = new Config() if (!s) {
let photoParent = DriveApp.getFolderById(config.productPhotosFolderId) toastAndLog("Could not find 'product_inventory' sheet.")
let skus = getColumnValuesByName(s, "sku") return
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())
} }
console.log("existing folders: " + folderNames.join(", "))
let skus = getColumnValuesByName(s, "sku")
for (let i = 0; i < skus.length; i++) { for (let i = 0; i < skus.length; i++) {
let sku = String(skus[i][0]) const sku = String(skus[i][0])
let updateLink: boolean = false if (!sku) {
if (null == sku || sku == "") {
continue continue
} }
if (folderNames.includes(sku)) { const product = new Product(sku)
console.log("folder '" + sku + "' already exists") product.CreatePhotoFolder()
} else {
console.log("creating folder '" + skus[i] + "'")
photoParent.createFolder(sku)
created.push(sku)
updateLink = true
} }
toastAndLog("Finished creating missing photo folders.")
// 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)
}
toastAndLog("created " + created.length + " folders: " + created.join(", "))
} }