Created
September 15, 2021 21:05
-
-
Save sibelius/dbcc33783e8cb755da04642dff9318a8 to your computer and use it in GitHub Desktop.
Automate your reports using JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import path from 'path'; | |
import util from 'util'; | |
import fs from 'fs'; | |
import * as XLSX from 'xlsx'; | |
const writeFile = util.promisify(fs.writeFile); | |
const cwd = process.cwd(); | |
export const reportJsonExcel = async (filename: string, data: any) => { | |
const jsonFile = path.join(cwd, `${filename}.json`); | |
const excelFile = path.join(cwd, `${filename}.xlsx`); | |
// eslint-disable-next-line | |
console.log(`report at: ${jsonFile}`); | |
// eslint-disable-next-line | |
console.log(`report at: ${excelFile}`); | |
await writeFile(jsonFile, JSON.stringify(data, null, 2)); | |
const ws = XLSX.utils.json_to_sheet(data); | |
const wb = XLSX.utils.book_new(); | |
XLSX.utils.book_append_sheet(wb, ws, 'Cohort'); | |
await XLSX.writeFile(wb, excelFile); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment