-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathExcelFile.js
More file actions
129 lines (103 loc) · 4.02 KB
/
ExcelFile.js
File metadata and controls
129 lines (103 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import React from "react";
import PropTypes from "prop-types";
import {saveAs} from "file-saver";
import XLSX from "tempa-xlsx";
import _ from 'lodash';
import ExcelSheet from "../elements/ExcelSheet";
import {strToArrBuffer, excelSheetFromAoA, excelSheetFromDataSet} from "../utils/DataUtil";
class ExcelFile extends React.Component {
fileExtensions = ['xlsx', 'xls', 'csv', 'txt', 'html'];
defaultFileExtension = 'xlsx';
static props = {
hideElement: PropTypes.bool,
filename: PropTypes.string,
fileExtension: PropTypes.string,
element: PropTypes.any,
children: function (props, propName, componentName) {
React.Children.forEach(props[propName], child => {
if (child.type !== ExcelSheet) {
throw new Error('<ExcelFile> can only have <ExcelSheet> as children. ');
}
});
}
};
static defaultProps = {
hideElement: false,
filename: "Download",
fileExtension: "xlsx",
element: <button>Download</button>
};
constructor(props) {
super(props);
if (this.props.hideElement) {
this.download();
} else {
this.handleDownload = this.download.bind(this);
}
this.createSheetData = this.createSheetData.bind(this);
}
createSheetData(sheet) {
const columns = sheet.props.children;
const sheetData = [React.Children.map(columns, column => column.props.label)];
const data = typeof (sheet.props.data) === 'function' ? sheet.props.data() : sheet.props.data;
data.forEach(row => {
const sheetRow = [];
React.Children.forEach(columns, column => {
const getValue = typeof (column.props.value) === 'function' ? column.props.value : row => _.get(row, column.props.value);
const itemValue = getValue(row);
sheetRow.push(isNaN(itemValue) ? (itemValue || '') : itemValue);
});
sheetData.push(sheetRow);
});
return sheetData;
}
download() {
const wb = {
SheetNames: React.Children.map(this.props.children, sheet => sheet.props.name),
Sheets: {}
};
React.Children.forEach(this.props.children, sheet => {
if (typeof sheet.props.dataSet === 'undefined' || sheet.props.dataSet.length === 0) {
wb.Sheets[sheet.props.name] = excelSheetFromAoA(this.createSheetData(sheet));
} else {
wb.Sheets[sheet.props.name] = excelSheetFromDataSet(sheet.props.dataSet);
}
});
const fileExtension = this.getFileExtension();
const fileName = this.getFileName();
const wbout = XLSX.write(wb, {bookType: fileExtension, bookSST: true, type: 'binary'});
saveAs(new Blob([strToArrBuffer(wbout)], {type: "application/octet-stream"}), fileName);
}
getFileName() {
if (this.props.filename === null || typeof this.props.filename !== 'string') {
throw Error('Invalid file name provided');
}
return this.getFileNameWithExtension(this.props.filename, this.getFileExtension());
}
getFileExtension() {
let extension = this.props.fileExtension;
if (extension.length === 0) {
const slugs = this.props.filename.split('.');
if (slugs.length === 0) {
throw Error('Invalid file name provided');
}
extension = slugs[slugs.length - 1];
}
if (this.fileExtensions.indexOf(extension) !== -1) {
return extension;
}
return this.defaultFileExtension;
}
getFileNameWithExtension(filename, extension) {
return `${filename}.${extension}`;
}
render() {
const { hideElement, element } = this.props;
if (hideElement) {
return null;
} else {
return (<span onClick={this.handleDownload}>{element}</span>);
}
}
}
export default ExcelFile;