From: Ardis Lu Date: Fri, 27 Aug 2021 03:41:19 +0000 (-0700) Subject: Use global constants UI and WORKBOOK X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=e7d31c78c82292fd12dedc5058de4ac01f7c83c8;p=sheets-db Use global constants UI and WORKBOOK Globals are more efficient than initializing them within each function --- diff --git a/Code.gs b/Code.gs index bc8b1c3..64a2fb9 100644 --- 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); }