-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
101 lines (83 loc) · 2.72 KB
/
script.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
/*
Author: Ola White
Description: Hello there! This is my JavaScript file. I appreciate your visit to my code base. Follow me on my pages:
Portfolio: https://www.olawhite.com/
Github: https://github.com/whitesoftx
Twitter: https://twitter.com/olawhite
Keep coding and exploring!
*/
// Initialize an array to store the content of each uploaded file
let filesContent = [];
function handleFileSelect(event) {
const files = event.target.files;
for (const file of files) {
// Use PapaParse to read the CSV file content
Papa.parse(file, {
complete: function (result) {
filesContent.push(result.data);
updateFileCountMessage(filesContent.length);
},
header: true,
});
}
}
function updateFileCountMessage(count) {
const messageElement = document.getElementById("fileCountMessage");
messageElement.style.fontSize = "18px";
messageElement.style.color = "#d43535";
messageElement.style.fontWeight = "bold";
messageElement.textContent = `You have added '${count}' CSV files to be merged`;
}
var now = new Date();
var day = now.getDate();
var month = now.getMonth() + 1; // Months are zero-based
var year = now.getFullYear();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
// Format the date and time components to have leading zeros if needed
day = day < 10 ? "0" + day : day;
month = month < 10 ? "0" + month : month;
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
var formattedDate =
"merged_CSV_files_" +
day +
"_" +
month +
"_" +
year +
"_" +
hours +
"_" +
minutes +
"_" +
seconds +
".csv";
function mergeFiles() {
// Merge the content of all uploaded files
const mergedContent = filesContent.flat();
// Convert the merged content to CSV
const mergedCSV = Papa.unparse(mergedContent);
// Create a Blob containing the CSV data
const blob = new Blob([mergedCSV], { type: "text/csv" });
// Create a link element to trigger the download
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = formattedDate;
// Append the link to the body and trigger the download
document.body.appendChild(link);
link.click();
// Clean up: remove the link element
document.body.removeChild(link);
}
// Attach the handleFileSelect function to the file input change event
const fileInput = document.getElementById("fileInput");
fileInput.addEventListener("change", handleFileSelect);
// Trigger the file input when the upload button is clicked
document
.querySelector(".upload-btn-wrapper .btn")
.addEventListener("click", function () {
fileInput.click();
});