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
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()

View File

@ -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
const product = new Product(sku)
product.CreatePhotoFolder()
}
// 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(", "))
toastAndLog("Finished creating missing photo folders.")
}