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

POC: Export to CSV, XLSX, PDF #2355

Merged
merged 9 commits into from
Apr 13, 2021
Merged

POC: Export to CSV, XLSX, PDF #2355

merged 9 commits into from
Apr 13, 2021

Conversation

amanmahajan7
Copy link
Contributor

No description provided.

@amanmahajan7 amanmahajan7 self-assigned this Apr 9, 2021
@amanmahajan7 amanmahajan7 changed the title POC: Export to CSV POC: Export to CSV, XLSX, PDF Apr 11, 2021
@@ -208,6 +209,7 @@ function DataGrid<R, SR>({
// Toggles and modes
enableFilterRow = false,
cellNavigationMode = 'NONE',
enableVirtualization = true,
Copy link
Contributor Author

@amanmahajan7 amanmahajan7 Apr 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the prop name. Any suggessions? disableVirtualization makes more sense but I kept this to be consistent with enableFilterRow . We can also consider forwarding this prop to the formatters to users can use it during printing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prop name is good to me. I prefer avoiding "negative" prop/variable names as they make reading logic harder.

We can also consider forwarding this prop to the formatters to users can use it during printing.

What use case do you have in mind?

Copy link
Contributor Author

@amanmahajan7 amanmahajan7 Apr 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If for example there is a cell that shows a complex data like an icon with a tooltip. It may not work out of the box and it would be helpful to provide an alternate formatter for printing. We can revisit if needed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like it should be a separate thing, and it could be implemented by the user via a provider.

}


export function exportToPdf<R, SR>(gridElement: ReactElement<DataGridProps<R, SR>>, fileName: string) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/simonbengtsson/jsPDF-AutoTable
Handy plugin to convert table to pdf

downloadFile(fileName, new Blob([content], { type: 'text/csv;charset=utf-8;' }));
}

export function exportToXlsx<R, SR>(gridElement: ReactElement<DataGridProps<R, SR>>, fileName: string) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/SheetJS/sheetjs
We can use the commercial version

}

function getGridContent<R, SR>(gridElement: ReactElement<DataGridProps<R, SR>>) {
const grid = document.createElement('content');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to grab the text content of each cell and create an array of arrays. Both pdf and xlsx libraries handle array of arrays

@amanmahajan7 amanmahajan7 marked this pull request as ready for review April 12, 2021 12:15
@@ -208,6 +209,7 @@ function DataGrid<R, SR>({
// Toggles and modes
enableFilterRow = false,
cellNavigationMode = 'NONE',
enableVirtualization = true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prop name is good to me. I prefer avoiding "negative" prop/variable names as they make reading logic harder.

We can also consider forwarding this prop to the formatters to users can use it during printing.

What use case do you have in mind?

src/hooks/useViewportColumns.ts Outdated Show resolved Hide resolved
src/hooks/useViewportRows.ts Outdated Show resolved Hide resolved
stories/demos/exportUtils.tsx Outdated Show resolved Hide resolved
).map(gridRow => {
return Array.from(
gridRow.querySelectorAll<HTMLDivElement>('.rdg-cell')
).map(gridCell => gridCell.innerText);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the cell ends up empty when we display an image, a checkbox, or a progress element for example?

Copy link
Contributor Author

@amanmahajan7 amanmahajan7 Apr 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. We can manually update cell value during export or provide a flag to the formatter so it can return a print friendly value


function getGridContent<R, SR>(gridElement: ReactElement<DataGridProps<R, SR>>) {
const grid = document.createElement('content');
grid.innerHTML = renderToStaticMarkup(cloneElement(gridElement, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're rendering images, it might end up downloading a lot of images at once.
We could avoid this by rendering the grid in an anonymous document: https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createDocument

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know. We can add it if needed or I will create a followup PR

}
}

function serialiseCellValue(value: unknown) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we handle \n as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can. It works for now so may be next PR if needed

stories/demos/exportUtils.tsx Outdated Show resolved Hide resolved
amanmahajan7 and others added 4 commits April 12, 2021 14:35
Co-authored-by: Nicolas Stepien <567105+nstepien@users.noreply.github.com>
Co-authored-by: Nicolas Stepien <567105+nstepien@users.noreply.github.com>
Co-authored-by: Nicolas Stepien <567105+nstepien@users.noreply.github.com>
Co-authored-by: Nicolas Stepien <567105+nstepien@users.noreply.github.com>
@amanmahajan7 amanmahajan7 merged commit 2d64182 into canary Apr 13, 2021
@amanmahajan7 amanmahajan7 deleted the am-export-csv branch April 13, 2021 12:08
gernotkogler pushed a commit to Garaio-REM/react-data-grid that referenced this pull request May 13, 2021
* Initial commit

* Fix dependencies

* Add utility function to export

* serialiseCellValue

* Export to pdf and xlsx

* Update src/hooks/useViewportColumns.ts

Co-authored-by: Nicolas Stepien <567105+nstepien@users.noreply.github.com>

* Update src/hooks/useViewportRows.ts

Co-authored-by: Nicolas Stepien <567105+nstepien@users.noreply.github.com>

* Update stories/demos/exportUtils.tsx

Co-authored-by: Nicolas Stepien <567105+nstepien@users.noreply.github.com>

* Update stories/demos/exportUtils.tsx

Co-authored-by: Nicolas Stepien <567105+nstepien@users.noreply.github.com>

Co-authored-by: Nicolas Stepien <567105+nstepien@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants