-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
107 lines (89 loc) · 3.56 KB
/
app.js
File metadata and controls
107 lines (89 loc) · 3.56 KB
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
function processCSV() {
const fileInput = document.getElementById('csvFile');
const file = fileInput.files[0];
const progress = document.getElementById('progress');
if (!file) {
alert("Please select a file");
return;
}
// Check if the file is a CSV
if (file.type !== 'text/csv') {
alert("Please upload a valid CSV file");
return;
}
progress.textContent = "Parsing CSV file...";
Papa.parse(file, {
header: true,
complete: function(results) {
progress.textContent = "CSV file parsed successfully";
const data = results.data;
const columns = results.meta.fields;
// Check if the CSV has a "SKILL AREA" column
if (!columns.includes("SKILL AREA")) {
alert("The CSV file must have a 'SKILL AREA' column");
progress.textContent = "CSV file missing 'SKILL AREA' column";
return;
}
progress.textContent = "Removing duplicate records...";
// Remove duplicate records
const uniqueRecords = removeDuplicates(data);
progress.textContent = "Processing data...";
const cleanedData = uniqueRecords.map((record, index) => {
const upperCaseRecord = {};
for (const key in record) {
if (key.toUpperCase() !== "TIMESTAMP") {
upperCaseRecord[key] = record[key].toUpperCase();
}
}
return upperCaseRecord;
});
const categorizedData = {};
cleanedData.forEach((record, index) => {
let skillArea = record['SKILL AREA'];
if (skillArea.length > 31) {
skillArea = skillArea.slice(0, 30);
}
if (!categorizedData[skillArea]) {
categorizedData[skillArea] = [];
}
categorizedData[skillArea].push(record);
progress.textContent = `Processed ${index + 1} records`;
});
progress.textContent = "Creating Excel workbook...";
createExcelWorkbook(categorizedData);
progress.textContent = "Excel workbook created successfully. Ready for download.";
}
});
}
function removeDuplicates(data) {
const uniqueRecords = [];
const seen = new Set();
data.forEach(record => {
const uniqueKey = record['STATE CODE'].concat(record['PHONE NO'])
if (!seen.has(uniqueKey)) {
seen.add(uniqueKey);
uniqueRecords.push(record);
}
});
alert(uniqueRecords.length)
return uniqueRecords;
}
function createExcelWorkbook(data) {
const workbook = XLSX.utils.book_new();
for (const skillArea in data) {
const records = data[skillArea];
const newRecords = records.map((record, index) => {
const newRecord = { 'S/N': index + 1, ...record }; // Add serial number as first column
return newRecord;
});
const worksheet = XLSX.utils.json_to_sheet(newRecords);
XLSX.utils.book_append_sheet(workbook, worksheet, skillArea);
}
const wbout = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
const blob = new Blob([wbout], { type: 'application/octet-stream' });
const url = URL.createObjectURL(blob);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = url;
downloadLink.download = 'processed_data.xlsx';
downloadLink.style.display = 'block';
}