Skip to content

Commit

Permalink
Merge pull request #23 from lowdefy/community-plugin-xlsx
Browse files Browse the repository at this point in the history
feat: Add community-plugin-xlsx.
  • Loading branch information
SamTolmay authored Oct 17, 2023
2 parents 2b90372 + d6618de commit 973725e
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/seven-phones-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lowdefy/community-plugin-xlsx': major
'@lowdefy/community-plugin-docs': minor
---

Added community-plugin-xlsx.
186 changes: 186 additions & 0 deletions apps/docs/community-plugin-xlsx/DownloadXlsx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
_ref:
path: templates/general.yaml.njk
vars:
pageId: DownloadXlsx
pageTitle: DownloadXlsx
section: '@lowdefy/community-plugin-xlsx'
content:
- id: markdown
type: MarkdownWithCode
properties:
content: |
The `DownloadXlsx` action implements [write-excel-file](https://www.npmjs.com/package/write-excel-file) for writing `*.xlsx` files.
- _ref:
path: templates/plugin_usage.yaml
vars:
name: '@lowdefy/community-plugin-xlsx'
packageJsonPath: ../../plugins/community-plugin-xlsx/package.json
- id: xlsx_button
type: Button
style:
margin: 40px
maxWidth: 300
properties:
title: Excel Download
icon: AiOutlineDownload
size: large
block: true
shape: round
type: default
events:
onClick:
- id: download_xlsx
type: DownloadXlsx
params:
fileName: download.xlsx
dateFormat: 'DD/MM/YYYY'
data:
- Category: a
Amount: 10
Date:
_date: 2023-01-26
- Category: b
Amount: 20
Date:
_date: 2023-01-27
- Category: c
Amount: 30
Date:
_date: 2023-01-28
schema:
- column: Category Name
value: Category
type: String
width: 20
- column: Amount Made
value: Amount
type: Number
width: 20
- column: Date Created
value: Date
type: Date
width: 20
- id: markdown
type: MarkdownWithCode
properties:
content: |
#### Properties
- `data: object[]`: An array of rows containing data.
- `fileName: string`: The name the file will be saved as when downloaded.
- `schema: object[]`: A schema to describe each column.
- `dateFormat: string`: The format of dates in the file.
See [write-excel-file](https://www.npmjs.com/package/write-excel-file) for additional properties.
### Examples
###### Defined data.
```yaml
id: xlsx_button
type: Button
properties:
title: Excel Download
events:
onClick:
- id: download_xlsx
type: DownloadXlsx
params:
fileName: download.xlsx
dateFormat: 'DD/MM/YYYY'
data:
- Category: a
Amount: 10
Date:
_date: 2023-01-26
- Category: b
Amount: 20
Date:
_date: 2023-01-27
- Category: c
Amount: 30
Date:
_date: 2023-01-28
schema:
- column: Category Name
value: Category
type: String
width: 20
- column: Amount Made
value: Amount
type: Number
width: 20
- column: Date Created
value: Date
type: Date
width: 20
```
###### Request data.
```yaml
id: xlsx_button
type: Button
properties:
title: Excel Download
events:
onClick:
- id: get_data
type: Request
params: get_data
- id: download_xlsx
type: DownloadXlsx
params:
fileName: download.xlsx
dateFormat: 'DD/MM/YYYY'
data:
_request: get_data
schema:
- column: Category Name
value: Category
type: String
width: 20
- column: Amount Made
value: Amount
type: Number
width: 20
- column: Date Created
value: Date
type: Date
width: 20
```
###### Schema columns with functions.
```yaml
id: xlsx_button
type: Button
properties:
title: Excel Download
events:
onClick:
- id: get_data
type: Request
params: get_data
- id: download_xlsx
type: DownloadXlsx
params:
fileName: download.xlsx
dateFormat: 'DD/MM/YYYY'
data:
_request: get_data
schema:
- column: Category Name
value:
_function:
__args: 0.Category
type: String
width: 20
- column: Amount Made
value:
_function:
__args: 0.Amount
type: Number
width: 20
- column: Date Created
value: Date
type: Date
width: 20
```
3 changes: 3 additions & 0 deletions apps/docs/lowdefy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ plugins:
version: 'workspace:*'
- name: '@lowdefy/community-plugin-confetti'
version: 'workspace:*'
- name: '@lowdefy/community-plugin-xlsx'
version: 'workspace:*'

menus:
_ref: menus.yaml
Expand All @@ -18,3 +20,4 @@ pages:
- _ref: community-plugin-confetti/Confetti.yaml
- _ref: community-plugin-nodemailer/EmailProvider.yaml
- _ref: community-plugin-mongodb/MongoDB.yaml
- _ref: community-plugin-xlsx/DownloadXlsx.yaml
5 changes: 5 additions & 0 deletions apps/docs/menus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@
pageId: community-plugin-mongodb
properties:
title: MongoDB
- id: community-plugin-xlsx
type: MenuLink
pageId: DownloadXlsx
properties:
title: DownloadXlsx
28 changes: 28 additions & 0 deletions plugins/community-plugin-xlsx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@lowdefy/community-plugin-xlsx",
"version": "0.0.0",
"license": "MIT",
"type": "module",
"exports": {
"./actions": "./dist/actions.js",
"./types": "./dist/types.js"
},
"files": [
"dist/*"
],
"scripts": {
"build": "swc src --out-dir dist --config-file ../../.swcrc --delete-dir-on-start --copy-files",
"prepare": "pnpm build"
},
"dependencies": {
"@lowdefy/helpers": "4.0.0-rc.10",
"write-excel-file": "1.4.27"
},
"devDependencies": {
"@swc/cli": "0.1.57",
"@swc/core": "1.2.194"
},
"publishConfig": {
"access": "public"
}
}
1 change: 1 addition & 0 deletions plugins/community-plugin-xlsx/src/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as DownloadXlsx } from './actions/DownloadXlsx.js';
33 changes: 33 additions & 0 deletions plugins/community-plugin-xlsx/src/actions/DownloadXlsx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { type } from '@lowdefy/helpers';
import writeXlsxFile from 'write-excel-file';

async function DownloadXlsx({ params }) {
const { data, fileName, schema, ...options } = params;

if (!type.isArray(data) || !type.isObject(data[0])) {
throw new Error('Data should be an array of objects.');
}

const colTypes = {
String: String,
Number: Number,
Boolean: Boolean,
Date: Date,
};

await writeXlsxFile(data, {
fileName: !type.isString(fileName) ? 'download.xlsx' : fileName,
schema:
type.isArray(schema) &&
schema.map((column) => ({
...column,
value: type.isString(column.value) ? (row) => row[column.value] : column.value,
type: column.type ? colTypes[column.type] : undefined,
})),
...options,
});

return;
}

export default DownloadXlsx;
5 changes: 5 additions & 0 deletions plugins/community-plugin-xlsx/src/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as actions from './actions.js';

export default {
actions: Object.keys(actions),
};

0 comments on commit 973725e

Please sign in to comment.