Skip to content

Commit 5d5578c

Browse files
Better docs for new npm release with ts
1 parent aceb6ba commit 5d5578c

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

README.md

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,61 @@ xlsx(data, settings) // Will download the excel file
5050

5151
### TypeScript
5252

53+
Here is an example of a server setup using TS, thanks to @elyse0 for the contribution
54+
5355
```ts
54-
const jsonSheets: IJsonSheet[] = [{
55-
sheet: "Friends",
56-
columns: [
57-
{ label: "Name", value: "name" },
58-
{ label: "Username", value: "username" }
59-
],
60-
content: [
61-
{ name: "Andreas", username: "andr34s" }
62-
]
63-
}]
64-
65-
xlsx(jsonSheets)
56+
import xlsx, { IJsonSheet, ISettings } from 'json-as-xlsx'
57+
import express from 'express'
58+
59+
const app = express()
60+
app.use(express.json())
61+
app.use(express.urlencoded({ extended: true }))
62+
63+
const data: IJsonSheet[] = [
64+
{
65+
sheet: 'Adults',
66+
columns: [
67+
{ label: 'User', value: 'user' },
68+
{ label: 'Age', value: 'age' }
69+
],
70+
content: [
71+
{ user: 'Andrea', age: 20, more: { phone: '11111111' } },
72+
{ user: 'Luis', age: 21, more: { phone: '12345678' } }
73+
]
74+
}, {
75+
sheet: 'Children',
76+
columns: [
77+
{ label: 'User', value: 'user' },
78+
{ label: 'Age', value: 'age' }
79+
],
80+
content: [
81+
{ user: 'Manuel', age: 16, more: { phone: '99999999' } },
82+
{ user: 'Ana', age: 17, more: { phone: '87654321' } }
83+
]
84+
}
85+
]
86+
87+
const settings: ISettings = {
88+
writeOptions: {
89+
type: 'buffer',
90+
bookType: 'xlsx'
91+
}
92+
}
93+
94+
app.get('/', (_, res) => {
95+
const buffer = xlsx(data, settings)
96+
res.writeHead(200, {
97+
'Content-Type': 'application/octet-stream',
98+
'Content-disposition': 'attachment; filename=MySheet.xlsx'
99+
})
100+
res.end(buffer)
101+
})
102+
103+
const port = process.env.PORT ?? 3000
104+
105+
app.listen(port, () => {
106+
console.log(`Your app is listening on port ${port}`)
107+
})
66108
```
67109

68110
## Examples

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-as-xlsx",
3-
"version": "2.2.1",
3+
"version": "2.2.2",
44
"main": "index.js",
55
"license": "MIT",
66
"description": "Create excel from json",

0 commit comments

Comments
 (0)