mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-29 19:09:14 +00:00
refactor: get worksheet names from excel upload
This commit is contained in:
committed by
Carlos Valente
parent
162693e19e
commit
b65b3fa6d2
@@ -11,22 +11,14 @@ const excelPath = `${apiEntryUrl}/excel`;
|
|||||||
* upload Excel file to server
|
* upload Excel file to server
|
||||||
* @return string - file ID op the uploaded file
|
* @return string - file ID op the uploaded file
|
||||||
*/
|
*/
|
||||||
export async function upload(file: File) {
|
export async function upload(file: File): Promise<string[]> {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('excel', file);
|
formData.append('excel', file);
|
||||||
await axios.post(`${excelPath}/upload`, formData, {
|
const response = await axios.post(`${excelPath}/upload`, formData, {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'multipart/form-data',
|
'Content-Type': 'multipart/form-data',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get Worksheet names
|
|
||||||
* @return string[] - array of available worksheets
|
|
||||||
*/
|
|
||||||
export async function getWorksheetNames(): Promise<string[]> {
|
|
||||||
const response: AxiosResponse<string[]> = await axios.get(`${excelPath}/worksheets`);
|
|
||||||
return response.data;
|
return response.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-3
@@ -3,7 +3,6 @@ import { IoCloudOutline, IoDownloadOutline } from 'react-icons/io5';
|
|||||||
import { getErrorMessage, ImportMap } from 'ontime-utils';
|
import { getErrorMessage, ImportMap } from 'ontime-utils';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
getWorksheetNames as getWorksheetNamesExcel,
|
|
||||||
importRundownPreview as importRundownPreviewExcel,
|
importRundownPreview as importRundownPreviewExcel,
|
||||||
upload as uploadExcel,
|
upload as uploadExcel,
|
||||||
} from '../../../../../common/api/excel';
|
} from '../../../../../common/api/excel';
|
||||||
@@ -54,8 +53,7 @@ export default function SourcesPanel() {
|
|||||||
try {
|
try {
|
||||||
setHasFile('loading');
|
setHasFile('loading');
|
||||||
validateExcelImport(fileToUpload);
|
validateExcelImport(fileToUpload);
|
||||||
await uploadExcel(fileToUpload);
|
const names = await uploadExcel(fileToUpload);
|
||||||
const names = await getWorksheetNamesExcel();
|
|
||||||
setWorksheets(names);
|
setWorksheets(names);
|
||||||
setImportFlow('excel');
|
setImportFlow('excel');
|
||||||
setHasFile('done');
|
setHasFile('done');
|
||||||
|
|||||||
@@ -2,35 +2,32 @@ import express from 'express';
|
|||||||
import type { Request, Response } from 'express';
|
import type { Request, Response } from 'express';
|
||||||
import { CustomFields, ErrorResponse, Rundown } from 'ontime-types';
|
import { CustomFields, ErrorResponse, Rundown } from 'ontime-types';
|
||||||
|
|
||||||
|
import { getDataProvider } from '../../classes/data-provider/DataProvider.js';
|
||||||
|
|
||||||
import { getProjectCustomFields } from '../rundown/rundown.dao.js';
|
import { getProjectCustomFields } from '../rundown/rundown.dao.js';
|
||||||
|
|
||||||
import { uploadExcel } from './excel.middleware.js';
|
import { uploadExcel } from './excel.middleware.js';
|
||||||
import { validateFileExists, validateImportMapOptions, validateRundownExport } from './excel.validation.js';
|
import { validateFileExists, validateImportMapOptions, validateRundownExport } from './excel.validation.js';
|
||||||
import { generateExcelFile, generateRundownPreview, listWorksheets, readExcelFile } from './excel.service.js';
|
import { generateExcelFile, generateRundownPreview, readExcelFile } from './excel.service.js';
|
||||||
import { EXCEL_MIME } from './excel.constants.js';
|
import { EXCEL_MIME } from './excel.constants.js';
|
||||||
import { getDataProvider } from '../../classes/data-provider/DataProvider.js';
|
|
||||||
|
|
||||||
export const router = express.Router();
|
export const router = express.Router();
|
||||||
|
|
||||||
router.post('/upload', uploadExcel, validateFileExists, async (req: Request, res: Response<never | ErrorResponse>) => {
|
router.post(
|
||||||
try {
|
'/upload',
|
||||||
// file has been validated by middleware
|
uploadExcel,
|
||||||
const filePath = (req.file as Express.Multer.File).path;
|
validateFileExists,
|
||||||
await readExcelFile(filePath);
|
async (req: Request, res: Response<string[] | ErrorResponse>) => {
|
||||||
res.status(201).send();
|
try {
|
||||||
} catch (error) {
|
// file has been validated by middleware
|
||||||
res.status(500).send({ message: String(error) });
|
const filePath = (req.file as Express.Multer.File).path;
|
||||||
}
|
const worksheetNames = await readExcelFile(filePath);
|
||||||
});
|
res.status(200).send(worksheetNames);
|
||||||
|
} catch (error) {
|
||||||
router.get('/worksheets', (_req: Request, res: Response<string[] | ErrorResponse>) => {
|
res.status(500).send({ message: String(error) });
|
||||||
try {
|
}
|
||||||
const names = listWorksheets();
|
},
|
||||||
res.status(200).send(names);
|
);
|
||||||
} catch (error) {
|
|
||||||
res.status(500).send({ message: String(error) });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
'/preview',
|
'/preview',
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ let excelData: WorkBook = xlsx.utils.book_new();
|
|||||||
* Receives and parses an excel file
|
* Receives and parses an excel file
|
||||||
* The file is deleted after being read
|
* The file is deleted after being read
|
||||||
*/
|
*/
|
||||||
export async function readExcelFile(filePath: string) {
|
export async function readExcelFile(filePath: string): Promise<string[]> {
|
||||||
if (!existsSync(filePath)) {
|
if (!existsSync(filePath)) {
|
||||||
throw new Error('Upload of excel file failed');
|
throw new Error('Upload of excel file failed');
|
||||||
}
|
}
|
||||||
@@ -39,12 +39,7 @@ export async function readExcelFile(filePath: string) {
|
|||||||
excelData = xlsx.readFile(filePath, { cellDates: true, cellFormula: false });
|
excelData = xlsx.readFile(filePath, { cellDates: true, cellFormula: false });
|
||||||
|
|
||||||
await deleteFile(filePath);
|
await deleteFile(filePath);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* List all worksheets in the current spreadsheet file
|
|
||||||
*/
|
|
||||||
export function listWorksheets(): string[] {
|
|
||||||
return excelData.SheetNames;
|
return excelData.SheetNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user