forked from kamonwantaengsuk/rmutr_report
Add Excel SUM formulas for header rows in equipment five-year report
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:
@@ -484,10 +484,14 @@ namespace rmutr_report.Controllers
|
|||||||
ws.Range("N2:O3").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
|
ws.Range("N2:O3").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
|
||||||
ws.Range("N2:O3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
ws.Range("N2:O3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
||||||
ws.Cell("N2").Style.Alignment.WrapText = true;
|
ws.Cell("N2").Style.Alignment.WrapText = true;
|
||||||
|
int planRow = -1;
|
||||||
|
var outputRows = new List<int>();
|
||||||
|
|
||||||
foreach (var equipments in summary_of_equipments.header_data)
|
foreach (var equipments in summary_of_equipments.header_data)
|
||||||
{
|
{
|
||||||
if (equipments.color == 1)
|
if (equipments.color == 1)
|
||||||
{
|
{
|
||||||
|
planRow = row;
|
||||||
ws.Cell(row, 1).Value = null;
|
ws.Cell(row, 1).Value = null;
|
||||||
ws.Cell(row, 2).Value = equipments.list;
|
ws.Cell(row, 2).Value = equipments.list;
|
||||||
ws.Cell(row, 3).Value = null;
|
ws.Cell(row, 3).Value = null;
|
||||||
@@ -548,8 +552,13 @@ namespace rmutr_report.Controllers
|
|||||||
row++;
|
row++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int outputRow = -1;
|
||||||
|
var facultyRowsForOutput = new List<int>();
|
||||||
|
|
||||||
if (equipments.color == 2)
|
if (equipments.color == 2)
|
||||||
{
|
{
|
||||||
|
outputRow = row;
|
||||||
|
outputRows.Add(outputRow);
|
||||||
ws.Cell(row, 1).Value = null;
|
ws.Cell(row, 1).Value = null;
|
||||||
ws.Cell(row, 2).Value = equipments.list;
|
ws.Cell(row, 2).Value = equipments.list;
|
||||||
ws.Cell(row, 3).Value = null;
|
ws.Cell(row, 3).Value = null;
|
||||||
@@ -612,8 +621,13 @@ namespace rmutr_report.Controllers
|
|||||||
|
|
||||||
foreach (var equipments1 in equipments.data)
|
foreach (var equipments1 in equipments.data)
|
||||||
{
|
{
|
||||||
|
int facultyRow = -1;
|
||||||
|
var areaRowsForFaculty = new List<int>();
|
||||||
|
|
||||||
if (equipments1.color == 3)
|
if (equipments1.color == 3)
|
||||||
{
|
{
|
||||||
|
facultyRow = row;
|
||||||
|
facultyRowsForOutput.Add(facultyRow);
|
||||||
ws.Cell(row, 1).Value = null;
|
ws.Cell(row, 1).Value = null;
|
||||||
ws.Cell(row, 2).Value = equipments1.faculty_name;
|
ws.Cell(row, 2).Value = equipments1.faculty_name;
|
||||||
ws.Cell(row, 3).Value = null;
|
ws.Cell(row, 3).Value = null;
|
||||||
@@ -677,8 +691,12 @@ namespace rmutr_report.Controllers
|
|||||||
|
|
||||||
foreach (var equipments2 in equipments1.data)
|
foreach (var equipments2 in equipments1.data)
|
||||||
{
|
{
|
||||||
|
int areaRow = -1;
|
||||||
|
|
||||||
if (equipments2.color == 4)
|
if (equipments2.color == 4)
|
||||||
{
|
{
|
||||||
|
areaRow = row;
|
||||||
|
areaRowsForFaculty.Add(areaRow);
|
||||||
ws.Cell(row, 1).Value = null;
|
ws.Cell(row, 1).Value = null;
|
||||||
ws.Cell(row, 2).Value = equipments2.list;
|
ws.Cell(row, 2).Value = equipments2.list;
|
||||||
ws.Cell(row, 3).Value = null;
|
ws.Cell(row, 3).Value = null;
|
||||||
@@ -740,6 +758,7 @@ namespace rmutr_report.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int detailStartRow = row;
|
||||||
foreach (var equipment3 in equipments2.data)
|
foreach (var equipment3 in equipments2.data)
|
||||||
{
|
{
|
||||||
ws.Cell(row, 1).Value = no;
|
ws.Cell(row, 1).Value = no;
|
||||||
@@ -845,9 +864,45 @@ namespace rmutr_report.Controllers
|
|||||||
|
|
||||||
row++;
|
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}"))})";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
using (var stream = new MemoryStream())
|
using (var stream = new MemoryStream())
|
||||||
|
|||||||
Reference in New Issue
Block a user