working version checkin
* add config class * add vlookup * add menu * add photo folder creation * add prep for product templates
This commit is contained in:
10
src/config.ts
Normal file
10
src/config.ts
Normal file
@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
49
src/createMissingPhotoFolders.ts
Normal file
49
src/createMissingPhotoFolders.ts
Normal file
@ -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(", "))
|
||||||
|
}
|
||||||
4
src/fillProductFromTemplate.ts
Normal file
4
src/fillProductFromTemplate.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
function fillProductFromTemplate() {
|
||||||
|
let row = SpreadsheetApp.getCurrentCell().getRow()
|
||||||
|
productTemplate(row)
|
||||||
|
}
|
||||||
7
src/initMenu.ts
Normal file
7
src/initMenu.ts
Normal file
@ -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()
|
||||||
|
}
|
||||||
3
src/onOpen.ts
Normal file
3
src/onOpen.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
function onOpen() {
|
||||||
|
initMenu()
|
||||||
|
}
|
||||||
44
src/productTemplate.ts
Normal file
44
src/productTemplate.ts
Normal file
@ -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 +
|
||||||
|
"'"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -41,3 +41,28 @@ function getColumnValuesByName(
|
|||||||
return column.getValues()
|
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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user