Add original FormatSummaryTables Sub and Round Function

This commit is contained in:
2025-08-13 19:45:59 +00:00
parent 5cc8e38db5
commit 7b7694efd5

View File

@@ -114,4 +114,82 @@ Sub CopyColumnData(oSheet As Object, col As Long, oSummary As Object, outCol As
dataCell = oSheet.getCellByPosition(col, row - 1)
oSummary.getCellByPosition(outCol, outRow + (row - startRow) + 1).Value = dataCell.Value
Next row
End Sub
End Sub
Sub FormatSummaryTables()
Dim oDoc As Object, oSheet As Object
Dim outRow As Long
Dim i As Long, j As Long
Dim iRow As Long
Dim cell As Object
Dim rangeStart As Long, rangeEnd As Long
Dim categories() As String
Dim lastCol As Long
Dim cellContent As String
oDoc = ThisComponent
oSheet = oDoc.Sheets.getByName("Summary")
categories = Array("Temp", "Wind", "Rel Humidity", "Avg Total Liquid Precipitation", "Rainy Days")
outRow = 0
For i = LBound(categories) To UBound(categories)
' Title row (e.g. "Temp Data Summary")
cell = oSheet.getCellByPosition(0, outRow)
cell.CharWeight = com.sun.star.awt.FontWeight.BOLD
cell.CharHeight = 12
cell.CellBackColor = RGB(255, 255, 255) ' white
outRow = outRow + 1
' Find last used column in header row
lastCol = 0
For j = 0 To oSheet.Columns.Count - 1
cell = oSheet.getCellByPosition(j, outRow)
If Trim(cell.String) = "" Then Exit For
lastCol = j
Next j
' Header row (months + data series names)
For j = 0 To lastCol
cell = oSheet.getCellByPosition(j, outRow)
cell.CharWeight = com.sun.star.awt.FontWeight.BOLD
cell.IsTextWrapped = True
cell.CellBackColor = RGB(255, 173, 0) ' #FFAD00 orange
cell.HoriJustify = com.sun.star.table.CellHoriJustify.CENTER
cell.VertJustify = com.sun.star.table.CellVertJustify.CENTER
Next j
' Data rows (months + values)
rangeStart = outRow + 1
rangeEnd = outRow + 12 ' assuming 12 months data rows
For iRow = rangeStart To rangeEnd
For j = 0 To lastCol
cell = oSheet.getCellByPosition(j, iRow)
cellContent = Trim(cell.String)
' Only round numeric cells (leave month abbreviations and other text alone)
If IsNumeric(cellContent) And cellContent <> "" Then
cell.Value = Round(cell.Value, 1)
cell.HoriJustify = com.sun.star.table.CellHoriJustify.CENTER
Else
cell.HoriJustify = com.sun.star.table.CellHoriJustify.CENTER
End If
Next j
Next iRow
' Skip rows for next category
outRow = rangeEnd + 2
Next i
MsgBox "Summary tables formatted."
End Sub
Function Round(num, Optional decimals)
Dim factor
If IsMissing(decimals) Then
decimals = 0
End If
factor = 10 ^ decimals
Round = Int(num * factor + 0.5) / factor
End Function