First Commit
This commit is contained in:
25
src/utils/excelParser.js
Normal file
25
src/utils/excelParser.js
Normal file
@ -0,0 +1,25 @@
|
||||
import * as XLSX from 'xlsx';
|
||||
|
||||
export const loadItemsFromExcel = async () => {
|
||||
try {
|
||||
const response = await fetch(`/items.xlsx?t=${new Date().getTime()}`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to load excel file');
|
||||
}
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
const workbook = XLSX.read(arrayBuffer, { type: 'array' });
|
||||
|
||||
// Assuming the items are in the first sheet
|
||||
const firstSheetName = workbook.SheetNames[0];
|
||||
const worksheet = workbook.Sheets[firstSheetName];
|
||||
|
||||
// Convert sheet to JSON
|
||||
const data = XLSX.utils.sheet_to_json(worksheet);
|
||||
|
||||
// We expect the data to have columns like 'Item ID', 'Description', 'Price'
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error("Error loading Excel items:", error);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user