MCPcopy Index your code
hub / github.com/exceljs/exceljs / CSV

Class CSV

lib/csv/csv.js:26–189  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24/* eslint-ensable quote-props */
25
26class CSV {
27 constructor(workbook) {
28 this.workbook = workbook;
29 this.worksheet = null;
30 }
31
32 async readFile(filename, options) {
33 options = options || {};
34 if (!(await exists(filename))) {
35 throw new Error(`File not found: ${filename}`);
36 }
37 const stream = fs.createReadStream(filename);
38 const worksheet = await this.read(stream, options);
39 stream.close();
40 return worksheet;
41 }
42
43 read(stream, options) {
44 options = options || {};
45
46 return new Promise((resolve, reject) => {
47 const worksheet = this.workbook.addWorksheet(options.sheetName);
48
49 const dateFormats = options.dateFormats || [
50 'YYYY-MM-DD[T]HH:mm:ssZ',
51 'YYYY-MM-DD[T]HH:mm:ss',
52 'MM-DD-YYYY',
53 'YYYY-MM-DD',
54 ];
55 const map =
56 options.map ||
57 function(datum) {
58 if (datum === '') {
59 return null;
60 }
61 const datumNumber = Number(datum);
62 if (!Number.isNaN(datumNumber) && datumNumber !== Infinity) {
63 return datumNumber;
64 }
65 const dt = dateFormats.reduce((matchingDate, currentDateFormat) => {
66 if (matchingDate) {
67 return matchingDate;
68 }
69 const dayjsObj = dayjs(datum, currentDateFormat, true);
70 if (dayjsObj.isValid()) {
71 return dayjsObj;
72 }
73 return null;
74 }, null);
75 if (dt) {
76 return new Date(dt.valueOf());
77 }
78 const special = SpecialValues[datum];
79 if (special !== undefined) {
80 return special;
81 }
82 return datum;
83 };

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected