|
| 1 | +# json-excel [](https://travis-ci.com/papb/json-excel) |
| 2 | + |
| 3 | +> Create a pretty Excel table from JSON data with a very simple API |
| 4 | +
|
| 5 | + |
| 6 | +## Highlights |
| 7 | + |
| 8 | +* Pretty output |
| 9 | +* Intelligently auto-fits cell sizes by default |
| 10 | +* Checks for [Excel limitations](https://support.microsoft.com/en-ie/office/excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3) automatically (such as maximum cell length) and throws helpful errors if any limit is exceeded |
| 11 | +* Get the *Format as Table* Excel styling, with filterable headers, by simply enabling an option |
| 12 | +* Written in TypeScript (you get autocomplete suggestions in your IDE!) |
| 13 | + |
| 14 | + |
| 15 | +## Install |
| 16 | + |
| 17 | +``` |
| 18 | +$ npm install json-excel |
| 19 | +``` |
| 20 | + |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +```js |
| 25 | +const jsonToExcel = require('json-excel'); |
| 26 | + |
| 27 | +(async () => { |
| 28 | + await jsonToExcel([ |
| 29 | + { |
| 30 | + sheetName: 'Hello World', |
| 31 | + data: [ |
| 32 | + ['Foo', 'Bar', 'Baz'], |
| 33 | + ['A large string here but with\none line break', 'Hi', 'Test'], |
| 34 | + [ |
| 35 | + '\'starting single quote\nis rendered normally', |
| 36 | + 'Lots\nof\nline\nbreaks', |
| 37 | + 'Auto-fits cells with a little extra margin' |
| 38 | + ], |
| 39 | + ['Nice!', '', 'Quick and to the point!'] |
| 40 | + ], |
| 41 | + formatAsTable: true |
| 42 | + } |
| 43 | + ], 'example.xlsx', { overwrite: true }); |
| 44 | +})(); |
| 45 | +``` |
| 46 | + |
| 47 | +Output is an excel file called `example.xlsx` with a single sheet (called `Hello World`) and the following content: |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +## API |
| 53 | + |
| 54 | +<!-- Ensure this part is consistent with ./types.ts and ./defaults.ts --> |
| 55 | + |
| 56 | +### jsonToExcel(jsonSheets, destinationPath, options?) |
| 57 | + |
| 58 | +Async function that creates a xlsx file with the provided data. |
| 59 | + |
| 60 | +#### jsonSheets |
| 61 | + |
| 62 | +Type: `object[]` |
| 63 | + |
| 64 | +An array of objects, each representing one sheet, with: |
| 65 | + |
| 66 | +* `sheetName` (`string`, required): The name of the Worksheet (shown in the sheet tab in the bottom in Excel). |
| 67 | +* `data` (`string[][]`, required): The data to be populated in the Worksheet. |
| 68 | +* `formatAsTable` (`boolean`, optional, default `false`): Whether or not to enable the *"Format as Table"* styling, like in the above example. This will enable striped rows and filter arrows on all headers. |
| 69 | +* `tableTheme` (`string`, optional, default `'TableStyleMedium9'`): Which theme to use when formatting as table. This option is ignored if `formatAsTable` is `false`. The default value corresponds to the one from the screenshot above (medium blue). The list of supported themes is shown right in your IDE via autocomplete suggestions to this option. The autocomplete works even if you are not using TypeScript! |
| 70 | +* `autoTrimWhitespace` (`boolean`, optional, default `true`): Whether or not to automatically remove leading and trailing whitespace from each cell. Having this enabled is great to make the cell content alignment be consistent with what is visible. |
| 71 | +* `autoFitCellSizes` (`boolean`, optional, default `true`): Whether or not to automatically calculate best widths for every column and best heights for every row. |
| 72 | +* `autoFitCellSizesOptions` (`object`, optional): Extra options for configuring the behavior of the auto-fitting of cell sizes: |
| 73 | + * `minHeight` (`number`, optional, default `15`): The minimum height (in *"excel points"*) for every row. |
| 74 | + * `maxHeight` (`number`, optional, default `408`): The maximum height (in *"excel points"*) for every row. Cannot be greater than 408 (this is an Excel limitation). |
| 75 | + * `minWidth` (`number`, optional, default `6`): The minimum width (in *"excel points"*) for every column. |
| 76 | + * `maxWidth` (`number`, optional, default `170`): The maximum width (in *"excel points"*) for every column. Cannot be greater than 254 (this is an Excel limitation). |
| 77 | + * `horizontalPadding` (`number`, optional, default `3`): Extra horizontal padding (in *"excel points"*) for every column. This amount will be added to the auto-calculated minimal width in which the contents fit. |
| 78 | + * `verticalPadding` (`number`, optional, default `2`): Extra vertical padding (in *"excel points"*) for every cell. This amount will be added to the auto-calculated minimal height in which the contents fit. |
| 79 | + |
| 80 | +#### destinationPath |
| 81 | + |
| 82 | +Type: `string` |
| 83 | + |
| 84 | +The path (absolute, or relative to `process.cwd()`) in which the new xlsx file should be created. In windows, both `/` and `\` are accepted as path separators. |
| 85 | + |
| 86 | +#### options |
| 87 | + |
| 88 | +Type: `object` |
| 89 | + |
| 90 | +##### overwrite |
| 91 | + |
| 92 | +Type: `boolean` |
| 93 | +Default: `false` |
| 94 | + |
| 95 | +Whether or not to overwrite the destination file if it already exists. |
| 96 | + |
| 97 | + |
| 98 | +## Tip: usage with `object[]` instead of `string[][]` |
| 99 | + |
| 100 | +If, instead of directly tabular data, you have a list of objects such as... |
| 101 | + |
| 102 | +```js |
| 103 | +const data = [ |
| 104 | + { name: 'Grape', size: 'small' }, |
| 105 | + { name: 'Watermelon', size: 'big' }, |
| 106 | + { name: 'Apple', size: 'medium' } |
| 107 | +]; |
| 108 | +``` |
| 109 | + |
| 110 | +...you can use `jsonToExcel` by simply converting that to a `string[][]` first, with a simple loop. Example: |
| 111 | + |
| 112 | +```js |
| 113 | +const headers = ['Name', 'Size']; |
| 114 | +const dataAs2DArray = data.map(fruit => [fruit.name, fruit.size]); |
| 115 | + |
| 116 | +jsonToExcel([{ |
| 117 | + sheetName: 'Fruits', |
| 118 | + data: [ |
| 119 | + headers, |
| 120 | + ...dataAs2DArray |
| 121 | + ], |
| 122 | + formatAsTable: true |
| 123 | +}], 'fruits.xlsx'); |
| 124 | +``` |
| 125 | + |
| 126 | + |
| 127 | +## License |
| 128 | + |
| 129 | +MIT © [Pedro Augusto de Paula Barbosa](https://github.com/papb) |
0 commit comments