You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am wanting to format the date in the output CSV according to the cell's numFmt value, but I am not sure how to do this?
My code is as follows, based on example in the readMe:
importExcelJS,{CsvWriteOptions,Worksheet}from'exceljs';import{DateTime}from'luxon';asyncfunctiontoCsv(filePath: string,worksheetName: string){constworkbook=newExcelJS.Workbook();awaitworkbook.xlsx.readFile(filePath);constwriteFileOptions: Partial<CsvWriteOptions>={sheetName: worksheetName,map(value,index){switch(index){case1:
// column 1 is stringreturnvalue;case2:
// column 2 is a dateconsole.log((value.numFmt));returnDateTime.fromJSDate(value).toFormat('yyyy-MM-dd');case3:
// column 3 is a formula, write just the resultreturnvalue.result;default:
// the rest are numbersreturnvalue;}},// https://c2fo.github.io/fast-csv/docs/formatting/optionsformatterOptions: {delimiter: '\t',quote: false,},};}
I was hoping value would be of use, but it does not represent cell object. From what I can tell, it just represents a primitive type?
The only alternative I am seeing is to process the sheet myself and then use a package such as csv-stringify to write the file:
importfsfrom'fs/promises';import{stringify}from'csv-stringify/sync';importExcelJS,{CsvWriteOptions,Worksheet}from'exceljs';functionworksheetToArray(worksheet: Worksheet): string[][]{constcellValues: string[][]=[];worksheet.eachRow(row=>{constvalues: string[]=[];row.eachCell(cell=>{if(cell.type===ExcelJS.ValueType.Date){console.log('TODO handle date format',cell.numFmt);values.push((cell.valueasDate).toISOString());}else{values.push(cell.text);}});cellValues.push(values);});returncellValues;}asyncfunctiontoCsv(filePath: string,worksheetName: string,outputPath: string,options?: Partial<CsvWriteOptions>){constvalues=awaitloadWorksheet(filePath,worksheetName);if(!values){thrownewError(`No worksheet values found in ${worksheetName}`);}constoutput=stringify(values,{delimiter: '\t'});awaitfs.writeFile(outputPath,output,'utf-8');}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am wanting to format the date in the output CSV according to the cell's numFmt value, but I am not sure how to do this?
My code is as follows, based on example in the readMe:
I was hoping value would be of use, but it does not represent cell object. From what I can tell, it just represents a primitive type?
The only alternative I am seeing is to process the sheet myself and then use a package such as
csv-stringify
to write the file:Note, using exceljs 4.4.0.
Beta Was this translation helpful? Give feedback.
All reactions