Skip to content

Commit

Permalink
use xlsx
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-Arc committed Oct 17, 2024
1 parent 86e6f8b commit 7c1f305
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 103 deletions.
4 changes: 2 additions & 2 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
"lowdb": "^7.0.1",
"multer": "^1.4.5-lts.1",
"node-osc": "^9.0.2",
"node-xlsx": "^0.23.0",
"ontime-utils": "workspace:*",
"sanitize-filename": "^1.6.3",
"steno": "^4.0.2",
"ws": "^8.13.0"
"ws": "^8.13.0",
"xlsx": "^0.18.5"
},
"devDependencies": {
"@types/cors": "^2.8.17",
Expand Down
18 changes: 10 additions & 8 deletions apps/server/src/api-data/excel/excel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import { ImportMap } from 'ontime-utils';

import { extname } from 'path';
import { existsSync } from 'fs';
import xlsx from 'node-xlsx';
import xlsx from 'xlsx';
import type { WorkBook } from 'xlsx';

import { parseExcel } from '../../utils/parser.js';
import { parseRundown } from '../../utils/parserFunctions.js';
import { deleteFile } from '../../utils/parserUtils.js';

let excelData: { name: string; data: unknown[][] }[] = [];
let excelData: WorkBook;

export async function saveExcelFile(filePath: string) {
if (!existsSync(filePath)) {
Expand All @@ -23,32 +24,33 @@ export async function saveExcelFile(filePath: string) {
if (extname(filePath) != '.xlsx') {
throw new Error('Wrong file format');
}
excelData = xlsx.parse(filePath, { cellDates: true });
excelData = xlsx.readFile(filePath, { cellDates: true, cellFormula: false });

await deleteFile(filePath);
}

export function listWorksheets() {
return excelData.map((value) => value.name);
export function listWorksheets(): string[] {
return excelData.SheetNames;
}

export function generateRundownPreview(options: ImportMap): { rundown: OntimeRundown; customFields: CustomFields } {
const data = excelData.find(({ name }) => name.toLowerCase() === options.worksheet.toLowerCase())?.data;
const data = excelData.Sheets[options.worksheet];

if (!data) {
throw new Error(`Could not find data to import, maybe the worksheet name is incorrect: ${options.worksheet}`);
}

const dataFromExcel = parseExcel(data, options);
const arrayOfData: any[][] = xlsx.utils.sheet_to_json(data, { header: 1, blankrows: false, raw: false });

const dataFromExcel = parseExcel(arrayOfData, options);
// we run the parsed data through an extra step to ensure the objects shape
const { rundown, customFields } = parseRundown(dataFromExcel);
if (rundown.length === 0) {
throw new Error(`Could not find data to import in the worksheet: ${options.worksheet}`);
}

// clear the data
excelData = [];
excelData = undefined;

return { rundown, customFields };
}
Loading

0 comments on commit 7c1f305

Please sign in to comment.