From 9c47bd66c75e6ec88bfc1ab1925dc8422f5ea4b7 Mon Sep 17 00:00:00 2001 From: cjd24 Date: Mon, 23 Apr 2018 00:12:59 +0100 Subject: DO NOT USE, this is an intermediate commit to do sad last min stuff lol --- src/conversion/csv/excel.ts | 34 +++++++++++++++++++++++++++------- src/conversion/types/helpers.ts | 6 +++++- 2 files changed, 32 insertions(+), 8 deletions(-) (limited to 'src/conversion') 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 = async (file: string): Promise => { +const toCSV: (file: string) => Promise = async (file: string): Promise => { 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 } } diff --git a/src/conversion/types/helpers.ts b/src/conversion/types/helpers.ts index 8e07944..bb10e58 100644 --- a/src/conversion/types/helpers.ts +++ b/src/conversion/types/helpers.ts @@ -6,7 +6,7 @@ import { Node } from 'xml-parser'; * of the protocol specification for scalable images */ export interface SupportedViews { - scalable_image: { + scalable_image?: { width: number; height: number; channels: @@ -25,6 +25,10 @@ export interface SupportedViews { }; metadata?: {}; }; + tabular?: { + columns: string[]; + rows: number; + } } /** -- cgit v1.2.1