]> git.ardis.lu - sheets-db/commitdiff
Use PropertiesService for FILE_ID master data
authorArdis Lu <ardisglu@gmail.com>
Fri, 27 Aug 2021 03:49:46 +0000 (20:49 -0700)
committerArdis Lu <ardisglu@gmail.com>
Fri, 27 Aug 2021 03:49:46 +0000 (20:49 -0700)
Code.gs
README.md

diff --git a/Code.gs b/Code.gs
index 64a2fb9f507478cacedca4ec56fa9590ab7df993..092658b1353f7390099497b89c6d54db7b575e54 100644 (file)
--- a/Code.gs
+++ b/Code.gs
@@ -1,6 +1,8 @@
 const UI = SpreadsheetApp.getUi();
 const WORKBOOK = SpreadsheetApp.getActive();
 
+const FILE_ID = PropertiesService.getScriptProperties().getProperty('FILE_ID');
+
 function onOpen() {
   const ui = SpreadsheetApp.getUi();
   ui.createMenu('SQLite')
@@ -39,7 +41,6 @@ function generateDatabaseQuery() {
    */
 
   const sheets = WORKBOOK.getSheets();
-  const fileId = '1xUrGGbaQLVRnytUKtnINAxzild0PBeJZ'; // Google Drive file ID to store the final SQL query
 
   /** Construct SQL for creating the database */
   let query = `-- This query was generated by sheets-db
@@ -100,7 +101,7 @@ function generateDatabaseQuery() {
 
   // URL to download the file:
   // https://drive.google.com/u/0/uc?id=1xUrGGbaQLVRnytUKtnINAxzild0PBeJZ&export=download
-  DriveApp.getFileById(fileId).setContent(query);
+  DriveApp.getFileById(FILE_ID).setContent(query);
 
-  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=${FILE_ID}&export=download`, ui.ButtonSet.OK);
 }
index d4bdf82072308b6a4fe34c9d6d8b352627f7e785..7a40cffb365e35f7032ee96a03afce68d42fa4b2 100644 (file)
--- a/README.md
+++ b/README.md
@@ -25,7 +25,7 @@ These steps only need to be completed once.
 3. Create a new Google Sheet or create a copy of the [demo Google Sheet](https://docs.google.com/spreadsheets/d/1pY-0oDh4pI8NffZQwN-AkpfJdot7bmlM9nky7w150UY/edit?usp=sharing).
 4. Go to Tools > Script Editor to create a new Google Apps Script (GAS) project. The GAS project is automatically attached to the Google Sheet.
 5. Copy `Code.gs` into the GAS project and save. If you copied the demo Google Sheet, this code should already be there.
-6. **(IMPORTANT)** In `Code.gs`, update the hardcoded constant `fileId` to the file ID noted in Step 2.
+6. **(IMPORTANT)** In `Code.gs`, there is a reference to a [Google Apps Script property](https://developers.google.com/apps-script/guides/properties) (environment variable) named `FILE_ID`. Use the GAS user interface to set `FILE_ID` to the file ID noted in Step 2.
 
 # Usage
 
This page took 0.040388 seconds and 4 git commands to generate.