aboutsummaryrefslogtreecommitdiff
path: root/src/conversion/csv/excel.ts
blob: 27641924d98b26f2ebdad37b617b70bde0e19ba5 (plain) (blame)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { logger } from '../../logger';
import * as xlsx from 'xlsx';
import * as fs from 'fs';

// const testDir: string = `/cs/scratch/cjd24/csv/`;
// const testFile: string = `test1.xlsx`


const toCSV: (file: string) => Promise<string[]> = async (file: string): Promise<string[]> => {
	logger.info(`xls convertor received new file to convert to csv: ${file}`);

	let book: xlsx.WorkBook = xlsx.readFile(file);
	try {
		let sheetnames: string[] = [];
		for (let name of book.SheetNames) {
			fs.writeFile(`${file.split('.')[0]}-${name}.csv`, xlsx.utils.sheet_to_csv(book.Sheets[name]));
			sheetnames.push(`${file.split('.')[0]}-${name}.csv`);
			logger.info(`Wrote ${file.split('.')[1]} subsheet {${file} : ${name}}  as CSV.`);
		}
		return sheetnames === [] ? ["__Empty_Book_Error"] : sheetnames;
	} catch (err) {
		logger.error(err);
		return ["__Conversion_to_csv_Error_Occurred: " + err]
	}
}

const toJSON: (file: string) => Promise<string[]> = async (file: string): Promise<string[]> => {
	logger.info(`xls convertor received new file to convert to json: ${file}`);

	let book: xlsx.WorkBook = xlsx.readFile(file);
	try {
		let sheetnames: string[] = [];
		for (let name of book.SheetNames) {
			fs.writeFile(`${file.split('.')[0]}-${name}.json`, xlsx.utils.sheet_to_json(book.Sheets[name]));
			sheetnames.push(`${file.split('.')[0]}-${name}.json`);
			logger.info(`Wrote ${file.split('.')[1]} subsheet {${file} : ${name}}  as JSON.`);
		}
		return sheetnames === [] ? ["__Empty_Book_Error"] : sheetnames;
	} catch (err) {
		logger.error(err);
		return ["__Conversion_to_json_Error_Occurred: " + err]
	}
}

const toHTML: (file: string) => Promise<string[]> = async (file: string): Promise<string[]> => {
	logger.info(`xls convertor received new file to convert to html: ${file}`);

	let book: xlsx.WorkBook = xlsx.readFile(file);
	try {
		let sheetnames: string[] = [];
		for (let name of book.SheetNames) {
			fs.writeFile(`${file.split('.')[0]}-${name}.html`, xlsx.utils.sheet_to_html(book.Sheets[name]));
			sheetnames.push(`${file.split('.')[0]}-${name}.html`);
			logger.info(`Wrote ${file.split('.')[1]} subsheet {${file} : ${name}}  as HTML.`);
		}
		return sheetnames === [] ? ["__Empty_Book_Error"] : sheetnames;
	} catch (err) {
		logger.error(err);
		return ["__Conversion_to_html_Error_Occurred: " + err]
	}
}

const toAll: (file: string) =>
Promise<{csv: string[], json: string[], html: string[]}> =
async (file: string):
Promise<{csv: string[], json: string[], html: string[]}> => {
	return {
		csv: await toCSV(file),
		json: await toCSV(file),
		html: await toCSV(file)
	}
}


// const main: () => void = async (): Promise<void> => {
//
// 	try {
// 		let file: xlsx.WorkBook = xlsx.readFile(`${testDir}${testFile}`);
//
// 		for (let name of file.SheetNames) {
// 			fs.writeFileSync(`${testDir}${testFile.split('.')[0]}-${name}.csv`, xlsx.utils.sheet_to_csv(file.Sheets[name]));
// 			logger.info(`Wrote ${testFile.split('.')[1]} subsheet {${testFile} : ${name}}  as CSV.`);
// 			fs.writeFileSync(`${testDir}${testFile.split('.')[0]}-${name}.json`, xlsx.utils.sheet_to_json(file.Sheets[name]));
// 			logger.info(`Wrote ${testFile.split('.')[1]} subsheet {${testFile} : ${name}}  as json.`);
// 			fs.writeFileSync(`${testDir}${testFile.split('.')[0]}-${name}.html`, xlsx.utils.sheet_to_html(file.Sheets[name]));
// 			logger.info(`Wrote ${testFile.split('.')[1]} subsheet {${testFile} : ${name}}  as HTML.`);
// 		}
//
// 		logger.success(`Wrote ${testFile.split('.')[1]} {${testFile}}  as CSV.`);
// 		logger.success(`Wrote ${testFile.split('.')[1]} {${testFile}}  as JSON.`);
// 		logger.success(`Wrote ${testFile.split('.')[1]} {${testFile}}  as HTML.`);
// 	} catch (err) {
// 		// console.log(err);
// 		logger.error(err);
// 	}
// };
// main();