Skip to content

Instantly share code, notes, and snippets.

@Fallenstedt
Created January 31, 2018 00:55
Show Gist options
  • Save Fallenstedt/987d24504578567075ebeec6ca10b8e3 to your computer and use it in GitHub Desktop.
Save Fallenstedt/987d24504578567075ebeec6ca10b8e3 to your computer and use it in GitHub Desktop.
generate n .xlsx files from a source template using a list of names
var fs = require('fs');
var XLSX = require('xlsx');
console.log('app starting')
const SOURCE = './source/listOfFilesYouWantToGenerate.xlsx';
const QUESTIONS = './source/sourceFileThatNeedsToBeCopiesManyTimes.xlsx';
const TARGET = './vendors';
const sourceFile = XLSX.readFile(SOURCE);
const fileNames = Object.keys(sourceFile.Sheets.Approved)
.filter((key) => doesStringContainCharacter(key, 'D'))
.map(item => {
let newFileName = sourceFile.Sheets.Approved[item].v;
newFileName = newFileName.replace(/\./g, '-').toLowerCase()
newFileName = newFileName.replace('((https://serverless-com/))','')
return newFileName
})
// TODO check if target directory is empty before copying so you don't fuck up your work
// .map(item => copyFile(item))
function copyFile(name) {
fs.createReadStream(QUESTIONS).pipe(fs.createWriteStream(TARGET + "/" + name + '.xlsx'));
}
function doesStringContainCharacter(key, char) {
return key.indexOf(char) > -1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment