diff --git a/.changeset/seven-phones-build.md b/.changeset/seven-phones-build.md new file mode 100644 index 0000000..cd92a85 --- /dev/null +++ b/.changeset/seven-phones-build.md @@ -0,0 +1,6 @@ +--- +'@lowdefy/community-plugin-xlsx': major +'@lowdefy/community-plugin-docs': minor +--- + +Added community-plugin-xlsx. diff --git a/apps/docs/community-plugin-xlsx/DownloadXlsx.yaml b/apps/docs/community-plugin-xlsx/DownloadXlsx.yaml new file mode 100644 index 0000000..01c1a72 --- /dev/null +++ b/apps/docs/community-plugin-xlsx/DownloadXlsx.yaml @@ -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 + ``` diff --git a/apps/docs/lowdefy.yaml b/apps/docs/lowdefy.yaml index 5cd0e86..4fc2c01 100644 --- a/apps/docs/lowdefy.yaml +++ b/apps/docs/lowdefy.yaml @@ -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 @@ -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 diff --git a/apps/docs/menus.yaml b/apps/docs/menus.yaml index 32833ca..8d882f8 100644 --- a/apps/docs/menus.yaml +++ b/apps/docs/menus.yaml @@ -20,3 +20,8 @@ pageId: community-plugin-mongodb properties: title: MongoDB + - id: community-plugin-xlsx + type: MenuLink + pageId: DownloadXlsx + properties: + title: DownloadXlsx diff --git a/plugins/community-plugin-xlsx/package.json b/plugins/community-plugin-xlsx/package.json new file mode 100644 index 0000000..d4b59e4 --- /dev/null +++ b/plugins/community-plugin-xlsx/package.json @@ -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" + } +} diff --git a/plugins/community-plugin-xlsx/src/actions.js b/plugins/community-plugin-xlsx/src/actions.js new file mode 100644 index 0000000..961209f --- /dev/null +++ b/plugins/community-plugin-xlsx/src/actions.js @@ -0,0 +1 @@ +export { default as DownloadXlsx } from './actions/DownloadXlsx.js'; diff --git a/plugins/community-plugin-xlsx/src/actions/DownloadXlsx.js b/plugins/community-plugin-xlsx/src/actions/DownloadXlsx.js new file mode 100644 index 0000000..2e57fa5 --- /dev/null +++ b/plugins/community-plugin-xlsx/src/actions/DownloadXlsx.js @@ -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; diff --git a/plugins/community-plugin-xlsx/src/types.js b/plugins/community-plugin-xlsx/src/types.js new file mode 100644 index 0000000..a85e72e --- /dev/null +++ b/plugins/community-plugin-xlsx/src/types.js @@ -0,0 +1,5 @@ +import * as actions from './actions.js'; + +export default { + actions: Object.keys(actions), +};