From 9276317f78a557e17a4bdabb9bbc2723ab22ee69 Mon Sep 17 00:00:00 2001 From: dvkdo <89021521+devkdo@users.noreply.github.com> Date: Tue, 8 Oct 2024 04:33:34 +0000 Subject: [PATCH 01/10] init --- esl/index.html | 24 +++++++++++++ esl/index.js | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 esl/index.html create mode 100644 esl/index.js diff --git a/esl/index.html b/esl/index.html new file mode 100644 index 0000000..b2892d2 --- /dev/null +++ b/esl/index.html @@ -0,0 +1,24 @@ + + + + + + + Class Sort by Level + + + + +
Upload XLS or XLSX file of Student Registration
+
+ + +
+
+ + + + + + \ No newline at end of file diff --git a/esl/index.js b/esl/index.js new file mode 100644 index 0000000..6815214 --- /dev/null +++ b/esl/index.js @@ -0,0 +1,98 @@ +const Q_NAME = "Nombre y Apellidos - Nomes e Sobrenomes - First and Last Names"; +const Q_EMAIL = "Email Address"; +const Q_LEVEL = + "¿Cuál es su nivel de inglés? Qual é o seu nível de inglês? What is your level of English?"; +const Q_DAY = + "Which night do you prefer? ¿Qué noche prefiere? Qual noite você prefere?"; + +const A_LEVEL1 = + "I don't speak or understand any English. No hablo ni entiendo nada de inglés. Eu não falo nem entendo nenhum inglês."; +const A_LEVEL2 = + "I speak and understand a little bit of English. Hablo y entiendo un poco de inglés. Falo e entendo um pouco de inglês."; +const A_LEVEL3 = + "I am comfortable having short conversations in English. Estoy cómodo para tener conversaciones cortas en inglés. Sinto-me confortável em conversas curtas em inglês."; +const A_DAYMON = "Monday - lunes - segunda-feira"; +const A_DAYTUE = "Tuesday - martes - terça-feira"; + +function processXLS() { + const fileInput = document.getElementById("input-file"); + const file = fileInput.files[0]; + + console.log(file); + + if (file) { + const reader = new FileReader(); + reader.onload = function (e) { + const fileData = new Uint8Array(e.target.result); + const workbook = XLSX.read(fileData, { type: "array" }); + const sheetName = workbook.SheetNames[0]; + const worksheet = workbook.Sheets[sheetName]; + const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); + + const headers = jsonData[0]; + const splitData = jsonData.slice(1).map((row) => { + let obj = {}; + headers.forEach((header, index) => { + if ( + header === Q_NAME || + header === Q_EMAIL || + header === Q_DAY || + header === Q_LEVEL + ) { + obj[header] = row[index]; + } + }); + return obj; + }); + + const mon1 = []; + const mon2 = []; + const monAny = []; + const tue1 = []; + const tue2 = []; + const tueAny = []; + + splitData.forEach((student) => { + const name = student[Q_NAME]; + const email = student[Q_EMAIL]; + const day = student[Q_DAY]; + const level = student[Q_LEVEL]; + const studentInfo = { name, email }; + + switch (day) { + case A_DAYMON: + if (level === A_LEVEL1) mon1.push(studentInfo); + else if (level === A_LEVEL3) mon2.push(studentInfo); + else monAny.push(studentInfo); + break; + + case A_DAYTUE: + if (level === A_LEVEL1) tue1.push(studentInfo); + else if (level === A_LEVEL3) tue2.push(studentInfo); + else tueAny.push(studentInfo); + break; + + default: + break; + } + }); + + displayLists(mon1, mon2, tue1, tue2); + }; + reader.readAsArrayBuffer(file); + } else { + alert("Please upload an XLS/XLSX file."); + } +} + +function displayLists(l1, l2, l3, l4) { + const output = document.getElementById("output"); + output.innerHTML = ` +

Monday Beginner

${JSON.stringify(l1, null, 2)}
+

Monday Non-Beginner

${JSON.stringify(l2, null, 2)}
+

Tuesday Beginner

${JSON.stringify(l3, null, 2)}
+

Tuesday Non-Beginner

${JSON.stringify(l4, null, 2)}
+ `; +} + +document.getElementById("input-file").addEventListener("change", processXLS); From 8d6322e99a8a2e6f8fd0ca966872f66403c81983 Mon Sep 17 00:00:00 2001 From: dvkdo <89021521+devkdo@users.noreply.github.com> Date: Wed, 9 Oct 2024 13:47:14 +0000 Subject: [PATCH 02/10] rename js, add evenDistribute() --- esl/index.html | 2 +- esl/{index.js => script.js} | 71 +++++++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 4 deletions(-) rename esl/{index.js => script.js} (64%) diff --git a/esl/index.html b/esl/index.html index b2892d2..5b27599 100644 --- a/esl/index.html +++ b/esl/index.html @@ -12,7 +12,7 @@
Upload XLS or XLSX file of Student Registration
- +
diff --git a/esl/index.js b/esl/script.js similarity index 64% rename from esl/index.js rename to esl/script.js index 6815214..b233be8 100644 --- a/esl/index.js +++ b/esl/script.js @@ -76,8 +76,9 @@ function processXLS() { break; } }); - - displayLists(mon1, mon2, tue1, tue2); + evenDistribute(monAny, mon1, mon2); + evenDistribute(tueAny, tue1, tue2); + // displayLists(mon1, mon2, tue1, tue2); }; reader.readAsArrayBuffer(file); } else { @@ -85,6 +86,69 @@ function processXLS() { } } +function evenDistribute(anyList, l1, l2) { + if (anyList.length == 0) return; + + const list1 = l1.slice(); + const list2 = l2.slice(); + const listAny = anyList.slice(); + const lengthListAny = listAny.length; + + console.log( + "START LENGTHS", + "sizeAny", + listAny.length, + lengthListAny, + "size1", + list1.length, + "size2", + list2.length + ); + + let loop = 0; + let done = false; + while (!done || listAny.length > 0) { + // default: case 0: same length + // split listany and add half to each + + // case 1: first list is bigger + // splice listany and add difference to second list + // split remaining listany between both lists + + // case 2: second list is bigger + // splice listany and add difference to first list + // split remaining listany between both lists + + let diff = list1.length - list2.length; + let maxIndex = + Math.abs(diff) < listAny.length ? diff - 1 : listAny.length - 1; + if (maxIndex == 0) maxIndex = 1; + // console.log("diff", diff); + // console.log("maxIndex", maxIndex); + + if (diff > 0) { + list2.push(...listAny.splice(0, maxIndex)); + } else if (diff < 0) { + list1.push(...listAny.splice(0, maxIndex)); + } else { + list1.push(...listAny.splice(0, listAny.length / 2)); + list2.push(...listAny.splice(0, listAny.length)); + done = true; + } + loop++; + } + console.log("loop count", loop); + console.log( + "FINAL LENGTHS", + "sizeAny", + listAny.length, + "size1", + list1.length, + "size2", + list2.length + ); +} + function displayLists(l1, l2, l3, l4) { const output = document.getElementById("output"); output.innerHTML = ` @@ -95,4 +159,5 @@ function displayLists(l1, l2, l3, l4) { `; } -document.getElementById("input-file").addEventListener("change", processXLS); +// document.getElementById("input-file").addEventListener("change", processXLS); +document.getElementById("btn-sort").addEventListener("click", processXLS); From 548da1d3735bc1106f052530b52b636a46ed2a0d Mon Sep 17 00:00:00 2001 From: dvkdo <89021521+devkdo@users.noreply.github.com> Date: Wed, 9 Oct 2024 14:44:01 +0000 Subject: [PATCH 03/10] lists display evenly with counts --- esl/script.js | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/esl/script.js b/esl/script.js index b233be8..fa91263 100644 --- a/esl/script.js +++ b/esl/script.js @@ -76,9 +76,9 @@ function processXLS() { break; } }); - evenDistribute(monAny, mon1, mon2); - evenDistribute(tueAny, tue1, tue2); - // displayLists(mon1, mon2, tue1, tue2); + const mondays = evenDistribute(monAny, mon1, mon2); + const tuesdays = evenDistribute(tueAny, tue1, tue2); + displayLists(mondays.beg, mondays.nonbeg, tuesdays.beg, tuesdays.nonbeg); }; reader.readAsArrayBuffer(file); } else { @@ -137,7 +137,7 @@ function evenDistribute(anyList, l1, l2) { } loop++; } - console.log("loop count", loop); + // console.log("loop count", loop); console.log( "FINAL LENGTHS", "sizeAny", @@ -147,15 +147,32 @@ function evenDistribute(anyList, l1, l2) { "size2", list2.length ); + return { beg: list1, nonbeg: list2 }; } function displayLists(l1, l2, l3, l4) { const output = document.getElementById("output"); output.innerHTML = ` -

Monday Beginner

${JSON.stringify(l1, null, 2)}
-

Monday Non-Beginner

${JSON.stringify(l2, null, 2)}
-

Tuesday Beginner

${JSON.stringify(l3, null, 2)}
-

Tuesday Non-Beginner

${JSON.stringify(l4, null, 2)}
+

Monday Beginner (${l1.length})

${JSON.stringify(
+    l1,
+    null,
+    2
+  )}
+

Monday Non-Beginner (${l2.length})

${JSON.stringify(
+    l2,
+    null,
+    2
+  )}
+

Tuesday Beginner (${l3.length})

${JSON.stringify(
+    l3,
+    null,
+    2
+  )}
+

Tuesday Non-Beginner (${l4.length})

${JSON.stringify(
+    l4,
+    null,
+    2
+  )}
`; } From 21996e5ea9fb8fc4eec452b9df18f8300b8b902f Mon Sep 17 00:00:00 2001 From: dvkdo <89021521+devkdo@users.noreply.github.com> Date: Wed, 9 Oct 2024 16:10:17 +0000 Subject: [PATCH 04/10] add styling --- esl/index.css | 11 +++++++++++ esl/index.html | 14 ++++++++------ esl/script.js | 44 +++++++++++++------------------------------- 3 files changed, 32 insertions(+), 37 deletions(-) create mode 100644 esl/index.css diff --git a/esl/index.css b/esl/index.css new file mode 100644 index 0000000..92329e0 --- /dev/null +++ b/esl/index.css @@ -0,0 +1,11 @@ +.container{ + margin: 10px; + padding: 10px; + } + .section-green { + background-color: honeydew; + color: black; + /* border: 2px solid black; */ + margin: 20px; + padding: 20px; + } \ No newline at end of file diff --git a/esl/index.html b/esl/index.html index 5b27599..9a74390 100644 --- a/esl/index.html +++ b/esl/index.html @@ -9,13 +9,15 @@ -
Upload XLS or XLSX file of Student Registration
-
- - + +
+

Upload XLS or XLSX file of Student Registration

+
+ + +
+
-
- diff --git a/esl/script.js b/esl/script.js index fa91263..fa171fd 100644 --- a/esl/script.js +++ b/esl/script.js @@ -14,12 +14,11 @@ const A_LEVEL3 = const A_DAYMON = "Monday - lunes - segunda-feira"; const A_DAYTUE = "Tuesday - martes - terça-feira"; + function processXLS() { const fileInput = document.getElementById("input-file"); const file = fileInput.files[0]; - console.log(file); - if (file) { const reader = new FileReader(); reader.onload = function (e) { @@ -94,16 +93,7 @@ function evenDistribute(anyList, l1, l2) { const listAny = anyList.slice(); const lengthListAny = listAny.length; - console.log( - "START LENGTHS", - "sizeAny", - listAny.length, - lengthListAny, - "size1", - list1.length, - "size2", - list2.length - ); + console.log(`Registered: beginner (${list1.length}), intermediate (${listAny.length}), advanced (${list2.length})`); let loop = 0; let done = false; @@ -137,44 +127,36 @@ function evenDistribute(anyList, l1, l2) { } loop++; } - // console.log("loop count", loop); - console.log( - "FINAL LENGTHS", - "sizeAny", - listAny.length, - "size1", - list1.length, - "size2", - list2.length - ); + + console.log(`Distributed: beginner (${list1.length}), intermediate (${listAny.length}), nonbeginner (${list2.length})`); + return { beg: list1, nonbeg: list2 }; } function displayLists(l1, l2, l3, l4) { const output = document.getElementById("output"); - output.innerHTML = ` + output.innerHTML = `

Monday Beginner (${l1.length})

${JSON.stringify(
     l1,
     null,
     2
-  )}
-

Monday Non-Beginner (${l2.length})

${JSON.stringify(
+  )}
+

Monday Non-Beginner (${l2.length})

${JSON.stringify(
     l2,
     null,
     2
-  )}
-

Tuesday Beginner (${l3.length})

${JSON.stringify(
+  )}
+

Tuesday Beginner (${l3.length})

${JSON.stringify(
     l3,
     null,
     2
-  )}
-

Tuesday Non-Beginner (${l4.length})

${JSON.stringify(
+  )}
+

Tuesday Non-Beginner (${l4.length})

${JSON.stringify(
     l4,
     null,
     2
-  )}
+ )}
`; } -// document.getElementById("input-file").addEventListener("change", processXLS); document.getElementById("btn-sort").addEventListener("click", processXLS); From 6fca7e2781a739f39a7398c58984601e1e12edf1 Mon Sep 17 00:00:00 2001 From: dvkdo <89021521+devkdo@users.noreply.github.com> Date: Wed, 9 Oct 2024 21:52:47 +0000 Subject: [PATCH 05/10] mailing list displays --- esl/index.css | 21 +++++++++++---------- esl/index.html | 10 +++++++--- esl/script.js | 31 +++++++++++++++++++++++++------ 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/esl/index.css b/esl/index.css index 92329e0..58b925a 100644 --- a/esl/index.css +++ b/esl/index.css @@ -1,11 +1,12 @@ -.container{ - margin: 10px; - padding: 10px; - } - .section-green { - background-color: honeydew; - color: black; +.container { + margin: 10px; + padding: 10px; +} + +.section-green { + background-color: honeydew; + color: black; /* border: 2px solid black; */ - margin: 20px; - padding: 20px; - } \ No newline at end of file + margin: 20px; + padding: 20px; +} \ No newline at end of file diff --git a/esl/index.html b/esl/index.html index 9a74390..a4a2d2b 100644 --- a/esl/index.html +++ b/esl/index.html @@ -9,13 +9,17 @@ - +
-

Upload XLS or XLSX file of Student Registration

+
+

Upload XLS or XLSX file of Student Registration

+
- + +
+
diff --git a/esl/script.js b/esl/script.js index 2874bc7..7a909ce 100644 --- a/esl/script.js +++ b/esl/script.js @@ -14,10 +14,28 @@ const A_LEVEL3 = const A_DAYMON = "Monday - lunes - segunda-feira"; const A_DAYTUE = "Tuesday - martes - terça-feira"; -document.getElementById("btn-sort").addEventListener("click", processXLS); - - -function processXLS() { +// document.getElementById("btn-sort").addEventListener("click", getClasses); +// document.getElementById("btn-emails").addEventListener("click", getEmails); +document.getElementById("btn-emails").addEventListener("click", processXLS); + +// function getEmails(){ +// let result=[]; +// processXLS(result); +// console.log("processed result", result); + +// const mondays = result[0]; +// const tuesdays = result[1]; +// displayEmails(mondays.beg, mondays.nonbeg, tuesdays.beg, tuesdays.nonbeg); + +// } +// function getClasses(){ +// const result = processXLS(); +// const mondays = result[0]; +// const tuesdays = result[1]; +// displayLists(mondays.beg, mondays.nonbeg, tuesdays.beg, tuesdays.nonbeg); +// } + +function processXLS(result) { const fileInput = document.getElementById("input-file"); const file = fileInput.files[0]; @@ -79,10 +97,12 @@ function processXLS() { }); const mondays = evenDistribute(monAny, mon1, mon2); const tuesdays = evenDistribute(tueAny, tue1, tue2); - // displayLists(mondays.beg, mondays.nonbeg, tuesdays.beg, tuesdays.nonbeg); displayEmails(mondays.beg, mondays.nonbeg, tuesdays.beg, tuesdays.nonbeg); + // result = (new Array([mondays, tuesdays])); + // console.log("result", result); }; reader.readAsArrayBuffer(file); + } else { alert("Please upload an XLS/XLSX file."); } @@ -130,15 +150,15 @@ function evenDistribute(anyList, l1, l2) { } loop++; } - - console.log(`Distributed: beginner (${list1.length}), intermediate (${listAny.length}), nonbeginner (${list2.length})`); - + + console.log(`Distributed: beginner (${list1.length}), intermediate (${listAny.length}), nonbeginner (${list2.length})`); + return { beg: list1, nonbeg: list2 }; } -function getEmailString(list) { +function getEmailString(list){ let emails = ""; - for (let student of list.values()) { + for(let student of list.values()){ const entry = `"${student.name}"<${student.email}>,`; emails += entry; } @@ -146,11 +166,25 @@ function getEmailString(list) { } function displayEmails(l1, l2, l3, l4) { - const emailstring = getEmailString(l1); - const output = document.getElementById("output"); - output.classList.add("section-green"); - output.innerHTML = `

Mailing list

Monday Beginner (${l1.length})`; - output.textContent += emailstring; + const outputContainer = document.getElementById("output-container"); + outputContainer.classList.add("section-green"); + + const outputHeaders1 = document.getElementById("output-headers1"); + const outputHeaders2 = document.getElementById("output-headers2"); + const outputHeaders3 = document.getElementById("output-headers3"); + const outputHeaders4 = document.getElementById("output-headers4"); + const output1 = document.getElementById("output1"); + const output2 = document.getElementById("output2"); + const output3 = document.getElementById("output3"); + const output4 = document.getElementById("output4"); + outputHeaders1.innerHTML = `

Mailing list

Monday Beginner (${l1.length})`; + outputHeaders2.innerHTML = `

Mailing list

Monday NonBeginner (${l2.length})`; + outputHeaders3.innerHTML = `

Mailing list

Tuesday Beginner (${l3.length})`; + outputHeaders4.innerHTML = `

Mailing list

Tuesday NonBeginner (${l4.length})`; + output1.textContent += getEmailString(l1); + output2.textContent += getEmailString(l2); + output3.textContent += getEmailString(l3); + output4.textContent += getEmailString(l4); } function displayLists(l1, l2, l3, l4) { From 252d8b90eae5f4af6d54b84a58d5aaffd1ab00b6 Mon Sep 17 00:00:00 2001 From: dvkdo <89021521+devkdo@users.noreply.github.com> Date: Thu, 10 Oct 2024 17:28:11 +0000 Subject: [PATCH 07/10] working pen --- esl/index.css | 12 ++-- esl/index.html | 8 +-- esl/script.js | 147 +++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 129 insertions(+), 38 deletions(-) diff --git a/esl/index.css b/esl/index.css index 58b925a..99998e9 100644 --- a/esl/index.css +++ b/esl/index.css @@ -1,12 +1,14 @@ -.container { +.container{ margin: 10px; padding: 10px; } - .section-green { background-color: honeydew; color: black; - /* border: 2px solid black; */ +/* border: 2px solid black; */ margin: 20px; - padding: 20px; -} \ No newline at end of file + padding: 20px; +} +.gap-bottom{ + margin-bottom: 20px; +} diff --git a/esl/index.html b/esl/index.html index 8833274..219f04d 100644 --- a/esl/index.html +++ b/esl/index.html @@ -19,13 +19,13 @@
-
+
-
+
-
+
-
+
- + +
-

Upload XLS or XLSX file of Student Registration

+
+

Upload XLS or XLSX file of Student Registration

+
-
-
-
-
-
-
-
-
-
-
+ +
+
+
+
+
+
+
+
+
+
+
+
+
+ - + \ No newline at end of file diff --git a/esl/script.js b/esl/script.js index ec13f4e..86eba45 100644 --- a/esl/script.js +++ b/esl/script.js @@ -1,40 +1,83 @@ -const Q_NAME = "Nombre y Apellidos - Nomes e Sobrenomes - First and Last Names"; -const Q_EMAIL = "Email Address"; -const Q_LEVEL = - "¿Cuál es su nivel de inglés? Qual é o seu nível de inglês? What is your level of English?"; -const Q_DAY = - "Which night do you prefer? ¿Qué noche prefiere? Qual noite você prefere?"; - -const A_LEVEL1 = - "I don't speak or understand any English. No hablo ni entiendo nada de inglés. Eu não falo nem entendo nenhum inglês."; -const A_LEVEL2 = - "I speak and understand a little bit of English. Hablo y entiendo un poco de inglés. Falo e entendo um pouco de inglês."; -const A_LEVEL3 = - "I am comfortable having short conversations in English. Estoy cómodo para tener conversaciones cortas en inglés. Sinto-me confortável em conversas curtas em inglês."; -const A_DAYMON = "Monday - lunes - segunda-feira"; -const A_DAYTUE = "Tuesday - martes - terça-feira"; - -// document.getElementById("btn-sort").addEventListener("click", getClasses); -// document.getElementById("btn-emails").addEventListener("click", getEmails); +import constants from './constants.js'; + document.getElementById("btn-sort").addEventListener("click", processXLSClasses); document.getElementById("btn-emails").addEventListener("click", processXLSEmails); +document.getElementById("btn-students").addEventListener("click", processXLSEmailsAndPhone); +// document.getElementById("btn-dl").addEventListener("click", processAttendance); -// function getEmails(){ -// let result=[]; -// processXLS(result); -// console.log("processed result", result); - -// const mondays = result[0]; -// const tuesdays = result[1]; -// displayEmails(mondays.beg, mondays.nonbeg, tuesdays.beg, tuesdays.nonbeg); - -// } -// function getClasses(){ -// const result = processXLS(); -// const mondays = result[0]; -// const tuesdays = result[1]; -// displayLists(mondays.beg, mondays.nonbeg, tuesdays.beg, tuesdays.nonbeg); -// } +function callProcess(){ + resetOutput(); + const fileInput = document.getElementById("input-file"); + const { mondays, tuesdays } = processXLS(fileInput); + displayLists(mondays.beg, mondays.nonbeg, tuesdays.beg, tuesdays.nonbeg); + console.log("result", mondays, tuesdays); +} + +function processXLS(fileInput) { + const file = fileInput.files[0]; + + if (file) { + const reader = new FileReader(); + reader.onload = function (e) { + const fileData = new Uint8Array(e.target.result); + const workbook = XLSX.read(fileData, { type: "array" }); + const sheetName = workbook.SheetNames[0]; + const worksheet = workbook.Sheets[sheetName]; + const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); + const headers = jsonData[0]; + + const splitData = jsonData.slice(1).map((row) => { + let obj = {}; + headers.forEach((header, index) => { + if ( + header === constants.Q_NAME || + header === constants.Q_EMAIL || + header === constants.Q_DAY || + header === constants.Q_LEVEL + ) { + obj[header] = row[index]; + } + }); + return obj; + }); + + const mon1 = [], mon2 = [], monAny = []; + const tue1 = [], tue2 = [], tueAny = []; + + splitData.forEach((student) => { + const name = student[constants.Q_NAME]; + const email = student[constants.Q_EMAIL]; + const day = student[constants.Q_DAY]; + const level = student[constants.Q_LEVEL]; + const studentInfo = { name, email }; + + switch (day) { + case constants.A_DAYMON: + if (level === constants.A_LEVEL1) mon1.push(studentInfo); + else if (level === constants.A_LEVEL3) mon2.push(studentInfo); + else monAny.push(studentInfo); + break; + case constants.A_DAYTUE: + if (level === constants.A_LEVEL1) tue1.push(studentInfo); + else if (level === constants.A_LEVEL3) tue2.push(studentInfo); + else tueAny.push(studentInfo); + break; + default: + break; + } + }); + + const mondays = evenDistribute(monAny, mon1, mon2); + const tuesdays = evenDistribute(tueAny, tue1, tue2); + + // Return the lists for Mondays and Tuesdays + return { mondays, tuesdays }; + }; + reader.readAsArrayBuffer(file); + } else { + alert("Please upload an XLS/XLSX file."); + } +} function processXLSEmails() { resetOutput(); @@ -55,10 +98,10 @@ function processXLSEmails() { let obj = {}; headers.forEach((header, index) => { if ( - header === Q_NAME || - header === Q_EMAIL || - header === Q_DAY || - header === Q_LEVEL + header === constants.Q_NAME || + header === constants.Q_EMAIL || + header === constants.Q_DAY || + header === constants.Q_LEVEL ) { obj[header] = row[index]; } @@ -74,22 +117,22 @@ function processXLSEmails() { const tueAny = []; splitData.forEach((student) => { - const name = student[Q_NAME]; - const email = student[Q_EMAIL]; - const day = student[Q_DAY]; - const level = student[Q_LEVEL]; + const name = student[constants.Q_NAME]; + const email = student[constants.Q_EMAIL]; + const day = student[constants.Q_DAY]; + const level = student[constants.Q_LEVEL]; const studentInfo = { name, email }; switch (day) { - case A_DAYMON: - if (level === A_LEVEL1) mon1.push(studentInfo); - else if (level === A_LEVEL3) mon2.push(studentInfo); + case constants.A_DAYMON: + if (level === constants.A_LEVEL1) mon1.push(studentInfo); + else if (level === constants.A_LEVEL3) mon2.push(studentInfo); else monAny.push(studentInfo); break; - case A_DAYTUE: - if (level === A_LEVEL1) tue1.push(studentInfo); - else if (level === A_LEVEL3) tue2.push(studentInfo); + case constants.A_DAYTUE: + if (level === constants.A_LEVEL1) tue1.push(studentInfo); + else if (level === constants.A_LEVEL3) tue2.push(studentInfo); else tueAny.push(studentInfo); break; @@ -100,6 +143,85 @@ function processXLSEmails() { const mondays = evenDistribute(monAny, mon1, mon2); const tuesdays = evenDistribute(tueAny, tue1, tue2); displayEmails(mondays.beg, mondays.nonbeg, tuesdays.beg, tuesdays.nonbeg); + // createExcel(mondays.beg, mondays.nonbeg, tuesdays.beg, tuesdays.nonbeg); + + // result = (new Array([mondays, tuesdays])); + // console.log("result", result); + }; + reader.readAsArrayBuffer(file); + + } else { + alert("Please upload an XLS/XLSX file."); + } +} +function processXLSEmailsAndPhone() { + resetOutput(); + const fileInput = document.getElementById("input-file"); + const file = fileInput.files[0]; + + if (file) { + const reader = new FileReader(); + reader.onload = function (e) { + const fileData = new Uint8Array(e.target.result); + const workbook = XLSX.read(fileData, { type: "array" }); + const sheetName = workbook.SheetNames[0]; + const worksheet = workbook.Sheets[sheetName]; + const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); + + const headers = jsonData[0]; + const splitData = jsonData.slice(1).map((row) => { + let obj = {}; + headers.forEach((header, index) => { + if ( + header === constants.Q_NAME || + header === constants.Q_EMAIL || + header === constants.Q_PHONE || + header === constants.Q_DAY || + header === constants.Q_LEVEL + ) { + obj[header] = row[index]; + } + }); + return obj; + }); + + const mon1 = []; + const mon2 = []; + const monAny = []; + const tue1 = []; + const tue2 = []; + const tueAny = []; + + splitData.forEach((student) => { + const name = student[constants.Q_NAME]; + const email = student[constants.Q_EMAIL]; + const phone = student[constants.Q_PHONE]; + const day = student[constants.Q_DAY]; + const level = student[constants.Q_LEVEL]; + const studentInfo = { name, email, phone }; + + switch (day) { + case constants.A_DAYMON: + if (level === constants.A_LEVEL1) mon1.push(studentInfo); + else if (level === constants.A_LEVEL3) mon2.push(studentInfo); + else monAny.push(studentInfo); + break; + + case constants.A_DAYTUE: + if (level === constants.A_LEVEL1) tue1.push(studentInfo); + else if (level === constants.A_LEVEL3) tue2.push(studentInfo); + else tueAny.push(studentInfo); + break; + + default: + break; + } + }); + const mondays = evenDistribute(monAny, mon1, mon2); + const tuesdays = evenDistribute(tueAny, tue1, tue2); + displayStudentInfo(mondays.beg, mondays.nonbeg, tuesdays.beg, tuesdays.nonbeg); + // createExcel(mondays.beg, mondays.nonbeg, tuesdays.beg, tuesdays.nonbeg); + // result = (new Array([mondays, tuesdays])); // console.log("result", result); }; @@ -128,10 +250,10 @@ function processXLSClasses() { let obj = {}; headers.forEach((header, index) => { if ( - header === Q_NAME || - header === Q_EMAIL || - header === Q_DAY || - header === Q_LEVEL + header === constants.Q_NAME || + header === constants.Q_EMAIL || + header === constants.Q_DAY || + header === constants.Q_LEVEL ) { obj[header] = row[index]; } @@ -147,22 +269,22 @@ function processXLSClasses() { const tueAny = []; splitData.forEach((student) => { - const name = student[Q_NAME]; - const email = student[Q_EMAIL]; - const day = student[Q_DAY]; - const level = student[Q_LEVEL]; + const name = student[constants.Q_NAME]; + const email = student[constants.Q_EMAIL]; + const day = student[constants.Q_DAY]; + const level = student[constants.Q_LEVEL]; const studentInfo = { name, email }; switch (day) { - case A_DAYMON: - if (level === A_LEVEL1) mon1.push(studentInfo); - else if (level === A_LEVEL3) mon2.push(studentInfo); + case constants.A_DAYMON: + if (level === constants.A_LEVEL1) mon1.push(studentInfo); + else if (level === constants.A_LEVEL3) mon2.push(studentInfo); else monAny.push(studentInfo); break; - case A_DAYTUE: - if (level === A_LEVEL1) tue1.push(studentInfo); - else if (level === A_LEVEL3) tue2.push(studentInfo); + case constants.A_DAYTUE: + if (level === constants.A_LEVEL1) tue1.push(studentInfo); + else if (level === constants.A_LEVEL3) tue2.push(studentInfo); else tueAny.push(studentInfo); break; @@ -173,6 +295,7 @@ function processXLSClasses() { const mondays = evenDistribute(monAny, mon1, mon2); const tuesdays = evenDistribute(tueAny, tue1, tue2); displayLists(mondays.beg, mondays.nonbeg, tuesdays.beg, tuesdays.nonbeg); + // createExcel(mondays.beg, mondays.nonbeg, tuesdays.beg, tuesdays.nonbeg); }; reader.readAsArrayBuffer(file); @@ -206,7 +329,7 @@ function evenDistribute(anyList, l1, l2) { let diff = list1.length - list2.length; let maxIndex = - Math.abs(diff) < listAny.length ? diff - 1 : listAny.length - 1; + Math.abs(diff) < listAny.length ? Math.abs(diff) - 1 : listAny.length - 1; if (maxIndex == 0) maxIndex = 1; // console.log("diff", diff); // console.log("maxIndex", maxIndex); @@ -236,7 +359,50 @@ function getEmailString(list){ } return emails; } +function getStudentInfoString(list){ + let names = ""; + let emails = ""; + let numbers = ""; + let infoString = ""; + for(let student of list.values()){ + const entry = `${student.name}, ${student.email}, ${student.phone}
`; + names += "\n" + student.name; + emails += "\n" + student.email; + numbers += "\n" + student.phone; + infoString += entry; + } + console.log(names); + console.log(emails); + console.log(numbers); + console.log(infoString); + return infoString; +} +function displayStudentInfo(l1, l2, l3, l4) { + const outputContainer = document.getElementById("output-container"); + outputContainer.classList.add("section-green"); + + const outputHeaders1 = document.getElementById("output-headers1"); + const outputHeaders2 = document.getElementById("output-headers2"); + const outputHeaders3 = document.getElementById("output-headers3"); + const outputHeaders4 = document.getElementById("output-headers4"); + const output1 = document.getElementById("output1"); + const output2 = document.getElementById("output2"); + const output3 = document.getElementById("output3"); + const output4 = document.getElementById("output4"); + outputHeaders1.innerHTML = `

Student Info

Monday Beginner (${l1.length})`; + outputHeaders2.innerHTML = `Monday NonBeginner (${l2.length})`; + outputHeaders3.innerHTML = `Tuesday Beginner (${l3.length})`; + outputHeaders4.innerHTML = `Tuesday NonBeginner (${l4.length})`; + console.log(outputHeaders1.textContent); + output1.innerHTML = getStudentInfoString(l1); + console.log(outputHeaders2.textContent); + output2.innerHTML = getStudentInfoString(l2); + console.log(outputHeaders3.textContent); + output3.innerHTML = getStudentInfoString(l3); + console.log(outputHeaders4.textContent); + output4.innerHTML = getStudentInfoString(l4); +} function displayEmails(l1, l2, l3, l4) { const outputContainer = document.getElementById("output-container"); outputContainer.classList.add("section-green"); @@ -301,4 +467,45 @@ function resetOutput(){ output2.textContent = ``; output3.textContent = ``; output4.textContent = ``; -} \ No newline at end of file +} + + +function createExcel(data1, data2, data3, data4) { + // Create a new workbook + const workbook = XLSX.utils.book_new(); + + // Create worksheets for each data array + createWorksheet(data1, 'Class1'); + createWorksheet(data2, 'Class2'); + createWorksheet(data3, 'Class3'); + createWorksheet(data4, 'Class4'); + + const wbout = XLSX.write(workbook, { bookType: 'xlsx', type: 'binary' }); + + const blob = new Blob([s2ab(wbout)], { type: 'application/octet-stream' }); + const url = URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + a.download = 'attendance.xlsx'; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + + // Write the workbook to a file + // XLSX.writeFile(workbook, 'attendance.xlsx'); +} + + function createWorksheet(data, sheetName) { + const worksheetData = data.map(item => [item.name, item.email]); + const worksheet = XLSX.utils.aoa_to_sheet([['Name', 'Email'], ...worksheetData]); + XLSX.utils.book_append_sheet(workbook, worksheet, sheetName); + } + +function s2ab(s) { + const buf = new ArrayBuffer(s.length); + const view = new Uint8Array(buf); // view the buffer as array of 8-bit unsigned int + for (let i = 0; i < s.length; i++) { + view[i] = s.charCodeAt(i) & 0xFF; // Assign each character's code to the buffer + } + return buf; +} diff --git a/esl/index.css b/esl/styles.css similarity index 100% rename from esl/index.css rename to esl/styles.css From 70d90caf113658ff0db0eb2489f716ac3cf6af82 Mon Sep 17 00:00:00 2001 From: dekaido Date: Sat, 19 Oct 2024 16:58:05 -0400 Subject: [PATCH 10/10] fix for phones --- esl/script.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/esl/script.js b/esl/script.js index 86eba45..8e133c0 100644 --- a/esl/script.js +++ b/esl/script.js @@ -118,7 +118,7 @@ function processXLSEmails() { splitData.forEach((student) => { const name = student[constants.Q_NAME]; - const email = student[constants.Q_EMAIL]; + const email = validateAndFixEmail(student[constants.Q_EMAIL]); const day = student[constants.Q_DAY]; const level = student[constants.Q_LEVEL]; const studentInfo = { name, email }; @@ -194,8 +194,8 @@ function processXLSEmailsAndPhone() { splitData.forEach((student) => { const name = student[constants.Q_NAME]; - const email = student[constants.Q_EMAIL]; - const phone = student[constants.Q_PHONE]; + const email = validateAndFixEmail(student[constants.Q_EMAIL]); + const phone = validateAndFixPhone( student[constants.Q_PHONE]); const day = student[constants.Q_DAY]; const level = student[constants.Q_LEVEL]; const studentInfo = { name, email, phone }; @@ -390,7 +390,7 @@ function displayStudentInfo(l1, l2, l3, l4) { const output2 = document.getElementById("output2"); const output3 = document.getElementById("output3"); const output4 = document.getElementById("output4"); - outputHeaders1.innerHTML = `

Student Info

Monday Beginner (${l1.length})`; + outputHeaders1.innerHTML = `

Student Info

Monday Beginner (${l1.length})`; outputHeaders2.innerHTML = `Monday NonBeginner (${l2.length})`; outputHeaders3.innerHTML = `Tuesday Beginner (${l3.length})`; outputHeaders4.innerHTML = `Tuesday NonBeginner (${l4.length})`; @@ -509,3 +509,54 @@ function s2ab(s) { } return buf; } +function validateAndFixEmail(email) { + // Regular expression to validate email format + const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + + // Function to fix common domain typos + function fixDomainTypos(email) { + // List of common typos and their corrections + const typoCorrections = { + 'or': 'org', + 'cmo': 'com', + 'con': 'com', + 'co': 'com', + 'cm': 'com' + }; + + return email.replace(/(@[^\s@]+\.)[^@]+$/, function(match, domain) { + return domain + (typoCorrections[match.slice(domain.length)] || match.slice(domain.length)); + }); + } + + // Fix domain typos + let correctedEmail = fixDomainTypos(email); + + // Validate corrected email + if (emailPattern.test(correctedEmail)) { + console.log('Email', email, 'Corrected Email:', correctedEmail); + return correctedEmail; + } else { + console.error('Invalid email format.', email); + return "invalid:" + email; + } +} +function validateAndFixPhone(phone) { + // Remove all non-digit characters + let digits = phone.toString().replace(/\D/g, ''); + + let correctedPhone = ""; + // Format based on length and starting digit + if (digits.length === 10) { + correctedPhone = digits.replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3'); + } else if (digits.length === 11 && digits.startsWith('1')) { + correctedPhone = digits.replace(/(\d{1})(\d{3})(\d{3})(\d{4})/, '$1-$2-$3-$4'); + } else if (digits.length > 10) { + correctedPhone = digits.replace(/(\d+)(\d{3})(\d{3})(\d{4})/, '$1-$2-$3-$4'); + } else { + console.error('Invalid phone format.', phone); + return "invalid:" + phone; + } + console.log('Phone', phone, 'Corrected Phone:', correctedPhone); + return correctedPhone; +}