-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
202 lines (172 loc) · 5.83 KB
/
index.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import {
parseFilePaths,
parseProperties,
parseSharedStrings,
parseSheet,
parseStyles,
} from "./src/modules/parser.js";
import { getXlsContents, getXmlContent } from "./src/modules/utils.js";
import { createSheetNotFoundError } from "./src/modules/validator.js";
import { createXlsx } from "./src/modules/write.js";
import ArrayData from "./src/structures/ArrayData.js";
/**
* Importing Types
* @typedef { import("./src/modules/parser").cellData } cellData
* @typedef { import("./src/modules/parser").fullData } fullData
* @typedef { import("./src/modules/parser").baseOptions } baseOptions
* @typedef { import("./src/modules/parser").parsedFilePath } parsedFilePath
* @typedef { import("./src/modules/parser").parsedStyles } parsedStyles
* @typedef { import("./src/modules/parser").parsedProperties } parsedProperties
* @typedef { import("./src/modules/parser").parsedSheetData<cellData> } parsedSheetData
* @typedef { import("./src/modules/parser").sheetProperties } sheetProperties
*
* @typedef { import("./src/files/worksheet").sheetData } sheetData
*/
const XLSX = (function () {
const _token = Symbol();
return class XLSX {
/**
* Create new XLSX class
* @param {Map<string, string>} contents
* @param {baseOptions} options
* @param {symbol} token
* @returns
*/
constructor(contents, options, token) {
if (_token !== token) {
throw new TypeError(
"XLSX is not constructable. Use XLSX.read(). or XLSX.write()"
);
}
/** @type {Map<string, string>} */
this.contents = contents;
/** @type {baseOptions} */
this.options = options;
}
/**
* @readonly
* @type {parsedProperties}
*/
get properties() {
let { contents } = this;
return parseProperties(getXmlContent("xl/workbook.xml"), contents);
}
/**
* @readonly
* @type {parsedFilePath} */
get paths() {
let { contents } = this;
return parseFilePaths(getXmlContent("xl/_rels/workbook.xml.rels"), contents);
}
/**
* @readonly
* @type {ArrayData<string>}
*/
get values() {
let { sharedStrings } = this.paths,
{ contents } = this;
return sharedStrings
? parseSharedStrings(getXmlContent(sharedStrings, contents))
: [];
}
/**
* @readonly
* @type {{}}
*/
get styles() {
let { styles } = this.paths,
{ contents } = this;
return styles ? parseStyles(getXmlContent(styles), contents) : {};
}
/**
* @readonly
* @type {string | undefined}
*/
get sheetId() {
let { sheet } = this.options,
{ sheets } = this.properties;
if (typeof sheet === "number") {
let { relationId } = sheets[sheet - 1];
return relationId;
}
for (let { name, relationId } of sheets) {
if (name === sheet) return relationId;
}
}
/**
* @readonly
* @type {string}
*/
get xmlContent() {
let { sheetId, properties, contents } = this,
{ sheet } = this.options,
{ sheets } = this.paths;
if (!sheetId || !sheets[sheetId]) {
throw createSheetNotFoundError(sheet, properties.sheets);
}
return getXmlContent(sheets[sheetId], contents);
}
/**
* @readonly
* @type {parsedSheetData}
*/
get parsedSheet() {
let { xmlContent: content, values, styles, properties, options } = this;
return parseSheet(content, values, styles, properties, options);
}
/**
* Read Xlsx contens data
* @returns {fullData}
*/
read() {
let { parsedSheet: sheets, options } = this;
let { dimensions, cells } = sheets,
{ transformData } = options;
if (cells.isEmpty) return [];
let [topLeft, bottomRight] = dimensions,
{ row: maxRow, column: maxCol } = bottomRight;
let data = new ArrayData(maxRow);
for (let { row, column, value } of cells) {
row -= 1;
column -= 1;
if (column < maxCol && row < maxRow) {
if (!data[row]) data[row] = new ArrayData();
data[row][column] = value;
}
}
return transformData ? transformData(data) : data;
}
/**
* Open and read Xlsx file data
* @param {File | Blob | ArrayBuffer} file
* @param {baseOptions} options
* @returns
*/
static async read(file, options) {
options = Object.assign({ sheet: 1 }, options);
const contents = await getXlsContents(file);
let xlsxFile = new XLSX(contents, options, _token);
return xlsxFile.read();
}
/**
* Create and download file Xlsx
*
* @overload
* @param {sheetData} data
* @param {string} filename
* @returns {void}
*/ /**
* Create and download file Xlsx with custom sheetname
*
* @overload
* @param {sheetData} data
* @param {string} filename
* @param {string} sheetname
* @returns {void}
*/
static write(data, filename, sheetname) {
return createXlsx({ data, filename, sheetname: sheetname || filename });
}
};
})();
export default XLSX;