Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created September 15, 2021 21:05
Show Gist options
  • Save sibelius/dbcc33783e8cb755da04642dff9318a8 to your computer and use it in GitHub Desktop.
Save sibelius/dbcc33783e8cb755da04642dff9318a8 to your computer and use it in GitHub Desktop.
Automate your reports using JavaScript
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