Add Excel SUM formulas for header rows in equipment five-year report
All checks were successful
continuous-integration/drone/push Build is passing

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 <noreply@anthropic.com>
This commit is contained in:
Nut.ไปเรื่อย
2026-03-24 11:24:07 +07:00
parent b896c28643
commit a9172dd5ef

View File

@@ -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<int>();
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<int>();
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<int>();
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}"))})";
}
}
}