]> git.ardis.lu - sheets-db/commitdiff
Move globals inside each function
authorArdis Lu <ardisglu@gmail.com>
Sat, 4 Dec 2021 06:23:53 +0000 (22:23 -0800)
committerArdis Lu <ardisglu@gmail.com>
Sat, 4 Dec 2021 06:23:53 +0000 (22:23 -0800)
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

diff --git a/Code.gs b/Code.gs
index 6bc029ea110e40a83c251a5f0895a59e6fddd680..890f73810c16b4472310cbc0b5033c9c7dcb1723 100644 (file)
--- 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
This page took 0.040748 seconds and 4 git commands to generate.