first commit
This commit is contained in:
95
plan-app/bydep.js
Normal file
95
plan-app/bydep.js
Normal file
@@ -0,0 +1,95 @@
|
||||
const XLSX = require('xlsx');
|
||||
const path = require('path');
|
||||
|
||||
function runBydep(inputPath, outputPath) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!inputPath) {
|
||||
return reject(new Error('❌ ไม่พบ path ของไฟล์ Excel'));
|
||||
}
|
||||
if (!outputPath) {
|
||||
return reject(new Error('❌ ไม่พบ path สำหรับบันทึกไฟล์'));
|
||||
}
|
||||
|
||||
console.log('📂 กำลังอ่านไฟล์ (bydep):', inputPath);
|
||||
|
||||
try {
|
||||
// 📘 อ่านไฟล์ Excel
|
||||
const excel = XLSX.readFile(path.resolve(inputPath));
|
||||
const sheet = excel.Sheets[excel.SheetNames[0]];
|
||||
const data = XLSX.utils.sheet_to_json(sheet);
|
||||
|
||||
// ⚙️ ฟังก์ชันลบตัวเลขและจุดนำหน้า
|
||||
const cleanSource = (text) => {
|
||||
if (!text) return '';
|
||||
return text.toString().replace(/^\d+\.\s*/, '').trim();
|
||||
};
|
||||
|
||||
// 🧮 เตรียมข้อมูลรวมตาม "แผนกวิชา"
|
||||
const pivot = {};
|
||||
|
||||
data.forEach(row => {
|
||||
const plan = row['แผนกวิชา'] || 'ไม่ระบุแผนกวิชา';
|
||||
const source = cleanSource(row['แหล่งงบประมาณ']);
|
||||
const budget = Number(row['งบประมาณ']) || 0;
|
||||
|
||||
if (!source || !plan) return;
|
||||
|
||||
if (!pivot[plan]) pivot[plan] = {};
|
||||
if (!pivot[plan][source]) pivot[plan][source] = 0;
|
||||
|
||||
pivot[plan][source] += budget;
|
||||
});
|
||||
|
||||
// 🧾 ดึงชื่อคอลัมน์ (แหล่งงบทั้งหมด)
|
||||
const allSources = Array.from(
|
||||
new Set(Object.values(pivot).flatMap(obj => Object.keys(obj)))
|
||||
);
|
||||
|
||||
// 📊 ฟังก์ชัน format ตัวเลข
|
||||
const formatNumber = (num) =>
|
||||
num ? num.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }) : '';
|
||||
|
||||
// 🪄 สร้างตารางผลลัพธ์
|
||||
const result = [];
|
||||
let grandTotalAll = 0;
|
||||
const columnTotals = {};
|
||||
|
||||
Object.entries(pivot).forEach(([plan, sources]) => {
|
||||
const row = { 'แผนกวิชา': plan };
|
||||
let rowTotal = 0;
|
||||
|
||||
allSources.forEach(source => {
|
||||
const val = sources[source] || 0;
|
||||
row[source] = val ? formatNumber(val) : '';
|
||||
rowTotal += val;
|
||||
columnTotals[source] = (columnTotals[source] || 0) + val;
|
||||
});
|
||||
|
||||
grandTotalAll += rowTotal;
|
||||
row['รวมทั้งหมด'] = formatNumber(rowTotal);
|
||||
result.push(row);
|
||||
});
|
||||
|
||||
// ➕ เพิ่มแถวสุดท้าย “รวมทั้งหมด”
|
||||
const totalRow = { 'แผนกวิชา': 'รวมทั้งหมด' };
|
||||
allSources.forEach(source => {
|
||||
totalRow[source] = columnTotals[source] ? formatNumber(columnTotals[source]) : '';
|
||||
});
|
||||
totalRow['รวมทั้งหมด'] = formatNumber(grandTotalAll);
|
||||
result.push(totalRow);
|
||||
|
||||
const newSheet = XLSX.utils.json_to_sheet(result);
|
||||
const newBook = XLSX.utils.book_new();
|
||||
XLSX.utils.book_append_sheet(newBook, newSheet, 'Summary');
|
||||
XLSX.writeFile(newBook, outputPath); // ✅ ใช้ outputPath ที่รับเข้ามา
|
||||
|
||||
console.log(`✅ สรุปผลสำเร็จ → ${outputPath}`);
|
||||
resolve(`✅ บันทึกไฟล์สำเร็จแล้ว`);
|
||||
} catch (error) {
|
||||
console.error('❌ เกิดข้อผิดพลาดใน bydep:', error);
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { runBydep };
|
||||
Reference in New Issue
Block a user