195 lines
6.7 KiB
Plaintext
195 lines
6.7 KiB
Plaintext
Sub BuildWeatherSummaries()
|
||
Dim oDoc As Object, oSheets As Object, oSheet As Object, oSummary As Object, oRefSheet As Object
|
||
Dim sheetIndex As Long, col As Long, outCol As Long, row As Long
|
||
Dim category As String, header As String, headerLow As String
|
||
Dim categories() As String
|
||
Dim startRow As Long, endRow As Long, headerRow As Long
|
||
Dim headerCell As Object
|
||
Dim outRow As Long
|
||
|
||
categories = Array("Temp", "Wind", "Rel Humidity", "Avg Total Liquid Precipitation", "Rainy Days")
|
||
|
||
headerRow = 23
|
||
startRow = 26
|
||
endRow = 37
|
||
|
||
oDoc = ThisComponent
|
||
oSheets = oDoc.Sheets
|
||
|
||
' Get REF sheet
|
||
If Not SheetExists("REF") Then
|
||
MsgBox "Reference sheet 'REF' not found! Cannot pull month abbreviations."
|
||
Exit Sub
|
||
End If
|
||
oRefSheet = oSheets.getByName("REF")
|
||
|
||
' Create or clear the Summary sheet
|
||
If Not SheetExists("Summary") Then
|
||
oSummary = oSheets.createByName("Summary", oSheets.Count)
|
||
oSheets.insertByName("Summary", oSummary)
|
||
End If
|
||
oSummary = oSheets.getByName("Summary")
|
||
oSummary.clearContents(1023) ' clear all
|
||
|
||
outRow = 0
|
||
|
||
Dim catIndex As Long
|
||
For catIndex = LBound(categories) To UBound(categories)
|
||
category = categories(catIndex)
|
||
|
||
' Write category title
|
||
oSummary.getCellByPosition(0, outRow).String = category & " Data Summary"
|
||
outRow = outRow + 1
|
||
|
||
' Write month header
|
||
oSummary.getCellByPosition(0, outRow).String = "Month"
|
||
|
||
' Fill months (Jan–Dec) from REF sheet, column A, rows 0 to 11 (adjust if needed)
|
||
For row = 0 To 11
|
||
On Error Resume Next
|
||
oSummary.getCellByPosition(0, outRow + row + 1).String = oRefSheet.getCellByPosition(0, row).String
|
||
On Error GoTo 0
|
||
Next row
|
||
|
||
outCol = 1
|
||
|
||
' Scan all sheets for matching name
|
||
For sheetIndex = 0 To oSheets.Count - 1
|
||
oSheet = oSheets.getByIndex(sheetIndex)
|
||
If InStr(LCase(oSheet.Name), "monthly") > 0 Then
|
||
col = 0
|
||
Do While col < 100 ' limit to 100 columns max to avoid infinite loop
|
||
headerCell = oSheet.getCellByPosition(col, headerRow - 1)
|
||
header = Trim(headerCell.String)
|
||
If header = "" Then Exit Do
|
||
|
||
headerLow = LCase(header)
|
||
|
||
If category = "Temp" Then
|
||
If InStr(headerLow, "avg") > 0 And InStr(headerLow, "temp") > 0 Then
|
||
Call CopyColumnData(oSheet, col, oSummary, outCol, outRow, startRow, endRow)
|
||
outCol = outCol + 1
|
||
End If
|
||
Else
|
||
If InStr(headerLow, LCase(category)) > 0 Then
|
||
Call CopyColumnData(oSheet, col, oSummary, outCol, outRow, startRow, endRow)
|
||
outCol = outCol + 1
|
||
End If
|
||
End If
|
||
|
||
col = col + 1
|
||
Loop
|
||
End If
|
||
Next sheetIndex
|
||
|
||
outRow = outRow + (endRow - startRow + 2) + 1 ' Skip past table with some space
|
||
Next catIndex
|
||
|
||
MsgBox "Weather summaries built successfully."
|
||
End Sub
|
||
|
||
|
||
Function SheetExists(sheetName As String) As Boolean
|
||
Dim oDoc As Object, oSheets As Object
|
||
oDoc = ThisComponent
|
||
oSheets = oDoc.Sheets
|
||
SheetExists = False
|
||
Dim i As Integer
|
||
For i = 0 To oSheets.Count - 1
|
||
If oSheets.getByIndex(i).Name = sheetName Then
|
||
SheetExists = True
|
||
Exit Function
|
||
End If
|
||
Next i
|
||
End Function
|
||
|
||
Sub CopyColumnData(oSheet As Object, col As Long, oSummary As Object, outCol As Long, outRow As Long, startRow As Long, endRow As Long)
|
||
Dim row As Long, dataCell As Object, headerCell As Object, header As String
|
||
|
||
headerCell = oSheet.getCellByPosition(col, 23 - 1)
|
||
header = headerCell.String
|
||
oSummary.getCellByPosition(outCol, outRow).String = oSheet.Name & " " & header
|
||
|
||
For row = startRow To endRow
|
||
dataCell = oSheet.getCellByPosition(col, row - 1)
|
||
oSummary.getCellByPosition(outCol, outRow + (row - startRow) + 1).Value = dataCell.Value
|
||
Next row
|
||
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 |