Skip to content

Commit 6ecfeb6

Browse files
committed
Added google sheet example
Description Added to readme Fixed typo
1 parent b0e68a9 commit 6ecfeb6

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

demos/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ can be installed with Bash on Windows or with `cygwin`.
4343
- [`electron application`](electron/)
4444
- [`nw.js application`](nwjs/)
4545
- [`Chrome / Chromium extensions`](chrome/)
46+
- [`Download a Google Sheet locally`](google-sheet/)
4647
- [`Adobe ExtendScript`](extendscript/)
4748
- [`Headless Browsers`](headless/)
4849
- [`canvas-datagrid`](datagrid/)

demos/google-sheet/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Google Sheet Demo
2+
3+
This demo is using [`drive-db`](https://github.com/franciscop/drive-db) to fetch a public Google Sheet and then `xlsx` to save the data locally as `test.xlsx`.
4+
5+
It uses modern Javascript; `import/export`, `async/away`, etc. To run this you need Node.js 12 or newer, and you will notice we added `"type": "module"` to the `package.json`.
6+
7+
Here is the full code:
8+
9+
```js
10+
import xlsx from "xlsx";
11+
import drive from "drive-db";
12+
13+
(async () => {
14+
const data = await drive("1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k");
15+
16+
/* Create a new workbook */
17+
const workbook = xlsx.utils.book_new();
18+
19+
/* make worksheet */
20+
const worksheet = xlsx.utils.json_to_sheet(data);
21+
22+
/* Add the worksheet to the workbook */
23+
xlsx.utils.book_append_sheet(workbook, worksheet);
24+
25+
xlsx.writeFile(workbook, "test.xlsx");
26+
})();
27+
```
28+
29+
Let's go over the different parts:
30+
31+
```js
32+
import xlsx from "xlsx";
33+
import drive from "drive-db";
34+
```
35+
36+
This imports both `xlsx` and `drive-db` libraries. While these are written in commonjs, Javascript Modules can usually import the commonjs modules with no problem.
37+
38+
```js
39+
(async () => {
40+
// ...
41+
})();
42+
```
43+
44+
This is what is called an [Immediately Invoked Function Expression](https://flaviocopes.com/javascript-iife/). These are normally used to either create a new execution context, or in this case to allow to run async code easier.
45+
46+
```js
47+
const data = await drive("1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k");
48+
```
49+
50+
Using `drive-db`, fetch the data for the given spreadsheet id. In this case it's [this Google Sheet document](https://docs.google.com/spreadsheets/d/1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k/edit), and since we don't specify the sheet it's the default one.
51+
52+
```js
53+
const workbook = xlsx.utils.book_new();
54+
const worksheet = xlsx.utils.json_to_sheet(data);
55+
```
56+
57+
We need to create a workbook with a worksheet inside. The worksheet is created from the previously fetched data. `drive-db` exports the data in the same format that `xlsx`'s `.json_to_sheet()` method expects, so it's a straightforward operation.
58+
59+
```js
60+
xlsx.utils.book_append_sheet(workbook, worksheet);
61+
```
62+
63+
The worksheet needs to be inside the workbook, so we use the operation `.book_append_sheet()` to make it so.
64+
65+
```js
66+
xlsx.writeFile(workbook, "test.xlsx");
67+
```
68+
69+
Finally we save the workbook into a XLSX file in out filesystem. With this, now it can be opened by any spreadsheet program that we have installed.
70+
71+
[![Analytics](https://ga-beacon.appspot.com/UA-36810333-1/SheetJS/js-xlsx?pixel)](https://github.com/SheetJS/js-xlsx)

demos/google-sheet/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import xlsx from "xlsx";
2+
import drive from "drive-db";
3+
4+
(async () => {
5+
const data = await drive("1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k");
6+
7+
/* Create a new workbook */
8+
const workbook = xlsx.utils.book_new();
9+
10+
/* make worksheet */
11+
const worksheet = xlsx.utils.json_to_sheet(data);
12+
13+
/* Add the worksheet to the workbook */
14+
xlsx.utils.book_append_sheet(workbook, worksheet);
15+
16+
xlsx.writeFile(workbook, "test.xlsx");
17+
})();

demos/google-sheet/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "google-sheet",
3+
"version": "1.0.0",
4+
"description": " This demo is using 'drive-db' to fetch a public Google Sheet and then `xlsx` to save the data locally as test.xlsx",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"start": "node ."
9+
},
10+
"keywords": [],
11+
"author": "Francisco Presencia <[email protected]> (https://francisco.io/)",
12+
"license": "MIT"
13+
}

0 commit comments

Comments
 (0)