Skip to content

Commit

Permalink
Emit totalSheetSize and processedSheetSize with data event.
Browse files Browse the repository at this point in the history
  • Loading branch information
jansivans committed Jul 16, 2021
1 parent fa5f260 commit a57eea2
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Memory-efficiently turns XLSX file into a [transform stream](https://nodejs.org/

* Stream is **pausable**.
* Emits all **default events** (`data`, `end`, etc.)
* Returns **header**, **raw** and **formatted** row data in just one `data` event.
* Returns **header**, **raw**, **totalSheetSize**, **processedSheetSize** and **formatted** row data in just one `data` event.
* Maintains desirable behavior of **merged cells**.
* Supports files created by **OpenXML**.
* Supports **standard**, **Excel** and **custom number formats**.
Expand Down Expand Up @@ -51,7 +51,9 @@ const { getXlsxStream } = require('xlstream');
"obj": { "A": "hello", "B": 123 },
"arr": [ "hello", 123 ]
},
"header": []
"header": [],
"totalSheetSize": 1110,
"processedSheetSize": 1110
}
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xlstream",
"version": "2.3.4",
"version": "2.4.0",
"description": "Turns XLSX into a readable stream.",
"main": "lib/index",
"types": "lib/index",
Expand Down
21 changes: 20 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { IMergedCellDictionary, IWorksheet, IWorksheetOptions, IXlsxStreamOption
const StreamZip = require('node-stream-zip');
const saxStream = require('sax-stream');
const rename = require('deep-rename-keys');
let currentSheetProcessedSize = 0;
let currentSheetSize = 0;

function lettersToNumber(letters: string) {
return letters.split('').reduce((r, a) => r * 26 + parseInt(a, 36) - 9, 0);
Expand Down Expand Up @@ -99,6 +101,8 @@ function getTransform(formats: (string | number)[], strings: string[], dict?: IM
arr: []
},
header: getFilledHeader(arr, header),
processedSheetSize: currentSheetProcessedSize,
totalSheetSize: currentSheetSize,
})
}
}
Expand Down Expand Up @@ -163,6 +167,8 @@ function getTransform(formats: (string | number)[], strings: string[], dict?: IM
arr: formattedArr,
},
header: getFilledHeader(arr, header),
processedSheetSize: currentSheetProcessedSize,
totalSheetSize: currentSheetSize,
});
}
},
Expand All @@ -185,6 +191,8 @@ function getTransform(formats: (string | number)[], strings: string[], dict?: IM
arr: formattedArr,
},
header: getFilledHeader(arr, header),
processedSheetSize: currentSheetProcessedSize,
totalSheetSize: currentSheetSize,
});
}
}
Expand Down Expand Up @@ -218,6 +226,7 @@ export async function* getXlsxStreams(options: IXlsxStreamsOptions): AsyncGenera
file: options.filePath,
storeEntries: true
});
let zipEntries: any = {};
let currentSheetIndex = 0;

function setupGenericData() {
Expand Down Expand Up @@ -315,6 +324,7 @@ export async function* getXlsxStreams(options: IXlsxStreamsOptions): AsyncGenera
}

zip.on('ready', () => {
zipEntries = zip.entries();
getRels();
});
zip.on('error', (err: any) => {
Expand Down Expand Up @@ -374,8 +384,17 @@ export async function* getXlsxStreams(options: IXlsxStreamsOptions): AsyncGenera
dict = await getMergedCellDictionary(sheetFileName);
}
return new Promise<Transform>((resolve, reject) => {
zip.stream(`xl/worksheets/${sheetFileName}`, (err: any, stream: ReadStream) => {
const sheetFullFileName = `xl/worksheets/${sheetFileName}`;
zip.stream(sheetFullFileName, (err: any, stream: ReadStream) => {
currentSheetProcessedSize = 0;
currentSheetSize = zipEntries[sheetFullFileName].size;
const readStream = stream
.pipe(new Transform({
transform(chunk, encoding, done) {
currentSheetProcessedSize += chunk.length;
done(undefined, chunk);
}
}))
.pipe(saxStream({
strict: true,
tag: ['x:row', 'row']
Expand Down
Loading

0 comments on commit a57eea2

Please sign in to comment.