diff options
| author | cjd24 | 2018-04-23 00:12:59 +0100 |
|---|---|---|
| committer | cjd24 | 2018-04-23 00:12:59 +0100 |
| commit | 9c47bd66c75e6ec88bfc1ab1925dc8422f5ea4b7 (patch) | |
| tree | 44d51a02f2977f8ded73f3147b86d280354bb7d5 /src/conversion/csv | |
| parent | Backing up - compressed the shellscript into node, and made some other unific... (diff) | |
| download | medicine-9c47bd66c75e6ec88bfc1ab1925dc8422f5ea4b7.tar.gz medicine-9c47bd66c75e6ec88bfc1ab1925dc8422f5ea4b7.zip | |
DO NOT USE, this is an intermediate commit to do sad last min stuff lol
Diffstat (limited to 'src/conversion/csv')
| -rw-r--r-- | src/conversion/csv/excel.ts | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/src/conversion/csv/excel.ts b/src/conversion/csv/excel.ts index 2764192..e5b1c62 100644 --- a/src/conversion/csv/excel.ts +++ b/src/conversion/csv/excel.ts @@ -1,26 +1,46 @@ import { logger } from '../../logger'; import * as xlsx from 'xlsx'; import * as fs from 'fs'; +import { SupportedViews } from '../types/helpers'; +import * as csv from 'csv-parser'; // const testDir: string = `/cs/scratch/cjd24/csv/`; // const testFile: string = `test1.xlsx` +const generateProtoViews: (sheet: xlsx.WorkSheet) => SupportedViews = (sheet: xlsx.WorkSheet): SupportedViews => { + return { + tabular: { + columns: [''], + rows: sheet['!rows'].length; + } + } +} -const toCSV: (file: string) => Promise<string[]> = async (file: string): Promise<string[]> => { +const toCSV: (file: string) => Promise<string> = async (file: string): Promise<string> => { logger.info(`xls convertor received new file to convert to csv: ${file}`); + let shortFile: string = file.split('.')[0].split('/')[1]; + let path: string = file.split('/')[0] + '/'; let book: xlsx.WorkBook = xlsx.readFile(file); + try { - let sheetnames: string[] = []; + let views: any = {}; views.items = []; + 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.`); + await fs.writeFile(`${path}${shortFile}-${name}.csv`, xlsx.utils.sheet_to_csv(book.Sheets[name])); + views.items.push(name); + let parse = csv(`${path}${shortFile}-${name}.csv`); + + views[name] = generateProtoViews(book.Sheets[name]); + logger.info(`Wrote subsheet {${shortFile} : ${name}} as CSV.`); } - return sheetnames === [] ? ["__Empty_Book_Error"] : sheetnames; + fs.writeFile(`${path}${shortFile}-BASE.supported_views.json`, views[views.items[0]]); + await fs.writeFile(`${path}${shortFile}-ALL.supported_views.json`, views); + + return views.items === [] ? "__Empty_Book_Error" : `${path}${shortFile}-ALL.supported_views.json`; } catch (err) { logger.error(err); - return ["__Conversion_to_csv_Error_Occurred: " + err] + return "__conversion_to_csv_error_occurred: " + err } } |
