]> git.ardis.lu - sheets-db/commitdiff
Generate a sqlite database instead of a query
authorArdis Lu <ardisglu@gmail.com>
Sun, 28 Nov 2021 04:04:37 +0000 (20:04 -0800)
committerArdis Lu <ardisglu@gmail.com>
Sun, 28 Nov 2021 04:04:37 +0000 (20:04 -0800)
Implemented by using client-side code which calls sql.js

Code.gs
query-to-db.html [new file with mode: 0644]

diff --git a/Code.gs b/Code.gs
index 69ce57e46520efa978df5a6d2f0cd9f898a38504..24174227dae5f4404f005eee9a212e499b296426 100644 (file)
--- a/Code.gs
+++ b/Code.gs
@@ -9,7 +9,7 @@ const BACKBLAZE_KEY = PROPERTIES.getProperty('BACKBLAZE_KEY');
 function onOpen() {
   UI.createMenu('SQLite')
     .addItem('Help', 'showHelp')
-    .addItem('Generate SQLite database query', 'generateDatabaseQuery')
+    .addItem('Download SQLite database', 'showDatabaseDownloadDialog')
     .addItem('Upload to Backblaze', 'uploadToBackblaze')
     .addToUi();
 }
@@ -19,7 +19,7 @@ function showHelp() {
 
 USAGE:
 - Edit the data on this workbook like you would on any other Google Sheet, noting KEY ASSUMPTIONS below.
-- When ready, press the "Generate SQLite database query" button to generate a SQL query which can be used to write a SQLite database containing this workbook's data!
+- When ready, press the "Download SQLite database" button to generate and download a SQLite database from the workbook's data!
 
 KEY ASSUMPTIONS: 
 - Each tab in this workbook is a data table
@@ -32,6 +32,7 @@ KEY ASSUMPTIONS:
   UI.alert('Help', helpMessage, UI.ButtonSet.OK);
 }
 
+// Helper function which will be called from the client-side dialog
 function generateDatabaseQuery() {
   const sheets = WORKBOOK.getSheets();
 
@@ -92,9 +93,12 @@ function generateDatabaseQuery() {
     `;
   }
 
-  DriveApp.getFileById(FILE_ID).setContent(query);
+  return query;
+}
 
-  UI.alert('Success!', `Query generated! Download here: https://drive.google.com/u/0/uc?id=${FILE_ID}&export=download`, UI.ButtonSet.OK);
+function showDatabaseDownloadDialog() {
+  const html = HtmlService.createHtmlOutputFromFile('query-to-db');
+  UI.showModalDialog(html, 'Generating database...');
 }
 
 function uploadToBackblaze() {
diff --git a/query-to-db.html b/query-to-db.html
new file mode 100644 (file)
index 0000000..c016013
--- /dev/null
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<html>
+
+<head>
+  <base target="_top">
+</head>
+
+<body>
+  <a>Generating database...</a>
+
+  <script src="https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.6.1/sql-wasm.min.js"></script>
+  <script>
+    (async () => {
+      // WARNING: The following line will break if you try to use string interpolation (i.e. strings with backticks)
+      const SQL = await initSqlJs({ locateFile: file => 'https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.6.1/' + file });
+      db = new SQL.Database();
+
+      const handler = query => {
+        db.run(query);
+        const buffer = db.export();
+        const file = new Blob([buffer], { type: 'application/vnd.sqlite3' });
+
+        const a = document.querySelector('a');
+        a.setAttribute('download', `${Date.now()}.sqlite3`);
+        a.setAttribute('href', `${URL.createObjectURL(file)}`);
+        a.textContent = 'Download ready! Click here to download the .sqlite3 database.';
+      }
+
+      google.script.run.withSuccessHandler(handler).generateDatabaseQuery();
+    })();
+  </script>
+</body>
+
+</html>
\ No newline at end of file
This page took 0.044133 seconds and 4 git commands to generate.