Skip to content

Instantly share code, notes, and snippets.

@trackd
Last active September 6, 2025 23:04
Show Gist options
  • Select an option

  • Save trackd/7102fce12be4176beac2bbbf5c68d959 to your computer and use it in GitHub Desktop.

Select an option

Save trackd/7102fce12be4176beac2bbbf5c68d959 to your computer and use it in GitHub Desktop.
Excel toc generation
function New-TableOfContents {
<#
.EXAMPLE
$excelfile = 'C:\temp\test.xlsx'
$toc = New-TableOfContents -ExcelFile $excelfile
$toc | Export-Excel -Path $ExcelFile -WorksheetName 'TOC' -Title "Table of Contents" -MoveToStart
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$ExcelFile
)
Get-ExcelSheetInfo -Path $ExcelFile | ForEach-Object {
[PSCustomObject]@{
Name = $_.Name -creplace '(?<=\w)([A-Z])', ' $1'
Index = $_.Index
Link = "XL://internal/{0}!A1" -f $_.Name
# something like this should also work, breaks atm but works when pasting into xlsx..
# AltLink = [string]::Concat('=HYPERLINK("#', "'", $_.Name, "'", '!A1";', '"', 'Goto ', $_.Name, '"', ')')
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment