I recently posted some code for review, and am now looking to gather some feedback on my latest implementation with that code. Yes, I have fallen into the OOP rabbit hole (thanks Mathieu Guindon) and am curious if my OOP approach is on the right track.
Some background: I am creating a Chart Workbook from scratch via data that is generated from a Bot at work. Basically I take the data from the Bot generated Workbook, store it into an Array and then use a Scripting Dictionary
to sort out all the duplicates, this approach works great! My code below is merely just the Worksheet part of my project, and am only at the point of creating the Headers for said chart.
Am I on the right track so far?
IChartFormatService
My hope was to separate this concern from my
ChartWorksheet
class. Why would I want to do that? In the future I may need to implement different styles for different reasons, based on customer / work needs. I can have a particularWorksheet
implement a particular flavor of colors for different representations.
'@Interface
Option Explicit
Public Sub FormatProductHeaderLabel()
End Sub
Public Sub FormatServiceHeaderLabel()
End Sub
Here is one implementation:
StandardChartWorkSheet
'@PredeclaredId
Option Explicit
Implements IChartFormatService
Implements IChart
Private Const ProductHeaderFont As String = "Arial"
Private Const ProductHeaderFontSize As Integer = 12
Private Const ProductHeaderFontColor As Long = 16777215
Private Const ServiceHeaderFont As String = "Arial"
Private Const ServiceHeaderFontSize As Integer = 10
Private Const ServiceHeaderFontColor As Long = 0
Public Enum ChartColor
InteriorProductColumnColor = 12549120
InteriorServiceColumnColor = 14277081
End Enum
Private Type TChartWorksheetService
HeaderColumn As Long
HeaderData As Scripting.Dictionary
ChartWorksheet As Worksheet
End Type
Private this As TChartWorksheetService
Public Function Create(ByVal hData As Scripting.Dictionary, cSheet As Worksheet) As IChart
With New StandardChartWorksheet
Set .HeaderData = hData
Set .ChartWorksheet = cSheet
Set Create = .Self
End With
End Function
Public Property Get HeaderData() As Scripting.Dictionary
Set HeaderData = this.HeaderData
End Property
Public Property Set HeaderData(ByVal value As Scripting.Dictionary)
Set this.HeaderData = value
End Property
Public Property Get ChartWorksheet() As Worksheet
Set ChartWorksheet = this.ChartWorksheet
End Property
Public Property Set ChartWorksheet(ByVal value As Worksheet)
Set this.ChartWorksheet = value
End Property
Public Property Get HeaderColumn() As Long
HeaderColumn = this.HeaderColumn
End Property
Public Property Let HeaderColumn(ByVal value As Long)
this.HeaderColumn = value
End Property
Public Property Get Self() As IChart
Set Self = Me
End Property
Private Sub BuildHeaders()
Application.ScreenUpdating = False
Dim product As Variant
For Each product In HeaderData
PrintProductValues product
this.HeaderColumn = this.HeaderColumn + 1
Dim service As Variant
For Each service In HeaderData(product)
PrintServiceValues service
this.HeaderColumn = this.HeaderColumn + 1
Next
Next
Application.ScreenUpdating = True
End Sub
Private Sub PrintProductValues(ByVal product As String)
With this.ChartWorksheet.Range(Cells(4, this.HeaderColumn), Cells(50, this.HeaderColumn))
.Interior.Color = InteriorProductColumnColor
End With
With Sheet3.Cells(4, this.HeaderColumn)
.value = product
IChartFormatService_FormatProductHeaderLabel
End With
End Sub
Private Sub PrintServiceValues(ByVal service As String)
With this.ChartWorksheet.Cells(4, this.HeaderColumn)
.value = Mid(service, 14, 100)
IChartFormatService_FormatServiceHeaderLabel
End With
End Sub
Private Sub IChartFormatService_FormatProductHeaderLabel()
With this.ChartWorksheet.Cells(4, this.HeaderColumn)
.Font.Name = ProductHeaderFont
.Font.Size = ProductHeaderFontSize
.Font.Color = ProductHeaderFontColor
.Font.Bold = True
.Orientation = Excel.XlOrientation.xlUpward
.Columns.AutoFit
End With
End Sub
Private Sub IChartFormatService_FormatServiceHeaderLabel()
With this.ChartWorksheet.Cells(4, this.HeaderColumn)
.Interior.Color = InteriorServiceColumnColor
.Font.Name = ServiceHeaderFont
.Font.Size = ServiceHeaderFontSize
.Font.Bold = False
.Font.Color = ServiceHeaderFontColor
.Orientation = Excel.XlOrientation.xlUpward
.Columns.AutoFit
End With
End Sub
Private Sub IChart_BuildChart()
If Not this.HeaderData Is Nothing Then
BuildHeaders
Else: Exit Sub
End If
End Sub
Private Sub Class_Initialize()
this.HeaderColumn = 3
End Sub
StandardChartWorksheet
class implements another interface, IChart
basically separating the concern of building a chart
'@Interface
Option Explicit
Public Sub BuildChart()
End Sub
My sample procedure, housed in Module 1
Sub test()
Dim chart As IChart
Set chart = StandardChartWorksheet.Create(GetTMProductDictionary, Sheet3)
chart.BuildChart
End Sub
Snippet of what's produced
There are 50 more columns, cropped the picture to keep it simple.