كود بالذكاء الاصطناعى لنسخ بيانات التلميذ من موقع الوزاره ـ الشرح بالصور

 كود بالذكاء الاصطناعي يتيح لنا النسخ من

 موقع بيانات التلميذ بالوزرة بالاسم والرقم 

القومي والكود

 لكل التلاميذ لكن الحديث فيه انه مرتب

 ابجديا ذكور ثم اناث طريقة الشرح مثل

 الطريقة القديمة ولكن شرحتها مصور 

بها كيفيه وضع الكود من خلال الصور

ــــــــــــــــــــــــــــــــــــــــــــــــــ

(async function () {

    'use strict';


    const table = $('table').DataTable();


    if (!table) {

        alert('لم يتم العثور على جدول التلاميذ');

        return;

    }


    $('#exportAllStudentsBtn').remove();


    const btn = $(`

        <button id="exportAllStudentsBtn"

        style="

            display:block;

            margin:15px auto;

            padding:12px 25px;

            background:#198754;

            color:white;

            border:0;

            border-radius:8px;

            font-size:17px;

            font-weight:bold;

            cursor:pointer;

            direction:rtl;

        ">

        📥 تحميل كل التلاميذ إلى Excel

        </button>

    `);


    $('table').before(btn);


    btn.on('click', async function () {


        try {


            btn.prop('disabled', true);

            btn.text('⏳ جاري جمع بيانات التلاميذ...');


            const info = table.page.info();


            console.log('إجمالي التلاميذ:', info.recordsDisplay);

            console.log('عدد الصفحات:', info.pages);


            const oldPage = info.page;


            // عناوين الأعمدة

            const headers = $('table thead th')

                .map(function () {

                    return $(this).text().trim();

                })

                .get();


            console.log('الأعمدة:', headers);


            // ترتيب الأعمدة عندك:

            // 0 = كود التلميذ

            // 1 = اسم التلميذ

            // 2 = الرقم القومي

            // 3 = الصف

            // 4 = الفصل

            // 5 = الجنسية

            // 6 = فارغ


            const nameIndex = 1;

            const nationalIdIndex = 2;


            let allRows = [];


            // ==========================================

            // جمع جميع الصفحات

            // ==========================================


            for (let page = 0; page < info.pages; page++) {


                btn.text(

                    `⏳ جاري جمع الصفحة ${page + 1} من ${info.pages}...`

                );


                table.page(page).draw('page');


                await new Promise(resolve =>

                    setTimeout(resolve, 300)

                );


                $('table tbody tr').each(function () {


                    const row = $(this)

                        .find('td')

                        .map(function () {

                            return $(this).text().trim();

                        })

                        .get();


                    if (row.length) {

                        allRows.push(row);

                    }

                });

            }


            // ==========================================

            // إزالة التكرار

            // ==========================================


            const uniqueRows = [];

            const seen = new Set();


            allRows.forEach(function (row) {


                const key = row.join('|||');


                if (!seen.has(key)) {

                    seen.add(key);

                    uniqueRows.push(row);

                }


            });


            table.page(oldPage).draw('page');


            console.log(

                'تم جمع:',

                uniqueRows.length,

                'تلميذ'

            );


            if (!uniqueRows.length) {

                throw new Error('لم يتم العثور على بيانات.');

            }


            // ==========================================

            // تحديد النوع من الرقم القومي

            // ==========================================


            function getGender(row) {


                let nationalId = String(

                    row[nationalIdIndex] || ''

                );


                // إزالة أي مسافات

                nationalId = nationalId

                    .replace(/\s/g, '')

                    .trim();


                /*

                 * الرقم القومي المصري:

                 *

                 * الرقم رقم 13:

                 * فردي = ذكر

                 * زوجي = أنثى

                 *

                 * مثال:

                 * 2XXXXXXXXXXX1 = ذكر

                 * 2XXXXXXXXXXX2 = أنثى

                 */


                if (nationalId.length >= 13) {


                    const digit = nationalId.charAt(12);


                    const number = parseInt(digit, 10);


                    if (!isNaN(number)) {


                        if (number % 2 === 1) {

                            return 1; // ذكر

                        }


                        return 2; // أنثى

                    }

                }


                return 3; // غير معروف

            }


            // ==========================================

            // ترتيب:

            // ذكور أولاً

            // ثم الإناث

            // وكل مجموعة أبجديًا

            // ==========================================


            uniqueRows.sort(function (a, b) {


                const genderA = getGender(a);

                const genderB = getGender(b);


                // الذكور أولاً

                if (genderA !== genderB) {

                    return genderA - genderB;

                }


                // الاسم

                const nameA = String(

                    a[nameIndex] || ''

                ).trim();


                const nameB = String(

                    b[nameIndex] || ''

                ).trim();


                return nameA.localeCompare(

                    nameB,

                    'ar',

                    {

                        sensitivity: 'base'

                    }

                );

            });


            console.log('تم ترتيب الذكور ثم الإناث أبجديًا');


            // ==========================================

            // إنشاء ملف Excel

            // ==========================================


            let html = `

<html>

<head>

<meta charset="UTF-8">


<style>


body {

    direction:rtl;

    font-family:Tahoma,Arial;

}


table {

    border-collapse:collapse;

    direction:rtl;

}


th {

    background:#198754;

    color:white;

    border:1px solid #555;

    padding:8px;

    font-weight:bold;

    text-align:center;

}


td {

    border:1px solid #777;

    padding:7px;

    text-align:center;

    mso-number-format:"\\@";

}


</style>


</head>


<body>


<table>


<tr>

`;


            headers.forEach(function (header) {


                html +=

                    '<th>' +

                    escapeHTML(header) +

                    '</th>';


            });


            html += '</tr>';


            // ==========================================

            // إضافة البيانات المرتبة

            // ==========================================


            uniqueRows.forEach(function (row) {


                html += '<tr>';


                for (

                    let i = 0;

                    i < headers.length;

                    i++

                ) {


                    let value = row[i] || '';


                    html +=

                        '<td style="mso-number-format:\'\\@\';">' +

                        escapeHTML(value) +

                        '</td>';


                }


                html += '</tr>';


            });


            html += `

</table>

</body>

</html>

`;


            // ==========================================

            // تحميل الملف

            // ==========================================


            const blob = new Blob(

                ['\ufeff', html],

                {

                    type:

                    'application/vnd.ms-excel;charset=utf-8'

                }

            );


            const url = URL.createObjectURL(blob);


            const a = document.createElement('a');


            a.href = url;


            a.download =

                'بيانات_التلاميذ_ذكور_ثم_إناث_' +

                uniqueRows.length +

                '_تلميذ.xls';


            document.body.appendChild(a);


            a.click();


            a.remove();


            setTimeout(function () {

                URL.revokeObjectURL(url);

            }, 5000);


            btn.text(

                `✅ تم تحميل ${uniqueRows.length} تلميذ — ذكور ثم إناث`

            );


            setTimeout(function () {


                btn.text(

                    '📥 تحميل كل التلاميذ إلى Excel'

                );


                btn.prop('disabled', false);


            }, 3000);


        } catch (error) {


            console.error(error);


            alert(

                '❌ حدث خطأ:\n\n' +

                error.message

            );


            btn.text(

                '📥 تحميل كل التلاميذ إلى Excel'

            );


            btn.prop('disabled', false);

        }

    });


    function escapeHTML(text) {


        return String(text)

            .replace(/&/g, '&amp;')

            .replace(/</g, '&lt;')

            .replace(/>/g, '&gt;')

            .replace(/"/g, '&quot;')

            .replace(/'/g, '&#039;');


    }


})();

ــــــــــــــــــــــــــــــــــــــــــــــــــــ



تعليقات

المشاركات الشائعة من هذه المدونة

نتيجه الثانويه العامه بالكشوف ٢٠٢٦

ملف ترقيات دفعه ٢٠٢٠ ـ جاهز للطباعه

ملفات الانجاز لكل التخصصات للترقيات دفعه ٢٠٢٠