Skip to content

Commit

Permalink
Merge pull request #50 from lowdefy/excel-download-feature
Browse files Browse the repository at this point in the history
Excel download feature
  • Loading branch information
SamTolmay authored Aug 8, 2024
2 parents 3aee482 + 36b3f68 commit 422e4ed
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 93 deletions.
8 changes: 8 additions & 0 deletions .changeset/rude-seas-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@lowdefy/community-plugin-xlsx': minor
---

- Added functionality for dot-notation in the value field.
- Added functionality for Array types.
- Added error if schema param is not defined.
- Updated dependency write-excel-file to v2.0.5.
26 changes: 26 additions & 0 deletions apps/docs/community-plugin-xlsx/DownloadXlsx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,22 @@ _ref:
Amount: 10
Date:
_date: 2023-01-26
Tags:
- 1
- 2
- Category: b
Amount: 20
Date:
_date: 2023-01-27
Tags:
- 2
- Category: c
Amount: 30
Date:
_date: 2023-01-28
Tags:
- 2
- 4
schema:
- column: Category Name
value: Category
Expand All @@ -60,6 +68,10 @@ _ref:
value: Date
type: Date
width: 20
- column: Tags
value: Tags
type: Array
width: 20
- id: markdown
type: MarkdownWithCode
properties:
Expand All @@ -72,6 +84,8 @@ _ref:
See [write-excel-file](https://www.npmjs.com/package/write-excel-file) for additional properties.
An `Array` type is added that joins the array separated by commas.
### Examples
###### Defined data.
Expand All @@ -92,14 +106,22 @@ _ref:
Amount: 10
Date:
_date: 2023-01-26
Tags:
- 1
- 2
- Category: b
Amount: 20
Date:
_date: 2023-01-27
Tags:
- 2
- Category: c
Amount: 30
Date:
_date: 2023-01-28
Tags:
- 2
- 4
schema:
- column: Category Name
value: Category
Expand All @@ -113,6 +135,10 @@ _ref:
value: Date
type: Date
width: 20
- column: Tags
value: Tags
type: Array
width: 20
```
###### Request data.
Expand Down
2 changes: 1 addition & 1 deletion plugins/community-plugin-xlsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"dependencies": {
"@lowdefy/helpers": "^4.0.0-rc.12",
"write-excel-file": "1.4.27"
"write-excel-file": "2.0.5"
},
"devDependencies": {
"@swc/cli": "0.1.62",
Expand Down
31 changes: 20 additions & 11 deletions plugins/community-plugin-xlsx/src/actions/DownloadXlsx.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
import { type } from '@lowdefy/helpers';
import { type, get } from '@lowdefy/helpers';
import writeXlsxFile from 'write-excel-file';

function createValueFunction(column) {
if (type.isFunction(column.value)) {
return column.value;
}
if (column.type === 'Array') {
return (row) => get(row, column.value)?.filter(Boolean).join(', ');
}
return (row) => get(row, column.value);
}

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.');
}

if (!type.isArray(schema) || !type.isObject(schema[0])) {
throw new Error('Schema should be an array of objects.');
}
const colTypes = {
String: String,
Number: Number,
Boolean: Boolean,
Date: Date,
Array: String,
};

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: type.isString(column.type) ? colTypes[column.type] ?? column.type : undefined,
})),
schema: schema.map((column) => ({
...column,
value: createValueFunction(column),
type: get(colTypes, column.type) ?? column.type,
})),
...options,
});

return;
}

Expand Down
Loading

0 comments on commit 422e4ed

Please sign in to comment.