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();
}
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
UI.alert('Help', helpMessage, UI.ButtonSet.OK);
}
+// Helper function which will be called from the client-side dialog
function generateDatabaseQuery() {
const sheets = WORKBOOK.getSheets();
`;
}
- 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() {
--- /dev/null
+<!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