Skip to content

Commit 973725e

Browse files
authored
Merge pull request #23 from lowdefy/community-plugin-xlsx
feat: Add community-plugin-xlsx.
2 parents 2b90372 + d6618de commit 973725e

File tree

8 files changed

+267
-0
lines changed

8 files changed

+267
-0
lines changed

.changeset/seven-phones-build.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@lowdefy/community-plugin-xlsx': major
3+
'@lowdefy/community-plugin-docs': minor
4+
---
5+
6+
Added community-plugin-xlsx.
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
_ref:
2+
path: templates/general.yaml.njk
3+
vars:
4+
pageId: DownloadXlsx
5+
pageTitle: DownloadXlsx
6+
section: '@lowdefy/community-plugin-xlsx'
7+
content:
8+
- id: markdown
9+
type: MarkdownWithCode
10+
properties:
11+
content: |
12+
The `DownloadXlsx` action implements [write-excel-file](https://www.npmjs.com/package/write-excel-file) for writing `*.xlsx` files.
13+
- _ref:
14+
path: templates/plugin_usage.yaml
15+
vars:
16+
name: '@lowdefy/community-plugin-xlsx'
17+
packageJsonPath: ../../plugins/community-plugin-xlsx/package.json
18+
- id: xlsx_button
19+
type: Button
20+
style:
21+
margin: 40px
22+
maxWidth: 300
23+
properties:
24+
title: Excel Download
25+
icon: AiOutlineDownload
26+
size: large
27+
block: true
28+
shape: round
29+
type: default
30+
events:
31+
onClick:
32+
- id: download_xlsx
33+
type: DownloadXlsx
34+
params:
35+
fileName: download.xlsx
36+
dateFormat: 'DD/MM/YYYY'
37+
data:
38+
- Category: a
39+
Amount: 10
40+
Date:
41+
_date: 2023-01-26
42+
- Category: b
43+
Amount: 20
44+
Date:
45+
_date: 2023-01-27
46+
- Category: c
47+
Amount: 30
48+
Date:
49+
_date: 2023-01-28
50+
schema:
51+
- column: Category Name
52+
value: Category
53+
type: String
54+
width: 20
55+
- column: Amount Made
56+
value: Amount
57+
type: Number
58+
width: 20
59+
- column: Date Created
60+
value: Date
61+
type: Date
62+
width: 20
63+
- id: markdown
64+
type: MarkdownWithCode
65+
properties:
66+
content: |
67+
#### Properties
68+
- `data: object[]`: An array of rows containing data.
69+
- `fileName: string`: The name the file will be saved as when downloaded.
70+
- `schema: object[]`: A schema to describe each column.
71+
- `dateFormat: string`: The format of dates in the file.
72+
73+
See [write-excel-file](https://www.npmjs.com/package/write-excel-file) for additional properties.
74+
75+
### Examples
76+
77+
###### Defined data.
78+
```yaml
79+
id: xlsx_button
80+
type: Button
81+
properties:
82+
title: Excel Download
83+
events:
84+
onClick:
85+
- id: download_xlsx
86+
type: DownloadXlsx
87+
params:
88+
fileName: download.xlsx
89+
dateFormat: 'DD/MM/YYYY'
90+
data:
91+
- Category: a
92+
Amount: 10
93+
Date:
94+
_date: 2023-01-26
95+
- Category: b
96+
Amount: 20
97+
Date:
98+
_date: 2023-01-27
99+
- Category: c
100+
Amount: 30
101+
Date:
102+
_date: 2023-01-28
103+
schema:
104+
- column: Category Name
105+
value: Category
106+
type: String
107+
width: 20
108+
- column: Amount Made
109+
value: Amount
110+
type: Number
111+
width: 20
112+
- column: Date Created
113+
value: Date
114+
type: Date
115+
width: 20
116+
```
117+
118+
###### Request data.
119+
```yaml
120+
id: xlsx_button
121+
type: Button
122+
properties:
123+
title: Excel Download
124+
events:
125+
onClick:
126+
- id: get_data
127+
type: Request
128+
params: get_data
129+
- id: download_xlsx
130+
type: DownloadXlsx
131+
params:
132+
fileName: download.xlsx
133+
dateFormat: 'DD/MM/YYYY'
134+
data:
135+
_request: get_data
136+
schema:
137+
- column: Category Name
138+
value: Category
139+
type: String
140+
width: 20
141+
- column: Amount Made
142+
value: Amount
143+
type: Number
144+
width: 20
145+
- column: Date Created
146+
value: Date
147+
type: Date
148+
width: 20
149+
```
150+
151+
###### Schema columns with functions.
152+
```yaml
153+
id: xlsx_button
154+
type: Button
155+
properties:
156+
title: Excel Download
157+
events:
158+
onClick:
159+
- id: get_data
160+
type: Request
161+
params: get_data
162+
- id: download_xlsx
163+
type: DownloadXlsx
164+
params:
165+
fileName: download.xlsx
166+
dateFormat: 'DD/MM/YYYY'
167+
data:
168+
_request: get_data
169+
schema:
170+
- column: Category Name
171+
value:
172+
_function:
173+
__args: 0.Category
174+
type: String
175+
width: 20
176+
- column: Amount Made
177+
value:
178+
_function:
179+
__args: 0.Amount
180+
type: Number
181+
width: 20
182+
- column: Date Created
183+
value: Date
184+
type: Date
185+
width: 20
186+
```

apps/docs/lowdefy.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ plugins:
1010
version: 'workspace:*'
1111
- name: '@lowdefy/community-plugin-confetti'
1212
version: 'workspace:*'
13+
- name: '@lowdefy/community-plugin-xlsx'
14+
version: 'workspace:*'
1315

1416
menus:
1517
_ref: menus.yaml
@@ -18,3 +20,4 @@ pages:
1820
- _ref: community-plugin-confetti/Confetti.yaml
1921
- _ref: community-plugin-nodemailer/EmailProvider.yaml
2022
- _ref: community-plugin-mongodb/MongoDB.yaml
23+
- _ref: community-plugin-xlsx/DownloadXlsx.yaml

apps/docs/menus.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@
2020
pageId: community-plugin-mongodb
2121
properties:
2222
title: MongoDB
23+
- id: community-plugin-xlsx
24+
type: MenuLink
25+
pageId: DownloadXlsx
26+
properties:
27+
title: DownloadXlsx
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "@lowdefy/community-plugin-xlsx",
3+
"version": "0.0.0",
4+
"license": "MIT",
5+
"type": "module",
6+
"exports": {
7+
"./actions": "./dist/actions.js",
8+
"./types": "./dist/types.js"
9+
},
10+
"files": [
11+
"dist/*"
12+
],
13+
"scripts": {
14+
"build": "swc src --out-dir dist --config-file ../../.swcrc --delete-dir-on-start --copy-files",
15+
"prepare": "pnpm build"
16+
},
17+
"dependencies": {
18+
"@lowdefy/helpers": "4.0.0-rc.10",
19+
"write-excel-file": "1.4.27"
20+
},
21+
"devDependencies": {
22+
"@swc/cli": "0.1.57",
23+
"@swc/core": "1.2.194"
24+
},
25+
"publishConfig": {
26+
"access": "public"
27+
}
28+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as DownloadXlsx } from './actions/DownloadXlsx.js';
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { type } from '@lowdefy/helpers';
2+
import writeXlsxFile from 'write-excel-file';
3+
4+
async function DownloadXlsx({ params }) {
5+
const { data, fileName, schema, ...options } = params;
6+
7+
if (!type.isArray(data) || !type.isObject(data[0])) {
8+
throw new Error('Data should be an array of objects.');
9+
}
10+
11+
const colTypes = {
12+
String: String,
13+
Number: Number,
14+
Boolean: Boolean,
15+
Date: Date,
16+
};
17+
18+
await writeXlsxFile(data, {
19+
fileName: !type.isString(fileName) ? 'download.xlsx' : fileName,
20+
schema:
21+
type.isArray(schema) &&
22+
schema.map((column) => ({
23+
...column,
24+
value: type.isString(column.value) ? (row) => row[column.value] : column.value,
25+
type: column.type ? colTypes[column.type] : undefined,
26+
})),
27+
...options,
28+
});
29+
30+
return;
31+
}
32+
33+
export default DownloadXlsx;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as actions from './actions.js';
2+
3+
export default {
4+
actions: Object.keys(actions),
5+
};

0 commit comments

Comments
 (0)