Skip to content

Commit fba73f3

Browse files
committed
new: ExcelRange
1 parent bafeb97 commit fba73f3

2 files changed

+163
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#Requires -Modules ImportExcel
2+
<#
3+
.description
4+
- Export a table with dates and numbers
5+
- explicitly setting format strings per-column
6+
- ranges are automatically detected if you use named tables
7+
8+
- It was based on this: <https://github.com/dfinke/ImportExcel/blob/fa907da4a459d9da2d080d60f563333828313ef4/Examples/CustomNumbers/ShortenNumbers.ps1>
9+
#>
10+
11+
12+
$Records = @'
13+
[ { 'name': 'bob', 'when': '2024-07-23 20:23:48Z', 'currency': '1234.3566' },
14+
{ 'name': 'jen', 'when': '2023-04-05 11:23:32Z', 'currency': '93.134545' } ]
15+
'@ | ConvertFrom-Json # -depth 10
16+
17+
18+
$Records | %{
19+
# in general you want to use [datetime]::ParseExact
20+
$_.when = [datetime] $_.when
21+
}
22+
23+
$exportSplat = @{
24+
Title = 'Column formatting example'
25+
TableName = 'Employees_table'
26+
WorksheetName = 'Employees'
27+
AutoSize = $true
28+
PassThru = $true
29+
}
30+
[OfficeOpenXml.ExcelPackage] $excel = $records | Export-Excel @exportSplat
31+
32+
$Sheet = $excel.Employees
33+
Set-ExcelRange -Worksheet $Sheet -Range 'C2:C4' -NumberFormat 'Currency' -Verbose
34+
Set-ExcelRange -Worksheet $Sheet -Range 'B2:B4' -NumberFormat 'Short Date' -Verbose
35+
Close-ExcelPackage $excel -Show
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#Requires -Modules ImportExcel
2+
<#
3+
.description
4+
- Export a table with dates and numbers
5+
- explicitly setting format strings per-column
6+
- ranges are automatically detected if you use named tables
7+
8+
- It was based on this: <https://github.com/dfinke/ImportExcel/blob/fa907da4a459d9da2d080d60f563333828313ef4/Examples/CustomNumbers/ShortenNumbers.ps1>
9+
#>
10+
11+
12+
$Records = @'
13+
[ { 'name': 'bob', 'when': '2024-07-23 20:23:48Z', 'currency': '1234.3566' },
14+
{ 'name': 'jen', 'when': '2023-04-05 11:23:32Z', 'currency': '93.134545' } ]
15+
'@ | ConvertFrom-Json # -depth 10
16+
17+
18+
$Records | %{
19+
# in general you want to use [datetime]::ParseExact
20+
$_.when = [datetime] $_.when
21+
}
22+
23+
$exportSplat = @{
24+
Title = 'Column formatting example'
25+
TableName = 'Employees_table'
26+
WorksheetName = 'Employees'
27+
AutoSize = $true
28+
PassThru = $true
29+
}
30+
[OfficeOpenXml.ExcelPackage] $excel = $records | Export-Excel @exportSplat
31+
32+
$Sheet = $excel.Employees
33+
# original
34+
# Set-ExcelRange -Worksheet $Sheet -Range 'C2:C4' -NumberFormat 'Currency' -Verbose
35+
# Set-ExcelRange -Worksheet $Sheet -Range 'B2:B4' -NumberFormat 'Short Date' -Verbose
36+
37+
$workSheetName = 'Employees'
38+
$tableName = 'Employees_table'
39+
40+
function GetColNames {
41+
param(
42+
[Parameter(Mandatory)]
43+
[ValidateNotNullOrEmpty()]
44+
[OfficeOpenXml.ExcelPackage] $Excel,
45+
46+
[Parameter(Mandatory)]
47+
[ValidateNotNullOrEmpty()]
48+
[ArgumentCompletions('Employees_table')]
49+
[string] $TableName,
50+
51+
[Parameter(Mandatory)]
52+
[ValidateNotNullOrEmpty()]
53+
[ArgumentCompletions('Employees')]
54+
[string] $WorkSheetName
55+
)
56+
57+
58+
return @( $excel.$WorkSheetName.Tables[ $tableName ].Columns.Name )
59+
}
60+
function BuildMappingTable {
61+
<#
62+
.synopsis
63+
Maps column names like 'Currency' to the (column,rows) range, for example: 'C2:C4'
64+
#>
65+
param(
66+
[Parameter(Mandatory)]
67+
[ValidateNotNullOrEmpty()]
68+
[OfficeOpenXml.ExcelPackage] $Excel,
69+
70+
[Parameter(Mandatory)]
71+
[ValidateNotNullOrEmpty()]
72+
[ArgumentCompletions('Employees_table')]
73+
[string] $TableName,
74+
75+
[Parameter(Mandatory)]
76+
[ValidateNotNullOrEmpty()]
77+
[ArgumentCompletions('Employees')]
78+
[string] $WorkSheetName
79+
)
80+
$chars = 'A'..'Z' # future: support more column names
81+
$Excel.$WorkSheetName.Tables[ $TableName ].Columns | %{
82+
$colChar = $chars[ $_.Position ]
83+
84+
$renderAddr = @( # ex: C2:C4
85+
$colChar
86+
$excel.$WorkSheetName.Tables[ $tableName ].Address.Start.Row
87+
':'
88+
$colChar
89+
$excel.$WorkSheetName.Tables[ $tableName ].Address.End.Row
90+
) -join ''
91+
92+
[pscustomobject]@{
93+
ColName = $_.name
94+
Char = $chars[ $_.Position ]
95+
Address = $renderAddr
96+
Id = $_.Id
97+
Position = $_.Position
98+
}
99+
}
100+
}
101+
102+
function ColNameToAddr {
103+
param(
104+
[object]$Map,
105+
[string] $ColName
106+
)
107+
$map.Where({ $_.ColName -eq $ColName }).Address
108+
}
109+
110+
$map = BuildMappingTable -Excel $excel -TableName 'Employees_table' -WorkSheetName Employees
111+
$map | ft -auto
112+
GetColNames -Excel $Excel -TableName 'Employees_table' -WorkSheetName Employees | Join-String -sep ', '
113+
114+
115+
$rangeSplat = @{
116+
Worksheet = $excel.Employees
117+
Range = ColNameToAddr $map 'when'
118+
NumberFormat = 'Short Date'
119+
}
120+
Set-ExcelRange @rangeSplat
121+
$rangeSplat = @{
122+
Worksheet = $excel.Employees
123+
Range = ColNameToAddr $map 'Currency'
124+
NumberFormat = 'Currency'
125+
}
126+
Set-ExcelRange @rangeSplat
127+
128+
Close-ExcelPackage $excel -Show

0 commit comments

Comments
 (0)