Private/Add-PS365ExcelAboutSheet.ps1

function Add-PS365ExcelAboutSheet {
    <#
    .SYNOPSIS
    Finishes an Excel report: colored and frozen header row on every data worksheet, an 'About' worksheet
    with the PS365 and Clidsys branding, and the file properties.
 
    .DESCRIPTION
    Called by every PS365 Excel export right after its last Export-Excel. The worksheet is appended last, so
    the data worksheets keep their layout (Import-Excel reads them back unchanged). It holds the PS365 logo,
    the report name, the generating function, the generation date, the module version and the links to
    https://ps365.clidsys.com and https://clidsys.com. The file properties (Author, Company, Title) are set
    too, so the branding also shows in Windows Explorer and in File > Info.
 
    Branding must never break an export: any failure (file locked, logo unreadable on a platform without
    System.Drawing) is reported with Write-Verbose and the report itself is left as written.
 
    .PARAMETER Path
    Path of the .xlsx file written by Export-Excel.
 
    .PARAMETER ReportName
    Human readable report name. Defaults to the name of the calling function.
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$Path,

        [Parameter(Mandatory = $false)]
        [string]$ReportName
    )

    $callerName = (Get-PSCallStack | Select-Object -Skip 1 -First 1).Command
    if (-not $ReportName) {
        $ReportName = $callerName
    }

    $ps365Url = 'https://ps365.clidsys.com'
    $clidsysUrl = 'https://clidsys.com'
    $moduleVersion = (Get-Module -Name PS365 | Select-Object -First 1).Version

    try {
        $excelPackage = Open-ExcelPackage -Path $Path -ErrorAction Stop
    }
    catch {
        Write-Verbose "Unable to open $Path to add the About worksheet: $($_.Exception.Message)"
        return
    }

    try {
        $workbook = $excelPackage.Workbook

        # Data worksheets: header row in the brand orange (white bold text) and frozen, so it stays visible
        # while scrolling. The header is the first row of the table Export-Excel created (row 2 when the
        # export added a -Title row above it), or row 1 of a worksheet without table. The table style
        # (Light9) and its filters are kept: a direct cell format only overrides the header colors.
        $headerFill = [System.Drawing.Color]::FromArgb(184, 92, 16)
        foreach ($dataSheet in @($workbook.Worksheets)) {
            if ($dataSheet.Name -eq 'About' -or $null -eq $dataSheet.Dimension) {
                continue
            }

            $headerRow = $dataSheet.Dimension.Start.Row
            $firstColumn = $dataSheet.Dimension.Start.Column
            $lastColumn = $dataSheet.Dimension.End.Column
            if ($dataSheet.Tables.Count -gt 0) {
                $tableAddress = $dataSheet.Tables[0].Address
                $headerRow = $tableAddress.Start.Row
                $firstColumn = $tableAddress.Start.Column
                $lastColumn = $tableAddress.End.Column
            }

            $headerRange = $dataSheet.Cells[$headerRow, $firstColumn, $headerRow, $lastColumn]
            $headerRange.Style.Fill.PatternType = [OfficeOpenXml.Style.ExcelFillStyle]::Solid
            $headerRange.Style.Fill.BackgroundColor.SetColor($headerFill)
            $headerRange.Style.Font.Color.SetColor([System.Drawing.Color]::White)
            $headerRange.Style.Font.Bold = $true
            $dataSheet.View.FreezePanes($headerRow + 1, 1)
        }

        $existingSheet = $workbook.Worksheets['About']
        if ($existingSheet) {
            $workbook.Worksheets.Delete($existingSheet)
        }
        $sheet = $workbook.Worksheets.Add('About')

        $sheet.Column(1).Width = 3
        $sheet.Column(2).Width = 22
        $sheet.Column(3).Width = 70
        $sheet.View.ShowGridLines = $false

        # Logo: rows 2 to 6. PNG, since Excel does not insert SVG
        $logoPath = Join-Path -Path $PSScriptRoot -ChildPath 'Assets\PS365-logo.png'
        if (Test-Path -Path $logoPath -PathType Leaf) {
            try {
                $picture = $sheet.Drawings.AddPicture('PS365Logo', [System.IO.FileInfo]::new($logoPath))
                $picture.SetPosition(1, 0, 1, 0)
                $picture.SetSize(80, 80)
            }
            catch {
                Write-Verbose "Unable to insert the PS365 logo in the About worksheet: $($_.Exception.Message)"
            }
        }

        $brandTeal = [System.Drawing.Color]::FromArgb(0, 53, 63)
        $brandOrange = [System.Drawing.Color]::FromArgb(222, 118, 29)
        $mutedGrey = [System.Drawing.Color]::FromArgb(95, 111, 122)
        # Darker orange for text on white (links): the brand orange alone is too light to read
        $linkOrange = [System.Drawing.Color]::FromArgb(179, 90, 16)

        # Orange is the dominant brand color: an orange band behind the title (rows 2 to 4). Column C only:
        # the logo sits in column B and its orange cloud would vanish on an orange background
        $bandRange = $sheet.Cells['C2:C4']
        $bandRange.Style.Fill.PatternType = [OfficeOpenXml.Style.ExcelFillStyle]::Solid
        $bandRange.Style.Fill.BackgroundColor.SetColor($brandOrange)

        $sheet.Cells['C2'].Value = $ReportName
        $sheet.Cells['C2'].Style.Font.Size = 18
        $sheet.Cells['C2'].Style.Font.Bold = $true
        $sheet.Cells['C2'].Style.Font.Color.SetColor($brandTeal)

        $sheet.Cells['C3'].Value = 'Generated by PS365'
        $sheet.Cells['C3'].Style.Font.Size = 12
        $sheet.Cells['C3'].Style.Font.Color.SetColor($brandTeal)

        $sheet.Cells['C4'].Value = 'by Clidsys'
        $sheet.Cells['C4'].Style.Font.Size = 12
        $sheet.Cells['C4'].Style.Font.Bold = $true
        $sheet.Cells['C4'].Style.Font.Color.SetColor($brandTeal)

        $rows = [ordered]@{
            'Report'       = $ReportName
            'Function'     = $callerName
            'Generated on' = (Get-Date -Format 'yyyy-MM-dd HH:mm:ss')
            'PS365 version' = if ($moduleVersion) { "$moduleVersion" } else { 'unknown' }
        }

        $rowIndex = 8
        foreach ($label in $rows.Keys) {
            $sheet.Cells[$rowIndex, 2].Value = $label
            $sheet.Cells[$rowIndex, 2].Style.Font.Bold = $true
            $sheet.Cells[$rowIndex, 2].Style.Font.Color.SetColor($mutedGrey)
            $sheet.Cells[$rowIndex, 3].Value = $rows[$label]
            $rowIndex++
        }

        $rowIndex++
        $links = [ordered]@{
            'PS365 documentation' = $ps365Url
            'Clidsys'             = $clidsysUrl
        }
        foreach ($label in $links.Keys) {
            $sheet.Cells[$rowIndex, 2].Value = $label
            $sheet.Cells[$rowIndex, 2].Style.Font.Bold = $true
            $sheet.Cells[$rowIndex, 2].Style.Font.Color.SetColor($mutedGrey)
            $linkCell = $sheet.Cells[$rowIndex, 3]
            $linkCell.Hyperlink = [System.Uri]::new($links[$label])
            $linkCell.Value = $links[$label]
            $linkCell.Style.Font.UnderLine = $true
            $linkCell.Style.Font.Color.SetColor($linkOrange)
            $rowIndex++
        }

        # File properties: visible in File > Info and in the Windows Explorer details
        $workbook.Properties.Author = 'PS365 by Clidsys'
        $workbook.Properties.Company = 'Clidsys'
        $workbook.Properties.Title = $ReportName
        $workbook.Properties.Comments = "Generated by PS365 ($ps365Url) - by Clidsys ($clidsysUrl)"
        $workbook.Properties.HyperlinkBase = [System.Uri]::new($clidsysUrl)

        Close-ExcelPackage -ExcelPackage $excelPackage
    }
    catch {
        Write-Verbose "Unable to add the About worksheet to $($Path): $($_.Exception.Message)"
        Close-ExcelPackage -ExcelPackage $excelPackage -NoSave
    }
}