|
| 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 | +[](https://github.com/SheetJS/js-xlsx) |
0 commit comments