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