diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..f9108ca --- /dev/null +++ b/src/config.ts @@ -0,0 +1,10 @@ +class Config { + productPhotosFolderId: string + + constructor() { + let ss = SpreadsheetApp.getActive() + let s = ss.getSheetByName("vars") + + this.productPhotosFolderId = vlookupByColumns("vars", "key", "productPhotosFolderId", "value") + } +} \ No newline at end of file diff --git a/src/createMissingPhotoFolders.ts b/src/createMissingPhotoFolders.ts new file mode 100644 index 0000000..1b7aff0 --- /dev/null +++ b/src/createMissingPhotoFolders.ts @@ -0,0 +1,49 @@ +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()) + } + console.log("existing folders: " + folderNames.join(", ")) + + for (let i = 0; i < skus.length; i++) { + let sku = String(skus[i][0]) + let updateLink: boolean = false + if (null == sku || 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) + } + toastAndLog("created " + created.length + " folders: " + created.join(", ")) +} diff --git a/src/fillProductFromTemplate.ts b/src/fillProductFromTemplate.ts new file mode 100644 index 0000000..398b8db --- /dev/null +++ b/src/fillProductFromTemplate.ts @@ -0,0 +1,4 @@ +function fillProductFromTemplate() { + let row = SpreadsheetApp.getCurrentCell().getRow() + productTemplate(row) +} diff --git a/src/initMenu.ts b/src/initMenu.ts new file mode 100644 index 0000000..f584f1a --- /dev/null +++ b/src/initMenu.ts @@ -0,0 +1,7 @@ +function initMenu() { + let ui = SpreadsheetApp.getUi() + ui.createMenu("BLM") + .addItem("Fill out product from template", "fillProductFromTemplate") + .addItem("Create missing photo folders", "createMissingPhotoFolders") + .addToUi() +} diff --git a/src/onOpen.ts b/src/onOpen.ts new file mode 100644 index 0000000..48e8d9d --- /dev/null +++ b/src/onOpen.ts @@ -0,0 +1,3 @@ +function onOpen() { + initMenu() +} diff --git a/src/productTemplate.ts b/src/productTemplate.ts new file mode 100644 index 0000000..d27f319 --- /dev/null +++ b/src/productTemplate.ts @@ -0,0 +1,44 @@ +function productTemplate(row: number) { + let updateColumns = [ + "function", + "type", + "category", + "product_type", + "tags", + "price", + "shipping", + "weight (grams)", + ] + + let productInventorySheet = + SpreadsheetApp.getActiveSpreadsheet().getSheetByName("product_inventory") + let titleValue = getCellValueByColumnName(productInventorySheet, "title", row) + for (let i = 0; i < updateColumns.length; i++) { + let currentValue = getCellValueByColumnName( + productInventorySheet, + updateColumns[i], + row + ) + if (currentValue != "") { + console.log( + "current value for '" + updateColumns[i] + "' is '" + currentValue + "'" + ) + continue + } + let templateValue = vlookupByColumns( + "product_types", + "title", + titleValue, + updateColumns[i] + ) + if (templateValue != "") { + console.log( + "template value for '" + + updateColumns[i] + + "' is '" + + templateValue + + "'" + ) + } + } +} diff --git a/src/sheetUtils.ts b/src/sheetUtils.ts index 03a9d1e..6ccd7cd 100644 --- a/src/sheetUtils.ts +++ b/src/sheetUtils.ts @@ -41,3 +41,28 @@ function getColumnValuesByName( return column.getValues() } } + +function vlookupByColumns( + sheetName: string, + searchColumn: string, + searchKey: string, + resultColumn: string +) { + let s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName) + let searchData = getColumnValuesByName(s, searchColumn) + let dataList = searchData.map((x) => x[0]) + let index = dataList.indexOf(searchKey) + + if (index === -1) { + toastAndLog(searchKey + " not found") + return + } + let resultValue = getCellValueByColumnName(s, resultColumn, index + 2) + toastAndLog(resultValue) + return resultValue +} + +function toastAndLog(message: string) { + SpreadsheetApp.getActive().toast(message) + console.log(message) +} \ No newline at end of file