Skip to content

Instantly share code, notes, and snippets.

@acidsound
Created February 15, 2021 16:54
Show Gist options
  • Save acidsound/1d104330857b634c6507a1691d9b8cf0 to your computer and use it in GitHub Desktop.
Save acidsound/1d104330857b634c6507a1691d9b8cf0 to your computer and use it in GitHub Desktop.
xlsx generator
const jszip = new require('jszip')
const zip = new jszip()
const fs = require('fs')
const csv = require('csvtojson')
const exec = async (templateFileName, sourceFileName) => {
const data = fs.readFileSync(templateFileName)
const content = await zip.loadAsync(data)
const targetFile = 'xl/sharedStrings.xml'
const doc = await content.files[targetFile].async('string')
// replace some texts
const jsonArray = await csv().fromFile(sourceFileName)
for (idx in jsonArray) {
let o = jsonArray[idx];
// fix special values (ex. <>&'") : reference http://xml.silmaril.ie/specials.html
for (a in o) o[a]=o[a]
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&apos;");
await zip.file(
targetFile,
doc
.replace('{#id}', o.id)
.replace('{#author_name}', o.author_name)
.replace('{#created_at}', o.created_at)
.replace('{#title}', o.title)
.replace('{#input}', o.input)
.replace('{#estimated_output}', o.estimated_output)
.replace('{#actual_output}', o.actual_output)
.replace('{#result}', o.result)
.replace('{#tested_at}', o.tested_at)
.replace('{#test_method}', o.test_method)
.replace('{#test_result}', o.test_result),
);
await new Promise((resolve, reject) => {
zip
.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
.pipe(fs.createWriteStream(`output/TEST_REPORT_${o.id}.xlsx`))
.on('finish', () => resolve())
});
console.log(`${o.id} done.`);
}
}
exec(
'Test Case Report_Template.xlsx',
'Aquamarine Version Control List - test_cases_report.csv',
)
{
"name": "testReportGenerator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node main.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"csv-parser": "^3.0.0",
"csvtojson": "^2.0.10",
"jszip": "^3.5.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment