From 9b01d6de8aa15540dda3de57939375ab1a97cd06 Mon Sep 17 00:00:00 2001 From: Ben Miller Date: Thu, 14 Nov 2024 00:50:05 -0700 Subject: [PATCH] use installed triggers cannot call external services from default triggers --- src/global.ts | 17 ++++++++++++----- src/initMenu.ts | 12 ++++++++++++ src/triggers.ts | 10 ++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 src/triggers.ts diff --git a/src/global.ts b/src/global.ts index 6afe3c0..1715479 100644 --- a/src/global.ts +++ b/src/global.ts @@ -1,13 +1,20 @@ /// import { onOpen } from "./onOpen" -import { onEdit } from "./onEdit" import { getShopifyProducts } from "./shopifyApi" import { runShopifyOrders } from "./shopifyApi" -import { matchProductToShopifyHandler } from "./initMenu" - -;(global as any).onOpen = () => onOpen() -;(global as any).onEdit = (e: GoogleAppsScript.Events.SheetsOnEdit) => onEdit(e) +import { + initMenu, + matchProductToShopifyHandler, + reauthorizeScript, +} from "./initMenu" +import { reinstallTriggers } from "./triggers" +import { newSkuHandler } from "./newSku" +;(global as any).onOpen = onOpen +;(global as any).initMenu = initMenu ;(global as any).getShopifyProducts = getShopifyProducts ;(global as any).runShopifyOrders = runShopifyOrders ;(global as any).matchProductToShopifyHandler = matchProductToShopifyHandler +;(global as any).reauthorizeScript = reauthorizeScript +;(global as any).reinstallTriggers = reinstallTriggers +;(global as any).newSkuHandler = newSkuHandler diff --git a/src/initMenu.ts b/src/initMenu.ts index 2c57c33..6814ea6 100644 --- a/src/initMenu.ts +++ b/src/initMenu.ts @@ -2,6 +2,7 @@ import { getShopifyProducts, runShopifyOrders } from "./shopifyApi" import { fillProductFromTemplate } from "./fillProductFromTemplate" import { createMissingPhotoFolders } from "./createMissingPhotoFolders" import { matchProductToShopify } from "./match" +import { reinstallTriggers } from "./triggers" export function initMenu() { let ui = SpreadsheetApp.getUi() @@ -20,6 +21,13 @@ export function initMenu() { .addItem("Run Shopify Orders", runShopifyOrders.name) .addItem("Get Shopify Products", getShopifyProducts.name) ) + .addSeparator() + .addSubMenu( + ui + .createMenu("Utilities...") + .addItem("Reauthorize script", reauthorizeScript.name) + .addItem("Reinstall triggers", reinstallTriggers.name) + ) .addToUi() } @@ -32,3 +40,7 @@ export function matchProductToShopifyHandler() { let row = SpreadsheetApp.getCurrentCell().getRow() matchProductToShopify(row) } + +export function reauthorizeScript() { + ScriptApp.invalidateAuth() +} diff --git a/src/triggers.ts b/src/triggers.ts new file mode 100644 index 0000000..a718a6c --- /dev/null +++ b/src/triggers.ts @@ -0,0 +1,10 @@ +export function reinstallTriggers() { + let currentTriggers = ScriptApp.getProjectTriggers() + for (let i = 0; i < currentTriggers.length; i++) { + ScriptApp.deleteTrigger(currentTriggers[i]) + } + + let ss = SpreadsheetApp.getActive() + ScriptApp.newTrigger("newSkuHandler").forSpreadsheet(ss).onEdit().create() + ScriptApp.newTrigger("matchProductToShopifyHandler").forSpreadsheet(ss).onEdit().create() +} \ No newline at end of file