Skip to content

Instantly share code, notes, and snippets.

@rognales
Created September 9, 2021 04:45
Show Gist options
  • Save rognales/2ba086eca4ef00981d6aba52beb25bf8 to your computer and use it in GitHub Desktop.
Save rognales/2ba086eca4ef00981d6aba52beb25bf8 to your computer and use it in GitHub Desktop.
Stream XLSX in NodeJS
const R = require('ramda')
const { getXlsxStream } = require('xlstream');
const buildFilter = (filter) => {
let query = {};
for (let keys in filter) {
if (filter[keys].constructor === Array && filter[keys].length > 0) {
query[keys] = filter[keys];
}
}
return query;
}
const filterRow = (row, query) => {
for (let key in query) {
if (row[key] === undefined || !query[key].includes(row[key])) {
return false;
}
}
return true;
}
module.exports = (file, selectedColumns = [], sheetIndex = 0, filters = {}) => {
let container = []
const query = buildFilter(filters);
return new Promise((done, reject) => {
(async () => {
const stream = await getXlsxStream({
filePath: file,
sheet: sheetIndex,
withHeader: true,
ignoreEmpty: true,
});
stream.on('data', function (row) {
console.log(row)
if (!filterRow(row.formatted.obj, query))
return;
// If passed, push only selected columns
if (selectedColumns.length != 0) {
container.push(R.pick(selectedColumns, row.formatted.obj))
return;
}
// Else push all columns
container.push(row.formatted.obj)
});
stream.on('end', function () {
done(container)
});
stream.on('error', function (error) {
reject(error)
});
})();
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment