-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSidebar.html
91 lines (79 loc) · 2.75 KB
/
Sidebar.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<!DOCTYPE html>
<html>
<body>
<pre id="status"></pre>
<pre id="results"></pre>
<script type="module">
let db;
const status = document.getElementById('status');
const results = document.getElementById('results');
async function logStatus(msg) {
console.log(msg);
status.textContent += msg + '\n';
}
async function loadDuckDB() {
logStatus('Loading DuckDB...');
const JSDELIVR = 'https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm';
const DUCKDB_CONFIG = {
mainModule: JSDELIVR + '@latest/dist/duckdb-eh.wasm',
mainWorker: JSDELIVR + '@latest/dist/duckdb-browser-eh.worker.js',
};
try {
const workerUrl = URL.createObjectURL(
new Blob([`importScripts("${DUCKDB_CONFIG.mainWorker}");`],
{ type: 'text/javascript' })
);
const worker = new Worker(workerUrl);
const logger = new duckdb.ConsoleLogger();
db = new duckdb.AsyncDuckDB(logger, worker);
await db.instantiate(DUCKDB_CONFIG.mainModule);
URL.revokeObjectURL(workerUrl);
logStatus('DuckDB Ready');
runQuery();
} catch (err) {
logStatus('Error: ' + err.message);
}
}
async function runQuery() {
try {
const query = await new Promise((resolve, reject) => {
google.script.run
.withSuccessHandler(q => {
logStatus('Got query: ' + q);
resolve(q);
})
.withFailureHandler(reject)
.getSelectedQuery();
});
logStatus('Executing query...');
const conn = await db.connect();
const result = await conn.query(query);
const rawData = result.toArray().map(row => Array.from(row));
// Extract just the values from each row
const processedData = rawData.map(row => {
return row.map(cell => cell[1]); // Take second element of each cell
});
logStatus('Query results:');
logStatus(JSON.stringify(processedData, null, 2));
if (processedData && processedData.length > 0) {
await new Promise((resolve, reject) => {
google.script.run
.withSuccessHandler(resolve)
.withFailureHandler(reject)
.writeResults(processedData);
});
logStatus('Data written to sheet');
} else {
logStatus('No results to write');
}
} catch (err) {
logStatus('Error: ' + err.message);
}
}
const script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@latest/dist/duckdb-browser.js';
script.onload = loadDuckDB;
document.body.appendChild(script);
</script>
</body>
</html>