node-streams's People
node-streams's Issues
My solution
import fetch from 'node-fetch';
import {
Readable,
pipeline,
Writable
} from 'stream';
import exceljs from 'exceljs';
import util from 'util';
const URL = 'https://jsonplaceholder.typicode.com/posts/'
const cpipeline = util.promisify(pipeline);
class ExcelStream extends Writable {
constructor(options = {}) {
super({
...options,
objectMode: true
});
this.i = 0;
const o = {
filename: "./test-file.xlsx",
useStyles: true,
useSharedStrings: true,
};
this.workbook = new exceljs.stream.xlsx.WorkbookWriter(o);
this.workbook.creator = "ABCDEF";
this.workbook.created = new Date();
this.sheet = this.workbook.addWorksheet("Discussion Event");
this.sheet.columns = [{
header: "testColumn",
width: 10,
style: {
font: {
bold: true,
},
},
}, ];
}
_write (chunk, encoding, done) {
console.log(chunk)
try {
this.i += 1;
this.sheet.addRow([chunk]).commit();
} catch (error) {
console.log("Error while transforming data - commit failed");
done(error)
return
}
done()
}
_flush(cb) {
try {
this.sheet.commit();
this.workbook.commit().then(() => cb(), cb);
} catch (error) {
console.log("Error cannot commit workbook");
}
}
}
async function run() {
let i = 1;
await cpipeline(async function * () {
while (true) {
let u = URL + i
const response = await fetch(u)
const result = await response.json()
if (result) {
yield result
} else {
break
}
i++
if (i == 20) break
}
}, new ExcelStream())
}
run()
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.