-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport-json-to-excel-nodejs.ts
More file actions
28 lines (20 loc) · 1 KB
/
export-json-to-excel-nodejs.ts
File metadata and controls
28 lines (20 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import jetpack from 'fs-jetpack';
import type { JsonSheet, ExportJsonToExcelOptions } from './types';
import { jsonToExcel } from './json-to-excel';
import { expandExportJsonToExcelOptions } from './defaults';
export async function exportJsonToExcelNodejs(destinationPath: string, sheets: JsonSheet[], options?: ExportJsonToExcelOptions): Promise<void> {
const expandedOptions = expandExportJsonToExcelOptions(options);
const existence = jetpack.exists(destinationPath);
if (existence === 'dir') {
throw new Error(`Destination path is a directory: "${destinationPath}"`);
}
if (existence === 'other') {
throw new Error(`Destination path already exists and is not a file: "${destinationPath}"`);
}
if (existence === 'file' && !expandedOptions.overwrite) {
throw new Error(`Destination file already exists, refusing to overwrite: "${destinationPath}"`);
}
const workbook = jsonToExcel(sheets, expandedOptions);
await expandedOptions.beforeSave(workbook);
await workbook.xlsx.writeFile(destinationPath);
}