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 | 13x 13x 13x 13x 13x 13x | import { SegmentRead, PinchUtilityRead, usePinchSegmentBulkCreateCreateMutation, usePinchPinchutilityBulkCreateCreateMutation, CreateSegment } from '@/api/apiStore.gen';
import { useCurrentStreamDataProjectID } from '@/hooks/PinchHooks';
import * as xlsx from 'node-xlsx';
import { toast } from 'sonner';
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 = xlsx.parse(data);
// Get rows from stream data sheet
const streamSheet = workbook.find(
(sheet) => sheet.name === "Stream Data",
);
Iif (!streamSheet) {
throw new Error("Streams Sheet Doesn't Exist");
}
const streamData = streamSheet.data.slice(2); // Skip header row
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: row[1],
parentZone: row[0],
t_supply: parseFloat(row[2]),
t_target: parseFloat(row[3]),
heat_flow: parseFloat(row[4]),
dt_cont: parseFloat(row[5]),
htc: parseFloat(row[6]),
});
}
// Get rows from utility data sheet
const utilitySheet = workbook.find(
(sheet) => sheet.name === "Utility Data",
);
Iif (!utilitySheet) {
throw new Error("Utility Sheet Doesn't Exist");
}
const utilityData = utilitySheet.data.slice(2); // Skip header rows
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: row[0],
type: row[1],
t_supply: parseFloat(row[2]),
t_target: parseFloat(row[3]),
dt_cont: parseFloat(row[4]),
price: parseFloat(row[5]),
htc: parseFloat(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;
}
|