Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | 62x 62x 62x 62x | import ExcelJS from "exceljs";
import { toast } from "sonner";
import {
CreateSegment,
PinchUtilityRead,
SegmentRead,
usePinchPinchutilityBulkCreateCreateMutation,
usePinchSegmentBulkCreateCreateMutation,
} from "@/api/apiStore.gen";
import { useCurrentStreamDataProjectID } from "@/hooks/PinchHooks";
function getWorksheetRows(worksheet: ExcelJS.Worksheet): ExcelJS.CellValue[][] {
const rows: ExcelJS.CellValue[][] = [];
worksheet.eachRow({ includeEmpty: true }, (row) => {
rows.push(row.values.slice(1));
});
return rows;
}
function getCellNumber(value: ExcelJS.CellValue): number {
if (typeof value === "number") {
return value;
}
return parseFloat(String(value ?? ""));
}
function getCellString(value: ExcelJS.CellValue): string {
return String(value ?? "");
}
export function useParseExcel() {
const project = useCurrentStreamDataProjectID();
const [bulkCreateStreams] = usePinchSegmentBulkCreateCreateMutation();
const [bulkCreateUtilities] = usePinchPinchutilityBulkCreateCreateMutation();
const handleExcelUpload = (file: File) => {
const parsingToastId = toast.loading("Parsing file...");
const reader = new FileReader();
reader.onload = async (e) => {
try {
const data = new Uint8Array(e.target!.result as ArrayBuffer);
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.load(data);
// Get rows from stream data sheet
const streamSheet = workbook.getWorksheet("Stream Data");
if (!streamSheet) {
throw new Error("Streams Sheet Doesn't Exist");
}
const streamData = getWorksheetRows(streamSheet).slice(2);
const parsedStreams: Partial<CreateSegment>[] = [];
for (const row of streamData) {
Iif (!row[0]) break; // Break if row is blank
// Serialize from cell index
parsedStreams.push({
name: getCellString(row[1]),
parentZone: getCellString(row[0]),
t_supply: getCellNumber(row[2]),
t_target: getCellNumber(row[3]),
heat_flow: getCellNumber(row[4]),
dt_cont: getCellNumber(row[5]),
htc: getCellNumber(row[6]),
});
}
// Get rows from utility data sheet
const utilitySheet = workbook.getWorksheet("Utility Data");
if (!utilitySheet) {
throw new Error("Utility Sheet Doesn't Exist");
}
const utilityData = getWorksheetRows(utilitySheet).slice(2);
const parsedUtilities: Partial<PinchUtilityRead>[] = [];
for (const row of utilityData) {
Iif (!row[0]) break; // Break if row is blank
// Serialize from cell index
parsedUtilities.push({
name: getCellString(row[0]),
type: getCellString(row[1]),
t_supply: getCellNumber(row[2]),
t_target: getCellNumber(row[3]),
dt_cont: getCellNumber(row[4]),
price: getCellNumber(row[5]),
htc: getCellNumber(row[6]),
});
}
// Create objects on backend
await bulkCreateStreamsAndUtilities(parsedStreams, parsedUtilities);
toast.success("File parsed!");
} catch (error) {
toast.error(`Error processing file: ${error}`);
} finally {
// Dismiss the loading toast
toast.dismiss(parsingToastId);
}
};
reader.readAsArrayBuffer(file);
};
// Add objects on backend
const bulkCreateStreamsAndUtilities = (
streams: Partial<SegmentRead>[],
utilities: Partial<PinchUtilityRead>[],
) => {
bulkCreateStreams({
bulkCreateStreams: { projectID: +project, streams: streams },
});
bulkCreateUtilities({
bulkCreateUtilities: { projectID: +project, utilities: utilities },
});
};
return handleExcelUpload;
}
|