Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commiteda23ae

Browse files
authored
Merge pull request#1019 from LibertyNJ/fix/csv-read-date-parsing
Fix issue # 991
2 parentsf4dc603 +a5c242b commiteda23ae

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

‎README.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,13 +1751,15 @@ The CSV parser uses [fast-csv](https://www.npmjs.com/package/fast-csv) to read t
17511751
The options passed into the read functions above is also passed to fast-csv for parsing of the csv data.
17521752
Please refer to the fast-csv README.md for details.
17531753

1754-
Dates are parsed using the npm module[moment](https://www.npmjs.com/package/moment).
1755-
Ifno dateFormatsaresupplied, the following are used:
1754+
Dates are parsed using the npm module[dayjs](https://www.npmjs.com/package/dayjs).
1755+
Ifa dateFormatsarray is notsupplied, the following dateFormats are used:
17561756

1757-
*moment.ISO_8601
1757+
*'YYYY-MM-DD\[T\]HH:mm:ss'
17581758
* 'MM-DD-YYYY'
17591759
* 'YYYY-MM-DD'
17601760

1761+
Please refer to the[dayjs CustomParseFormat plugin](https://github.com/iamkun/dayjs/blob/HEAD/docs/en/Plugin.md#customparseformat) for details on how to structure a dateFormat.
1762+
17611763
####Writing CSV
17621764

17631765
```javascript

‎lib/csv/csv.js‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
constfs=require('fs');
22
constfastCsv=require('fast-csv');
3-
constdayjs=require('dayjs');
3+
constcustomParseFormat=require('dayjs/plugin/customParseFormat');
4+
constdayjs=require('dayjs').extend(customParseFormat);
45
constStreamBuf=require('../utils/stream-buf');
56

67
const{fs:{exists}}=require('../utils/utils');
@@ -51,7 +52,7 @@ class CSV {
5152
options=options||{};
5253
constworksheet=this.workbook.addWorksheet(options.sheetName);
5354

54-
constdateFormats=options.dateFormats||[dayjs.ISO_8601,'MM-DD-YYYY','YYYY-MM-DD'];
55+
constdateFormats=options.dateFormats||['YYYY-MM-DD[T]HH:mm:ss','MM-DD-YYYY','YYYY-MM-DD'];
5556
constmap=
5657
options.map||
5758
function(datum){
@@ -62,8 +63,17 @@ class CSV {
6263
if(!Number.isNaN(datumNumber)&&datumNumber!==Infinity){
6364
returndatumNumber;
6465
}
65-
constdt=dayjs(datum,dateFormats,true);
66-
if(dt.isValid()){
66+
constdt=dateFormats.reduce((matchingDate,currentDateFormat)=>{
67+
if(matchingDate){
68+
returnmatchingDate;
69+
}
70+
constdayjsObj=dayjs(datum,currentDateFormat,true);
71+
if(dayjsObj.isValid()){
72+
returndayjsObj;
73+
}
74+
returnnull;
75+
},null);
76+
if(dt){
6777
returnnewDate(dt.valueOf());
6878
}
6979
constspecial=SpecialValues[datum];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2019-11-04
2+
11-04-2019
3+
2019-11-04T10:17:55
4+
00210PRG1
5+
1234-5thisisnotadate
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
constExcelJS=verquire('exceljs');
2+
3+
describe('github issues',()=>{
4+
it('issue 991 - differentiates between strings with leading numbers and dates when reading csv files',()=>{
5+
constwb=newExcelJS.Workbook();
6+
returnwb.csv
7+
.readFile('./spec/integration/data/test-issue-991.csv')
8+
.then((worksheet)=>{
9+
expect(worksheet.getCell('A1').value.toString()).to.equal(newDate('2019-11-04T00:00:00').toString());
10+
expect(worksheet.getCell('A2').value.toString()).to.equal(newDate('2019-11-04T00:00:00').toString());
11+
expect(worksheet.getCell('A3').value.toString()).to.equal(newDate('2019-11-04T10:17:55').toString());
12+
expect(worksheet.getCell('A4').value).to.equal('00210PRG1');
13+
expect(worksheet.getCell('A5').value).to.equal('1234-5thisisnotadate');
14+
});
15+
});
16+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp