chore(repo): initialize planarchy workspace
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import * as XLSX from "xlsx";
|
||||
|
||||
/**
|
||||
* Parse an Excel (.xlsx, .xls) or CSV file to an array of row objects.
|
||||
* Keys come from the first row (headers).
|
||||
*/
|
||||
export function parseSpreadsheet(file: File): Promise<Record<string, string>[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = (e) => {
|
||||
try {
|
||||
const data = new Uint8Array(e.target!.result as ArrayBuffer);
|
||||
const workbook = XLSX.read(data, { type: "array" });
|
||||
const sheetName = workbook.SheetNames[0];
|
||||
if (!sheetName) {
|
||||
resolve([]);
|
||||
return;
|
||||
}
|
||||
const sheet = workbook.Sheets[sheetName];
|
||||
if (!sheet) {
|
||||
resolve([]);
|
||||
return;
|
||||
}
|
||||
const rows = XLSX.utils.sheet_to_json<Record<string, string>>(sheet, {
|
||||
raw: false,
|
||||
defval: "",
|
||||
});
|
||||
resolve(rows);
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
};
|
||||
reader.onerror = () => reject(reader.error);
|
||||
reader.readAsArrayBuffer(file);
|
||||
});
|
||||
}
|
||||
|
||||
export function isSpreadsheetFile(file: File): boolean {
|
||||
return (
|
||||
file.type === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
|
||||
file.type === "application/vnd.ms-excel" ||
|
||||
file.name.endsWith(".xlsx") ||
|
||||
file.name.endsWith(".xls") ||
|
||||
file.name.endsWith(".csv")
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user