]> git.ardis.lu - sheets-db/commitdiff
Use global constants UI and WORKBOOK
authorArdis Lu <ardisglu@gmail.com>
Fri, 27 Aug 2021 03:41:19 +0000 (20:41 -0700)
committerArdis Lu <ardisglu@gmail.com>
Fri, 27 Aug 2021 03:41:19 +0000 (20:41 -0700)
Globals are more efficient than initializing them within each function

Code.gs

diff --git a/Code.gs b/Code.gs
index bc8b1c3c0500fd12a6e296aea8e222ad546f9532..64a2fb9f507478cacedca4ec56fa9590ab7df993 100644 (file)
--- a/Code.gs
+++ b/Code.gs
@@ -1,3 +1,6 @@
+const UI = SpreadsheetApp.getUi();
+const WORKBOOK = SpreadsheetApp.getActive();
+
 function onOpen() {
   const ui = SpreadsheetApp.getUi();
   ui.createMenu('SQLite')
@@ -21,8 +24,7 @@ KEY ASSUMPTIONS:
 - All dataset values are valid
 - There are no empty rows or empty columns`;
 
-  const ui = SpreadsheetApp.getUi();
-  ui.alert('Help', helpMessage, ui.ButtonSet.OK);
+  UI.alert('Help', helpMessage, ui.ButtonSet.OK);
 }
 
 function generateDatabaseQuery() {
@@ -36,7 +38,7 @@ function generateDatabaseQuery() {
    * - There are no empty rows or empty columns
    */
 
-  const sheets = SpreadsheetApp.getActive().getSheets();
+  const sheets = WORKBOOK.getSheets();
   const fileId = '1xUrGGbaQLVRnytUKtnINAxzild0PBeJZ'; // Google Drive file ID to store the final SQL query
 
   /** Construct SQL for creating the database */
@@ -100,6 +102,5 @@ function generateDatabaseQuery() {
   // https://drive.google.com/u/0/uc?id=1xUrGGbaQLVRnytUKtnINAxzild0PBeJZ&export=download
   DriveApp.getFileById(fileId).setContent(query);
 
-  const ui = SpreadsheetApp.getUi();
-  ui.alert('Success!', `Query generated! Download here: https://drive.google.com/u/0/uc?id=${fileId}&export=download`, ui.ButtonSet.OK);
+  UI.alert('Success!', `Query generated! Download here: https://drive.google.com/u/0/uc?id=${fileId}&export=download`, ui.ButtonSet.OK);
 }
This page took 0.03364 seconds and 4 git commands to generate.