I have a sqlite3 file in my google drive and I would like to setup a Google Apps Script from a spreadsheet in Google Drive to read that SQLite file database and fill some rows. Any idea on how to achieve this?
EDIT:
I managed to query the SQLite file using an additional html page with an async javascript call.
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Integrations')
.addItem('SQLite','sqlite')
.addToUi();
}
function sqlite() {
const ui = SpreadsheetApp.getUi();
const html = HtmlService.createHtmlOutputFromFile('sqlite').setTitle('SQLite');
ui.showModalDialog(html, ' ');
}
function getDriveFile() {
return DriveApp.getFilesByName('filename.db').next().getBlob().getBytes();
}
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/sql.js@1.8.0/dist/sql-wasm.min.js"></script>
<script>
// Async call to the DriveApp that loads the SQLite file
let SQL, db, file;
(async() => {
SQL = await initSqlJs({ locateFile: file => 'https://cdn.jsdelivr.net/npm/sql.js@1.8.0/dist/' + file });
google.script.run.withSuccessHandler(buffer => {
db = new SQL.Database(new Uint8Array(buffer));
const stmt = db.prepare("SELECT * FROM TABLE WHERE ID=:id");
const result = stmt.getAsObject({':id' : 1});
console.log(result);
//how can I pass the result back to the main gs file?
google.script.host.close()
}).getDriveFile();
})();
</script>
</head>
<body>
...
</body>
</html>