From a9172dd5ef0018170a2440694df79626fc7eb41e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nut=2E=E0=B9=84=E0=B8=9B=E0=B9=80=E0=B8=A3=E0=B8=B7?= =?UTF-8?q?=E0=B9=88=E0=B8=AD=E0=B8=A2?= Date: Tue, 24 Mar 2026 11:24:07 +0700 Subject: [PATCH] Add Excel SUM formulas for header rows in equipment five-year report MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace hardcoded aggregated values with Excel SUM formulas at each hierarchy level: พื้นที่ (area) sums detail rows, คณะ (faculty) sums area rows, ผลผลิต (output) sums faculty rows, แผนงาน (plan) sums output rows — for columns I-M (price_1 through price_5). Co-Authored-By: Claude Sonnet 4.6 --- Controllers/Summary.Controller.cs | 55 +++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/Controllers/Summary.Controller.cs b/Controllers/Summary.Controller.cs index 871933a..4be1c24 100644 --- a/Controllers/Summary.Controller.cs +++ b/Controllers/Summary.Controller.cs @@ -484,10 +484,14 @@ namespace rmutr_report.Controllers ws.Range("N2:O3").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center; ws.Range("N2:O3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin; ws.Cell("N2").Style.Alignment.WrapText = true; + int planRow = -1; + var outputRows = new List(); + foreach (var equipments in summary_of_equipments.header_data) { if (equipments.color == 1) { + planRow = row; ws.Cell(row, 1).Value = null; ws.Cell(row, 2).Value = equipments.list; ws.Cell(row, 3).Value = null; @@ -548,8 +552,13 @@ namespace rmutr_report.Controllers row++; } + int outputRow = -1; + var facultyRowsForOutput = new List(); + if (equipments.color == 2) { + outputRow = row; + outputRows.Add(outputRow); ws.Cell(row, 1).Value = null; ws.Cell(row, 2).Value = equipments.list; ws.Cell(row, 3).Value = null; @@ -612,8 +621,13 @@ namespace rmutr_report.Controllers foreach (var equipments1 in equipments.data) { + int facultyRow = -1; + var areaRowsForFaculty = new List(); + if (equipments1.color == 3) { + facultyRow = row; + facultyRowsForOutput.Add(facultyRow); ws.Cell(row, 1).Value = null; ws.Cell(row, 2).Value = equipments1.faculty_name; ws.Cell(row, 3).Value = null; @@ -677,8 +691,12 @@ namespace rmutr_report.Controllers foreach (var equipments2 in equipments1.data) { + int areaRow = -1; + if (equipments2.color == 4) { + areaRow = row; + areaRowsForFaculty.Add(areaRow); ws.Cell(row, 1).Value = null; ws.Cell(row, 2).Value = equipments2.list; ws.Cell(row, 3).Value = null; @@ -740,6 +758,7 @@ namespace rmutr_report.Controllers } + int detailStartRow = row; foreach (var equipment3 in equipments2.data) { ws.Cell(row, 1).Value = no; @@ -845,7 +864,43 @@ namespace rmutr_report.Controllers row++; } + // SUM formula: พื้นที่ (color=4) = SUM of detail rows + if (areaRow != -1 && detailStartRow <= row - 1) + { + for (int c = 9; c <= 13; c++) + { + string col = ((char)('A' + c - 1)).ToString(); + ws.Cell(areaRow, c).FormulaA1 = $"SUM({col}{detailStartRow}:{col}{row - 1})"; + } + } } + // SUM formula: คณะ (color=3) = SUM of area header rows + if (facultyRow != -1 && areaRowsForFaculty.Count > 0) + { + for (int c = 9; c <= 13; c++) + { + string col = ((char)('A' + c - 1)).ToString(); + ws.Cell(facultyRow, c).FormulaA1 = $"SUM({string.Join(",", areaRowsForFaculty.Select(r => $"{col}{r}"))})"; + } + } + } + // SUM formula: ผลผลิต (color=2) = SUM of faculty header rows + if (outputRow != -1 && facultyRowsForOutput.Count > 0) + { + for (int c = 9; c <= 13; c++) + { + string col = ((char)('A' + c - 1)).ToString(); + ws.Cell(outputRow, c).FormulaA1 = $"SUM({string.Join(",", facultyRowsForOutput.Select(r => $"{col}{r}"))})"; + } + } + } + // SUM formula: แผนงาน (color=1) = SUM of output header rows + if (planRow != -1 && outputRows.Count > 0) + { + for (int c = 9; c <= 13; c++) + { + string col = ((char)('A' + c - 1)).ToString(); + ws.Cell(planRow, c).FormulaA1 = $"SUM({string.Join(",", outputRows.Select(r => $"{col}{r}"))})"; } } }