From c5f33bf1aa00bba11fb58796f2128d9c8ab06839 Mon Sep 17 00:00:00 2001 From: Ioannis Ktistakis <47716028+Yianni99@users.noreply.github.com> Date: Mon, 16 Oct 2023 16:38:14 +0200 Subject: [PATCH 1/2] feat: Add community-plugin-xlsx. --- .changeset/seven-phones-build.md | 6 + .../community-plugin-xlsx/DownloadXlsx.yaml | 186 ++++++++++++++++++ apps/docs/lowdefy.yaml | 3 + apps/docs/menus.yaml | 5 + plugins/community-plugin-xlsx/package.json | 28 +++ plugins/community-plugin-xlsx/src/actions.js | 1 + .../src/actions/DownloadXlsx.js | 45 +++++ plugins/community-plugin-xlsx/src/types.js | 5 + 8 files changed, 279 insertions(+) create mode 100644 .changeset/seven-phones-build.md create mode 100644 apps/docs/community-plugin-xlsx/DownloadXlsx.yaml create mode 100644 plugins/community-plugin-xlsx/package.json create mode 100644 plugins/community-plugin-xlsx/src/actions.js create mode 100644 plugins/community-plugin-xlsx/src/actions/DownloadXlsx.js create mode 100644 plugins/community-plugin-xlsx/src/types.js 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..eb6e6e6 --- /dev/null +++ b/plugins/community-plugin-xlsx/src/actions/DownloadXlsx.js @@ -0,0 +1,45 @@ +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.'); + } + + await writeXlsxFile(data, { + fileName: !type.isString(fileName) ? 'download.xlsx' : fileName, + schema: + type.isArray(schema) && + schema.map((column) => { + let colType; + switch (column.type) { + case 'String': + colType = String; + break; + case 'Number': + colType = Number; + break; + case 'Boolean': + colType = Boolean; + break; + case 'Date': + colType = Date; + break; + default: + break; + } + return { + ...column, + value: type.isString(column.value) ? (row) => row[column.value] : column.value, + type: colType, + }; + }), + ...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), +}; From d6618de9facb51affed554c72901420fe2a2f93b Mon Sep 17 00:00:00 2001 From: Ioannis Ktistakis <47716028+Yianni99@users.noreply.github.com> Date: Tue, 17 Oct 2023 09:32:28 +0200 Subject: [PATCH 2/2] feat: Abbreviate DownloadXlsx logic. --- .../src/actions/DownloadXlsx.js | 36 +++++++------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/plugins/community-plugin-xlsx/src/actions/DownloadXlsx.js b/plugins/community-plugin-xlsx/src/actions/DownloadXlsx.js index eb6e6e6..2e57fa5 100644 --- a/plugins/community-plugin-xlsx/src/actions/DownloadXlsx.js +++ b/plugins/community-plugin-xlsx/src/actions/DownloadXlsx.js @@ -8,34 +8,22 @@ async function DownloadXlsx({ params }) { 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) => { - let colType; - switch (column.type) { - case 'String': - colType = String; - break; - case 'Number': - colType = Number; - break; - case 'Boolean': - colType = Boolean; - break; - case 'Date': - colType = Date; - break; - default: - break; - } - return { - ...column, - value: type.isString(column.value) ? (row) => row[column.value] : column.value, - type: colType, - }; - }), + schema.map((column) => ({ + ...column, + value: type.isString(column.value) ? (row) => row[column.value] : column.value, + type: column.type ? colTypes[column.type] : undefined, + })), ...options, });