From f85fec9e10b6eea4aa97a8917854ccd2874dd975 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Fri, 3 Dec 2021 22:23:53 -0800 Subject: [PATCH] Move globals inside each function All globals are called each time any function is run, even if the function does not reference the variable. This is really inefficient so it's best to repeat the calls in each function when needed. --- Code.gs | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/Code.gs b/Code.gs index 6bc029e..890f738 100644 --- a/Code.gs +++ b/Code.gs @@ -1,18 +1,15 @@ -const UI = SpreadsheetApp.getUi(); -const WORKBOOK = SpreadsheetApp.getActive(); - -const PROPERTIES = PropertiesService.getScriptProperties(); -const BACKBLAZE_ID = PROPERTIES.getProperty('BACKBLAZE_ID'); -const BACKBLAZE_KEY = PROPERTIES.getProperty('BACKBLAZE_KEY'); - function onOpen() { - UI.createMenu('🔥 sheets-db') + const ui = SpreadsheetApp.getUi(); + + ui.createMenu('🔥 sheets-db') .addItem('❓ Help', 'showHelp') .addItem('🟢 Open sidebar', 'openSidebar') .addToUi(); } function showHelp() { + const ui = SpreadsheetApp.getUi(); + const helpMessage = `Welcome to sheets-db! USAGE: @@ -27,17 +24,19 @@ KEY ASSUMPTIONS: - All dataset values are valid - There are no empty rows or empty columns`; - UI.alert('Help', helpMessage, UI.ButtonSet.OK); + ui.alert('Help', helpMessage, ui.ButtonSet.OK); } function openSidebar() { + const ui = SpreadsheetApp.getUi(); const html = HtmlService.createHtmlOutputFromFile('query-to-db').setTitle('🔥 sheets-db'); - UI.showSidebar(html); + ui.showSidebar(html); } // Helper function which will be called from the client-side dialog function generateDatabaseQuery() { - const sheets = WORKBOOK.getSheets(); + const workbook = SpreadsheetApp.getActive(); + const sheets = workbook.getSheets(); /** Construct SQL for creating the database */ let query = `-- This query was generated by sheets-db @@ -101,16 +100,24 @@ function generateDatabaseQuery() { // Helper function to determine if the upload button should be shown on the sidebar function backblazeCredentialsExist() { + const properties = PropertiesService.getScriptProperties(); + const backblazeId = properties.getProperty('BACKBLAZE_ID'); + const backblazeKey = properties.getProperty('BACKBLAZE_KEY'); + return { - 'BACKBLAZE_ID': BACKBLAZE_ID ? true : false, - 'BACKBLAZE_KEY': BACKBLAZE_KEY ? true : false + 'BACKBLAZE_ID': backblazeId ? true : false, + 'BACKBLAZE_KEY': backblazeKey ? true : false } } // Helper function which will be called from the client-side dialog function uploadToBackblaze(byteArray) { + const properties = PropertiesService.getScriptProperties(); + const backblazeId = properties.getProperty('BACKBLAZE_ID'); + const backblazeKey = properties.getProperty('BACKBLAZE_KEY'); + // b2_authorize_account - const credentials = `Basic ${Utilities.base64Encode(`${BACKBLAZE_ID}:${BACKBLAZE_KEY}`)}`; + const credentials = `Basic ${Utilities.base64Encode(`${backblazeId}:${backblazeKey}`)}`; const authOptions = { 'headers': { 'Authorization': credentials -- 2.47.3