Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use dynamic import and make api async #16

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ Addon that encapsulates ability to render a data set as either excel or csv.

Forked from [roofstock/ember-cli-data-export](https://github.com/roofstock/ember-cli-data-export).

First you need to install your own copy of xlsx and file-saver, these are peerDependencies to let you choose the version.

In order to see which peerDependencies are needed you can run the command

```
npm info ember-spreadsheet-export peerDependencies
```

Differences from `ember-cli-data-export` include:
------------------------------------------------------------------------------
Expand Down
23 changes: 18 additions & 5 deletions addon/services/excel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Service from '@ember/service';
import { saveAs } from 'file-saver';
import XLSX from 'xlsx';
import optionize from '../utils/utils';

const defaultConfig = {
Expand All @@ -12,8 +11,17 @@ const defaultConfig = {
tableParseOptions: {},
};

export async function loadXLSX() {
return import('xlsx');
}

export default class ExcelService extends Service {
export(data, options) {
async XLSX() {
return loadXLSX();
}

async export(data, options) {
const XLSX = await this.XLSX();
options = optionize(options, defaultConfig);

function s2ab(s) {
Expand Down Expand Up @@ -57,8 +65,9 @@ export default class ExcelService extends Service {
if (range.e.c < C) {
range.e.c = C;
}
let cell = { v: data[R][C] };
if (cell.v == null) {
let cellValue = data[R][C];
let cell = { v: cellValue };
if (cellValue.v == null) {
betocantu93 marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
let cell_ref = XLSX.utils.encode_cell({ c: C, r: R });
Expand All @@ -75,9 +84,13 @@ export default class ExcelService extends Service {
cell.t = 's';
}

//support passing more cell options
cell = options.callbackPerCell?.(cellValue, cell) || cell;

ws[cell_ref] = cell;
}
}

if (range.s.c < 10000000) {
ws['!ref'] = XLSX.utils.encode_range(range);
}
Expand Down Expand Up @@ -119,7 +132,7 @@ export default class ExcelService extends Service {
});

let output = new Blob([s2ab(wbout)], { type: 'application/octet-stream' });

if (options.download) {
saveAs(output, options.fileName);
}
Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
module.exports = {
name: require('./package').name,

included(app) {
this._super.included.apply(this, arguments);
app.import('vendor/Blob.js');
options: {
babel: {
plugins: [require.resolve('ember-auto-import/babel-plugin')],
},
},
};
Loading