-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathcsv-helper.ts
174 lines (165 loc) · 7.14 KB
/
csv-helper.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { ValueFormatter } from './value-formatter';
/**
* CsvHelper class
* @private
*/
export class CsvHelper {
private isMicrosoftBrowser: boolean;
private buffer: Blob;
private csvStr: string;
private formatter: ValueFormatter;
private globalStyles: Map<string, string>;
private isServerRendered: boolean;
private separator: string;
/* tslint:disable:no-any */
constructor(json: any, separator: string) {
this.csvStr = '';
if (separator === null || separator === undefined) {
this.separator = ',';
} else {
this.separator = separator;
}
this.formatter = new ValueFormatter();
this.isMicrosoftBrowser = !(!navigator.msSaveBlob);
if (json.isServerRendered !== null && json.isServerRendered !== undefined) {
this.isServerRendered = json.isServerRendered;
}
if (json.styles !== null && json.styles !== undefined) {
this.globalStyles = new Map<string, string>();
for (let i: number = 0; i < json.styles.length; i++) {
if (json.styles[i].name !== undefined && json.styles[i].numberFormat !== undefined) {
this.globalStyles.set(json.styles[i].name, json.styles[i].numberFormat);
}
}
}
// Parses Worksheets data to DOM.
if (json.worksheets !== null && json.worksheets !== undefined) {
this.parseWorksheet(json.worksheets[0]);
}
//this.csvStr = 'a1,a2,a3\nb1,b2,b3';
}
private parseWorksheet(json: any): void {
//Rows
if (json.rows !== null && json.rows !== undefined) {
this.parseRows(json.rows);
}
}
/* tslint:disable:no-any */
private parseRows(rows: any): void {
let count: number = 1;
for (let row of rows) {
//Row index
if (row.index !== null && row.index !== undefined) {
while (count < row.index) {
this.csvStr += '\n';
count++;
}
this.parseRow(row);
} else {
throw Error('Row index is missing.');
}
}
}
/* tslint:disable:no-any */
private parseRow(row: any): void {
if (row.cells !== null && row.cells !== undefined) {
let count: number = 1;
for (let cell of row.cells) {
//cell index
if (cell.index !== null && cell.index !== undefined) {
while (count < cell.index) {
this.csvStr += this.separator;
count++;
}
this.parseCell(cell);
} else {
throw Error('Cell index is missing.');
}
}
}
}
/* tslint:disable:no-any */
private parseCell(cell: any): void {
let csv: string = this.csvStr;
if (cell.value !== undefined) {
if (cell.value instanceof Date) {
if (cell.style !== undefined && cell.style.numberFormat !== undefined) {
/* tslint:disable-next-line:max-line-length */
try {
csv += this.parseCellValue(this.formatter.displayText(cell.value, { type: 'dateTime', skeleton: cell.style.numberFormat }, this.isServerRendered));
} catch (error) {
/* tslint:disable-next-line:max-line-length */
csv += this.parseCellValue(this.formatter.displayText(cell.value, { type: 'dateTime', format: cell.style.numberFormat }, this.isServerRendered));
}
} else if (cell.style !== undefined && cell.style.name !== undefined && this.globalStyles.has(cell.style.name)) {
/* tslint:disable-next-line:max-line-length */
try {
csv += this.parseCellValue(this.formatter.displayText(cell.value, { type: 'dateTime', skeleton: this.globalStyles.get(cell.style.name) }, this.isServerRendered));
} catch (error) {
/* tslint:disable-next-line:max-line-length */
csv += this.parseCellValue(this.formatter.displayText(cell.value, { type: 'dateTime', format: this.globalStyles.get(cell.style.name) }, this.isServerRendered));
}
} else {
csv += cell.value;
}
} else if (typeof (cell.value) === 'boolean') {
csv += cell.value ? 'TRUE' : 'FALSE';
} else if (typeof (cell.value) === 'number') {
if (cell.style !== undefined && cell.style.numberFormat !== undefined) {
/* tslint:disable-next-line:max-line-length */
csv += this.parseCellValue(this.formatter.displayText(cell.value, { format: cell.style.numberFormat }, this.isServerRendered));
} else if (cell.style !== undefined && cell.style.name !== undefined && this.globalStyles.has(cell.style.name)) {
/* tslint:disable-next-line:max-line-length */
csv += this.parseCellValue(this.formatter.displayText(cell.value, { format: this.globalStyles.get(cell.style.name) }, this.isServerRendered));
} else {
csv += cell.value;
}
} else {
csv += this.parseCellValue(cell.value);
}
}
this.csvStr = csv;
}
private parseCellValue(value: String): any {
let val: string = '';
let length: number = value.length;
for (let start: number = 0; start < length ; start++) {
if (value[start] === '\"') {
val += value[start].replace('\"', '\"\"');
} else {
val += value[start];
}
}
value = val;
if (value.indexOf(this.separator) !== -1 || value.indexOf('\n') !== -1) {
return value = '\"' + value + '\"';
} else {
return value;
}
}
/**
* Saves the file with specified name and sends the file to client browser
* @param {string} fileName- file name to save.
* @param {Blob} buffer- the content to write in file
*/
public save(fileName: string): void {
this.buffer = new Blob(['\ufeff' + this.csvStr], { type: 'text/csv;charset=UTF-8' });
if (this.isMicrosoftBrowser) {
navigator.msSaveBlob(this.buffer, fileName);
} else {
let dataUrl: string = window.URL.createObjectURL(this.buffer);
let dwlLink: HTMLAnchorElement = document.createElementNS('http://www.w3.org/1999/xhtml', 'a') as HTMLAnchorElement;
dwlLink.download = fileName;
dwlLink.href = dataUrl;
let event: MouseEvent = document.createEvent('MouseEvent');
event.initEvent('click', true, true);
dwlLink.dispatchEvent(event);
setTimeout((): void => {
window.URL.revokeObjectURL(dataUrl);
});
}
}
public saveAsBlob(): Blob {
return new Blob(['\ufeff' + this.csvStr], { type: 'text/csv;charset=UTF-8' });
}
}