-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSheetManager.js
138 lines (124 loc) · 5.8 KB
/
SheetManager.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* @Author: mikey.zhaopeng
* @Date: 2024-01-31 21:22:37
* @Last Modified by: sibusiso.nkosi
* @Last Modified time: 2024-03-16 22:57:13
*/
const fs = require('fs');
const XLSX = require('xlsx');
const GenerateAreaName = require('./utils/generate-area-name');
const GenerateSuburbId = require('./utils/generate-sid');
/**
*/
class SheetManager {
/**
* This function is for extracting all the `suburbs` data found in the suburb sheet.
* @returns [] Returns a list of all the `suburbs` from the suburb sheet.
*/
static extractEskomDirectSuburbs = () => new Promise(async (resolve, reject) => {
fs.readdir('./sheets/suburbs-sheets/eskom-direct', { encoding: 'utf8' }, (err, files) => {
if (err) console.error(err);
else {
const suburbs = [];
const uniqueList = [];
let suburbsMap = {};
files.forEach(async fileName => {
const workbook = XLSX.readFile(`./sheets/suburbs-sheets/eskom-direct/${fileName}`, { cellFormula: true, sheetStubs: true });
const suburbSheetName = workbook.SheetNames[3];
const suburbSheet = workbook.Sheets[suburbSheetName];
const provinceRange = XLSX.utils.decode_range('A6');
const province = XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]], { range: provinceRange, header: 1 });
const jsonList = XLSX.utils.sheet_to_json(suburbSheet);
const provinceName = province[0][0];
jsonList.forEach((suburb, i) => {
const sid = GenerateSuburbId.generateSid(`EskomDirect-${suburb['BLOCK']}-${suburb['SP_NAME']}${suburb['MP_NAME']}${provinceName}`);
suburbs.push({
sid,
name: suburb['SP_NAME'],
region: `Eskom Direct, ${suburb['MP_NAME']}, ${provinceName}`,
block: suburb['BLOCK']
});
})
suburbs.forEach(suburb => {
if (suburbsMap[suburb.sid] === undefined) {
suburbsMap[suburb.sid] = 0;
uniqueList.push(suburb);
}
suburbsMap[suburb.sid]++;
})
});
resolve(uniqueList);
}
})
})
/**
* This function is for extracting all the `load shedding schedule` data from the sheet
* @param {string} areaId
* @returns
*/
static extractEskomDrirectSchedule = () => new Promise(async (resolve) => {
const workbook = XLSX.readFile(`./sheets/schedule-sheets/Eskom Schedule.xlsx`, { cellFormula: true, sheetStubs: true });
const scheduleName = workbook.SheetNames[0];
const scheduleSheet = workbook.Sheets[scheduleName];
const loadsheddingRange = XLSX.utils.decode_range('A16:AH111');
XLSX.utils.sheet_to_csv()
const loadsheddingJsonData = XLSX.utils.sheet_to_json(scheduleSheet, { range: loadsheddingRange, header: 1 });
resolve(loadsheddingJsonData);
})
static extractCityPowerSchedule = () => new Promise(resolve => {
const workbook = XLSX.readFile(`./sheets/schedule-sheets/City Power Schedule.xlsx`, { cellFormula: true, sheetStubs: true });
const scheduleName = workbook.SheetNames[0];
const scheduleSheet = workbook.Sheets[scheduleName];
const loadsheddingRange = XLSX.utils.decode_range('A4:AH99');
const loadsheddingJsonData = XLSX.utils.sheet_to_json(scheduleSheet, { range: loadsheddingRange, header: 1 });
resolve(loadsheddingJsonData);
})
static extractCityPowerSuburbs = () => new Promise(resolve => {
const suburbs = [];
const uniqueList = [];
let suburbsMap = {};
const workbook = XLSX.readFile(`./sheets/suburbs-sheets/city-power/Copy of Load Shedding Schedule Version 6 Rev 0.1 Block Areas_.xlsx`, { cellFormula: true, sheetStubs: true, });
const suburbsSheet = workbook.Sheets[workbook.SheetNames[0]];
const jsonData = XLSX.utils.sheet_to_json(suburbsSheet);
jsonData.forEach((suburb, i) => {
const suburbName = suburb['Area']
const block = suburb['Load Block'];
const sid = GenerateSuburbId.generateSid(`CityPower-${block}-${suburbName}CityofJohannesburgGauteng`);
const match = suburbName.match(/\b(\d+),?/g);
if (match) {
const extensions = match.map(ext => {
return ext.replace(/\D/g, '');
});
if (extensions.length > 1) {
extensions.forEach((extNumber, i) => {
let extName = GenerateAreaName.generate(suburbName);
suburbs.push({
sid,
name: `${extName} ${extNumber}`,
region: `City Power, City of Johannesburg, Gauteng`,
block: block
});
});
}
} else {
suburbs.push({
sid,
name: suburbName,
region: `City Power, City of Johannesburg, Gauteng`,
block: block
});
}
})
suburbs.forEach(suburb => {
if (suburbsMap[suburb['sid']] === undefined) {
suburbsMap[suburb['sid']] = 0;
uniqueList.push(suburb);
}
suburbsMap[suburb['sid']]++;
})
resolve(uniqueList)
})
}
// SheetManager.extractCityPowerSchedule();
// SheetManager.extractEskomSchedule();
module.exports = SheetManager;