-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathExport-Excel.ps1
697 lines (671 loc) · 39.5 KB
/
Export-Excel.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
function Export-Excel {
[CmdletBinding(DefaultParameterSetName = 'Default')]
[OutputType([OfficeOpenXml.ExcelPackage])]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "")]
param(
[Parameter(ParameterSetName = 'Default', Position = 0)]
[String]$Path,
[Parameter(Mandatory = $true, ParameterSetName = "Package")]
[OfficeOpenXml.ExcelPackage]$ExcelPackage,
[Parameter(ValueFromPipeline = $true)]
[Alias('TargetData')]
$InputObject,
[Switch]$Calculate,
[Switch]$Show,
[String]$WorksheetName = 'Sheet1',
[Alias("PW")]
[String]$Password,
[switch]$ClearSheet,
[switch]$Append,
[String]$Title,
[OfficeOpenXml.Style.ExcelFillStyle]$TitleFillPattern = 'Solid',
[Switch]$TitleBold,
[Int]$TitleSize = 22,
$TitleBackgroundColor,
[parameter(DontShow = $true)]
[Switch]$IncludePivotTable,
[String]$PivotTableName,
[String[]]$PivotRows,
[String[]]$PivotColumns,
$PivotData,
[String[]]$PivotFilter,
[Switch]$PivotDataToColumn,
[Hashtable]$PivotTableDefinition,
[Switch]$IncludePivotChart,
[Alias('ChartType')]
[OfficeOpenXml.Drawing.Chart.eChartType]$PivotChartType = 'Pie',
[Switch]$NoLegend,
[Switch]$ShowCategory,
[Switch]$ShowPercent,
[Switch]$AutoSize,
$MaxAutoSizeRows = 1000,
[Switch]$NoClobber,
[Switch]$FreezeTopRow,
[Switch]$FreezeFirstColumn,
[Switch]$FreezeTopRowFirstColumn,
[Int[]]$FreezePane,
[Switch]$AutoFilter,
[Switch]$BoldTopRow,
[Switch]$NoHeader,
[ValidateScript( {
if (-not $_) { throw 'RangeName is null or empty.' }
elseif ($_[0] -notmatch '[a-z]') { throw 'RangeName starts with an invalid character.' }
else { $true }
})]
[String]$RangeName,
[Alias('Table')]
$TableName,
[OfficeOpenXml.Table.TableStyles]$TableStyle = [OfficeOpenXml.Table.TableStyles]::Medium6,
[HashTable]$TableTotalSettings,
[Switch]$BarChart,
[Switch]$PieChart,
[Switch]$LineChart ,
[Switch]$ColumnChart ,
[Object[]]$ExcelChartDefinition,
[String[]]$HideSheet,
[String[]]$UnHideSheet,
[Switch]$MoveToStart,
[Switch]$MoveToEnd,
$MoveBefore ,
$MoveAfter ,
[Switch]$KillExcel,
[Switch]$AutoNameRange,
[Int]$StartRow = 1,
[Int]$StartColumn = 1,
[alias('PT')]
[Switch]$PassThru,
[String]$Numberformat = 'General',
[string[]]$ExcludeProperty,
[Switch]$NoAliasOrScriptPropeties,
[Switch]$DisplayPropertySet,
[String[]]$NoNumberConversion,
[String[]]$NoHyperLinkConversion,
[Object[]]$ConditionalFormat,
[Object[]]$ConditionalText,
[Object[]]$Style,
[ScriptBlock]$CellStyleSB,
#If there is already content in the workbook the sheet with the PivotTable will not be active UNLESS Activate is specified
[switch]$Activate,
[Parameter(ParameterSetName = 'Default')]
[Switch]$Now,
[Switch]$ReturnRange,
#By default PivotTables have Totals for each Row (on the right) and for each column at the bottom. This allows just one or neither to be selected.
[ValidateSet("Both", "Columns", "Rows", "None")]
[String]$PivotTotals = "Both",
#Included for compatibility - equivalent to -PivotTotals "None"
[Switch]$NoTotalsInPivot,
[Switch]$ReZip
)
begin {
$numberRegex = [Regex]'\d'
$isDataTypeValueType = $false
if ($NoClobber) { Write-Warning -Message "-NoClobber parameter is no longer used" }
#Open the file, get the worksheet, and decide where in the sheet we are writing, and if there is a number format to apply.
try {
$script:Header = $null
if ($Append -and $ClearSheet) { throw "You can't use -Append AND -ClearSheet." ; return }
#To force -Now not to format as a table, allow $false in -TableName to be "No table"
$TableName = if ($null -eq $TableName -or ($TableName -is [bool] -and $false -eq $TableName)) { $null } else { [String]$TableName }
if ($Now -or (-not $Path -and -not $ExcelPackage) ) {
if (-not $PSBoundParameters.ContainsKey("Path")) { $Path = [System.IO.Path]::GetTempFileName() -replace '\.tmp', '.xlsx' }
if (-not $PSBoundParameters.ContainsKey("Show")) { $Show = $true }
if (-not $PSBoundParameters.ContainsKey("AutoSize")) { $AutoSize = $true }
#"Now" option will create a table, unless something passed in TableName/Table Style. False in TableName will block autocreation
if (-not $PSBoundParameters.ContainsKey("TableName") -and
-not $PSBoundParameters.ContainsKey("TableStyle") -and
-not $AutoFilter) {
$TableName = '' # later rely on distinction between NULL and ""
}
}
if ($ExcelPackage) {
$pkg = $ExcelPackage
$Path = $pkg.File
}
Else { $pkg = Open-ExcelPackage -Path $Path -Create -KillExcel:$KillExcel -Password:$Password }
}
catch { throw "Could not open Excel Package $path" }
try {
$params = @{WorksheetName = $WorksheetName }
foreach ($p in @("ClearSheet", "MoveToStart", "MoveToEnd", "MoveBefore", "MoveAfter", "Activate")) { if ($PSBoundParameters[$p]) { $params[$p] = $PSBoundParameters[$p] } }
$ws = $pkg | Add-Worksheet @params
if ($ws.Name -ne $WorksheetName) {
Write-Warning -Message "The Worksheet name has been changed from $WorksheetName to $($ws.Name), this may cause errors later."
$WorksheetName = $ws.Name
}
}
catch { throw "Could not get worksheet $WorksheetName" }
try {
if ($Append -and $ws.Dimension) {
#if there is a title or anything else above the header row, append needs to be combined wih a suitable startrow parameter
$headerRange = $ws.Dimension.Address -replace "\d+$", $StartRow
#using a slightly odd syntax otherwise header ends up as a 2D array
$ws.Cells[$headerRange].Value | ForEach-Object -Begin { $Script:header = @() } -Process { $Script:header += $_ }
$NoHeader = $true
#if we did not get AutoNameRange, but headers have ranges of the same name make autoNameRange True, otherwise make it false
if (-not $AutoNameRange) {
$AutoNameRange = $true ; foreach ($h in $header) { if ($ws.names.name -notcontains $h) { $AutoNameRange = $false } }
}
#if we did not get a Rangename but there is a Range which covers the active part of the sheet, set Rangename to that.
if (-not $RangeName -and $ws.names.where({ $_.name[0] -match '[a-z]' })) {
$theRange = $ws.names.where({
($_.Name[0] -match '[a-z]' ) -and
($_.Start.Row -eq $StartRow) -and
($_.Start.Column -eq $StartColumn) -and
($_.End.Row -eq $ws.Dimension.End.Row) -and
($_.End.Column -eq $ws.Dimension.End.column) } , 'First', 1)
if ($theRange) { $rangename = $theRange.name }
}
#if we did not get a table name but there is a table which covers the active part of the sheet, set table name to that, and don't do anything with autofilter
$existingTable = $ws.Tables.Where({ $_.address.address -eq $ws.dimension.address }, 'First', 1)
if ($null -eq $TableName -and $existingTable) {
$TableName = $existingTable.Name
$TableStyle = $existingTable.StyleName -replace "^TableStyle", ""
$AutoFilter = $false
}
#if we did not get $autofilter but a filter range is set and it covers the right area, set autofilter to true
elseif (-not $AutoFilter -and $ws.Names['_xlnm._FilterDatabase']) {
if ( ($ws.Names['_xlnm._FilterDatabase'].Start.Row -eq $StartRow) -and
($ws.Names['_xlnm._FilterDatabase'].Start.Column -eq $StartColumn) -and
($ws.Names['_xlnm._FilterDatabase'].End.Row -eq $ws.Dimension.End.Row) -and
($ws.Names['_xlnm._FilterDatabase'].End.Column -eq $ws.Dimension.End.Column) ) { $AutoFilter = $true }
}
$row = $ws.Dimension.End.Row
Write-Debug -Message ("Appending: headers are " + ($script:Header -join ", ") + " Start row is $row")
if ($Title) { Write-Warning -Message "-Title Parameter is ignored when appending." }
}
elseif ($Title) {
#Can only add a title if not appending!
$row = $StartRow
$ws.Cells[$row, $StartColumn].Value = $Title
$ws.Cells[$row, $StartColumn].Style.Font.Size = $TitleSize
if ($PSBoundParameters.ContainsKey("TitleBold")) {
#Set title to Bold face font if -TitleBold was specified.
#Otherwise the default will be unbolded.
$ws.Cells[$row, $StartColumn].Style.Font.Bold = [boolean]$TitleBold
}
if ($TitleBackgroundColor ) {
if ($TitleBackgroundColor -is [string]) { $TitleBackgroundColor = [System.Drawing.Color]::$TitleBackgroundColor }
$ws.Cells[$row, $StartColumn].Style.Fill.PatternType = $TitleFillPattern
$ws.Cells[$row, $StartColumn].Style.Fill.BackgroundColor.SetColor($TitleBackgroundColor)
}
$row ++ ; $startRow ++
}
else { $row = $StartRow }
$ColumnIndex = $StartColumn
$Numberformat = Expand-NumberFormat -NumberFormat $Numberformat
if ((-not $ws.Dimension) -and ($Numberformat -ne $ws.Cells.Style.Numberformat.Format)) {
$ws.Cells.Style.Numberformat.Format = $Numberformat
$setNumformat = $false
}
else { $setNumformat = ($Numberformat -ne $ws.Cells.Style.Numberformat.Format) }
}
catch { throw "Failed preparing to export to worksheet '$WorksheetName' to '$Path': $_" }
#region Special case -inputobject passed a dataTable object
<# If inputObject was passed via the pipeline it won't be visible until the process block, we will only see it here if it was passed as a parameter
if it is a data table don't do foreach on it (slow) - put the whole table in and set dates on date columns,
set things up for the end block, and skip the process block #>
if ($InputObject -is [System.Data.DataTable]) {
if ($Append -and $ws.dimension) {
$row ++
$null = $ws.Cells[$row, $StartColumn].LoadFromDataTable($InputObject, $false )
if ($TableName -or $PSBoundParameters.ContainsKey('TableStyle')) {
Add-ExcelTable -Range $ws.Cells[$ws.Dimension] -TableName $TableName -TableStyle $TableStyle -TableTotalSettings $TableTotalSettings
}
}
else {
#Change TableName if $TableName is non-empty; don't leave caller with a renamed table!
$orginalTableName = $InputObject.TableName
if ($PSBoundParameters.ContainsKey("TableName")) {
$InputObject.TableName = $TableName
}
while ($InputObject.TableName -in $pkg.Workbook.Worksheets.Tables.name) {
Write-Warning "Table name $($InputObject.TableName) is not unique, adding '_' to it "
$InputObject.TableName += "_"
}
#Insert as a table, if Tablestyle didn't arrive as a default, or $TableName non-null - even if empty
if ($null -ne $TableName -or $PSBoundParameters.ContainsKey("TableStyle")) {
$null = $ws.Cells[$row, $StartColumn].LoadFromDataTable($InputObject, (-not $noHeader), $TableStyle )
# Workaround for EPPlus not marking the empty row on an empty table as dummy row.
if ($InputObject.Rows.Count -eq 0) {
($ws.Tables | Select-Object -Last 1).TableXml.table.SetAttribute('insertRow', 1)
}
}
else {
$null = $ws.Cells[$row, $StartColumn].LoadFromDataTable($InputObject, (-not $noHeader) )
}
$InputObject.TableName = $orginalTableName
}
foreach ($c in $InputObject.Columns.where({ $_.datatype -eq [datetime] })) {
Set-ExcelColumn -Worksheet $ws -Column ($c.Ordinal + $StartColumn) -NumberFormat 'Date-Time'
}
foreach ($c in $InputObject.Columns.where({ $_.datatype -eq [timespan] })) {
Set-ExcelColumn -Worksheet $ws -Column ($c.Ordinal + $StartColumn) -NumberFormat '[h]:mm:ss'
}
$ColumnIndex += $InputObject.Columns.Count - 1
if ($noHeader) { $row += $InputObject.Rows.Count - 1 }
else { $row += $InputObject.Rows.Count }
$null = $PSBoundParameters.Remove('InputObject')
$firstTimeThru = $false
}
#endregion
else { $firstTimeThru = $true }
}
process {
if ($PSBoundParameters.ContainsKey("InputObject")) {
try {
if ($null -eq $InputObject) { $row += 1 }
foreach ($TargetData in $InputObject) {
if ($firstTimeThru) {
$firstTimeThru = $false
$isDataTypeValueType = ($null -eq $TargetData) -or ($TargetData.GetType().name -match 'string|timespan|datetime|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort|URI|ExcelHyperLink')
if ($isDataTypeValueType ) {
$script:Header = @(".") # dummy value to make sure we go through the "for each name in $header"
if (-not $Append) { $row -= 1 } # By default row will be 1, it is incremented before inserting values (so it ends pointing at final row.); si first data row is 2 - move back up 1 if there is no header .
}
if ($null -ne $TargetData) { Write-Debug "DataTypeName is '$($TargetData.GetType().name)' isDataTypeValueType '$isDataTypeValueType'" }
}
#region Add headers - if we are appending, or we have been through here once already we will have the headers
if (-not $script:Header) {
if ($DisplayPropertySet -and $TargetData.psStandardmembers.DefaultDisplayPropertySet.ReferencedPropertyNames) {
$script:Header = $TargetData.psStandardmembers.DefaultDisplayPropertySet.ReferencedPropertyNames.Where( { $_ -notin $ExcludeProperty })
}
else {
if ($NoAliasOrScriptPropeties) { $propType = "Property" } else { $propType = "*" }
$script:Header = $TargetData.PSObject.Properties.where( { $_.MemberType -like $propType }).Name
}
foreach ($exclusion in $ExcludeProperty) { $script:Header = $script:Header -notlike $exclusion }
if ($NoHeader) {
# Don't push the headers to the spreadsheet
$row -= 1
}
else {
$ColumnIndex = $StartColumn
foreach ($Name in $script:Header) {
$ws.Cells[$row, $ColumnIndex].Value = $Name
Write-Verbose "Cell '$row`:$ColumnIndex' add header '$Name'"
$ColumnIndex += 1
}
}
}
#endregion
#region Add non header values
$row += 1
$ColumnIndex = $StartColumn
<#
For each item in the header OR for the Data item if this is a simple Type or data table :
If it is a date insert with one of Excel's built in formats - recognized as "Date and time to be localized"
if it is a timespan insert with a built in format for elapsed hours, minutes and seconds
if its any other numeric insert as is , setting format if need be.
Preserve URI, Insert a data table, convert non string objects to string.
For strings, check for fomula, URI or Number, before inserting as a string (ignore nulls) #>
foreach ($Name in $script:Header) {
if ($isDataTypeValueType) { $v = $TargetData }
else { $v = $TargetData.$Name }
try {
if ($v -is [DateTime]) {
$ws.Cells[$row, $ColumnIndex].Value = $v
$ws.Cells[$row, $ColumnIndex].Style.Numberformat.Format = 'm/d/yy h:mm' # This is not a custom format, but a preset recognized as date and localized.
}
elseif ($v -is [TimeSpan]) {
$ws.Cells[$row, $ColumnIndex].Value = $v
$ws.Cells[$row, $ColumnIndex].Style.Numberformat.Format = '[h]:mm:ss'
}
elseif ($v -is [System.ValueType]) {
$ws.Cells[$row, $ColumnIndex].Value = $v
if ($setNumformat) { $ws.Cells[$row, $ColumnIndex].Style.Numberformat.Format = $Numberformat }
}
elseif ($v -is [uri] -and
$NoHyperLinkConversion -ne '*' -and
$NoHyperLinkConversion -notcontains $Name ) {
$ws.Cells[$row, $ColumnIndex].HyperLink = $v
$ws.Cells[$row, $ColumnIndex].Style.Font.Color.SetColor([System.Drawing.Color]::Blue)
$ws.Cells[$row, $ColumnIndex].Style.Font.UnderLine = $true
}
elseif ($v -isnot [String] ) {
#Other objects or null.
if ($null -ne $v) { $ws.Cells[$row, $ColumnIndex].Value = $v.toString() }
}
elseif ($v[0] -eq '=') {
$ws.Cells[$row, $ColumnIndex].Formula = ($v -replace '^=', '')
if ($setNumformat) { $ws.Cells[$row, $ColumnIndex].Style.Numberformat.Format = $Numberformat }
}
elseif ( $NoHyperLinkConversion -ne '*' -and # Put the check for 'NoHyperLinkConversion is null' first to skip checking for wellformedstring
$NoHyperLinkConversion -notcontains $Name -and
[System.Uri]::IsWellFormedUriString($v , [System.UriKind]::Absolute)
) {
if ($v -match "^xl://internal/") {
$referenceAddress = $v -replace "^xl://internal/" , ""
$display = $referenceAddress -replace "!A1$" , ""
$h = New-Object -TypeName OfficeOpenXml.ExcelHyperLink -ArgumentList $referenceAddress , $display
$ws.Cells[$row, $ColumnIndex].HyperLink = $h
}
else { $ws.Cells[$row, $ColumnIndex].HyperLink = $v } #$ws.Cells[$row, $ColumnIndex].Value = $v.AbsoluteUri
$ws.Cells[$row, $ColumnIndex].Style.Font.Color.SetColor([System.Drawing.Color]::Blue)
$ws.Cells[$row, $ColumnIndex].Style.Font.UnderLine = $true
}
else {
$number = $null
if ( $NoNumberConversion -ne '*' -and # Check if NoNumberConversion isn't specified. Put this first as it's going to stop the if clause. Quicker than putting regex check first
$numberRegex.IsMatch($v) -and # and if it contains digit(s) - this syntax is quicker than -match for many items and cuts out slow checks for non numbers
$NoNumberConversion -notcontains $Name -and
[Double]::TryParse($v, [System.Globalization.NumberStyles]::Any, [System.Globalization.NumberFormatInfo]::CurrentInfo, [Ref]$number)
) {
$ws.Cells[$row, $ColumnIndex].Value = $number
if ($setNumformat) { $ws.Cells[$row, $ColumnIndex].Style.Numberformat.Format = $Numberformat }
}
else {
$ws.Cells[$row, $ColumnIndex].Value = $v
}
}
}
catch { Write-Warning -Message "Could not insert the '$Name' property at Row $row, Column $ColumnIndex" }
$ColumnIndex += 1
}
$ColumnIndex -= 1 # column index will be the last column whether isDataTypeValueType was true or false
#endregion
}
}
catch { throw "Failed exporting data to worksheet '$WorksheetName' to '$Path': $_" }
}
}
end {
if ($firstTimeThru -and $ws.Dimension) {
$LastRow = $ws.Dimension.End.Row
$LastCol = $ws.Dimension.End.Column
$endAddress = $ws.Dimension.End.Address
}
else {
$LastRow = $row
$LastCol = $ColumnIndex
$endAddress = [OfficeOpenXml.ExcelAddress]::GetAddress($LastRow , $LastCol)
}
$startAddress = [OfficeOpenXml.ExcelAddress]::GetAddress($StartRow, $StartColumn)
$dataRange = "{0}:{1}" -f $startAddress, $endAddress
Write-Debug "Data Range '$dataRange'"
if ($AutoNameRange) {
try {
if (-not $script:header) {
# if there aren't any headers, use the the first row of data to name the ranges: this is the last point that headers will be used.
$headerRange = $ws.Dimension.Address -replace "\d+$", $StartRow
#using a slightly odd syntax otherwise header ends up as a 2D array
$ws.Cells[$headerRange].Value | ForEach-Object -Begin { $Script:header = @() } -Process { $Script:header += $_ }
if ($PSBoundParameters.ContainsKey($TargetData)) {
#if Export was called with data that writes no header start the range at $startRow ($startRow is data)
$targetRow = $StartRow
}
else { $targetRow = $StartRow + 1 } #if Export was called without data to add names (assume $startRow is a header) or...
} # ... called with data that writes a header, then start the range at $startRow + 1
else { $targetRow = $StartRow + 1 }
#Dimension.start.row always seems to be one so we work out the target row
#, but start.column is the first populated one and .Columns is the count of populated ones.
# if we have 5 columns from 3 to 8, headers are numbered 0..4, so that is in the for loop and used for getting the name...
# but we have to add the start column on when referencing positions
foreach ($c in 0..($LastCol - $StartColumn)) {
$targetRangeName = @($script:Header)[$c] #Let Add-ExcelName fix (and warn about) bad names
Add-ExcelName -RangeName $targetRangeName -Range $ws.Cells[$targetRow, ($StartColumn + $c ), $LastRow, ($StartColumn + $c )]
try {
#this test can throw with some names, surpress any error
if ([OfficeOpenXml.FormulaParsing.ExcelUtilities.ExcelAddressUtil]::IsValidAddress(($targetRangeName -replace '\W' , '_' ))) {
Write-Warning -Message "AutoNameRange: Property name '$targetRangeName' is also a valid Excel address and may cause issues. Consider renaming the property."
}
}
catch {
Write-Warning -Message "AutoNameRange: Testing '$targetRangeName' caused an error. This should be harmless, but a change of property name may be needed.."
}
}
}
catch { Write-Warning -Message "Failed adding named ranges to worksheet '$WorksheetName': $_" }
}
#Empty string is not allowed as a name for ranges or tables.
if ($RangeName) { Add-ExcelName -Range $ws.Cells[$dataRange] -RangeName $RangeName }
#Allow table to be inserted by specifying Name, or Style or both; only process autoFilter if there is no table (they clash).
if ($null -ne $TableName -or $PSBoundParameters.ContainsKey('TableStyle')) {
#Already inserted Excel table if input was a DataTable
if ($InputObject -isnot [System.Data.DataTable]) {
Add-ExcelTable -Range $ws.Cells[$dataRange] -TableName $TableName -TableStyle $TableStyle -TableTotalSettings $TableTotalSettings
}
}
elseif ($AutoFilter) {
try {
$ws.Cells[$dataRange].AutoFilter = $true
Write-Verbose -Message "Enabled autofilter. "
}
catch { Write-Warning -Message "Failed adding autofilter to worksheet '$WorksheetName': $_" }
}
if ($PivotTableDefinition) {
foreach ($item in $PivotTableDefinition.GetEnumerator()) {
$params = $item.value
if ($Activate) { $params.Activate = $true }
if ($params.keys -notcontains 'SourceRange' -and
($params.Keys -notcontains 'SourceWorksheet' -or $params.SourceWorksheet -eq $WorksheetName)) { $params.SourceRange = $dataRange }
if ($params.Keys -notcontains 'SourceWorksheet') { $params.SourceWorksheet = $ws }
if ($params.Keys -notcontains 'NoTotalsInPivot' -and $NoTotalsInPivot ) { $params.PivotTotals = 'None' }
if ($params.Keys -notcontains 'PivotTotals' -and $PivotTotals ) { $params.PivotTotals = $PivotTotals }
if ($params.Keys -notcontains 'PivotDataToColumn' -and $PivotDataToColumn) { $params.PivotDataToColumn = $true }
Add-PivotTable -ExcelPackage $pkg -PivotTableName $item.key @Params
}
}
if ($IncludePivotTable -or $IncludePivotChart -or $PivotData) {
$params = @{
'SourceRange' = $dataRange
}
if ($PivotTableName -and ($pkg.workbook.worksheets.tables.name -contains $PivotTableName)) {
Write-Warning -Message "The selected PivotTable name '$PivotTableName' is already used as a table name. Adding a suffix of 'Pivot'."
$PivotTableName += 'Pivot'
}
if ($PivotTableName) { $params.PivotTableName = $PivotTableName }
else { $params.PivotTableName = $WorksheetName + 'PivotTable' }
if ($Activate) { $params.Activate = $true }
if ($PivotFilter) { $params.PivotFilter = $PivotFilter }
if ($PivotRows) { $params.PivotRows = $PivotRows }
if ($PivotColumns) { $Params.PivotColumns = $PivotColumns }
if ($PivotData) { $Params.PivotData = $PivotData }
if ($NoTotalsInPivot) { $params.PivotTotals = "None" }
Elseif ($PivotTotals) { $params.PivotTotals = $PivotTotals }
if ($PivotDataToColumn) { $params.PivotDataToColumn = $true }
if ($IncludePivotChart -or
$PSBoundParameters.ContainsKey('PivotChartType')) {
$params.IncludePivotChart = $true
$Params.ChartType = $PivotChartType
if ($ShowCategory) { $params.ShowCategory = $true }
if ($ShowPercent) { $params.ShowPercent = $true }
if ($NoLegend) { $params.NoLegend = $true }
}
Add-PivotTable -ExcelPackage $pkg -SourceWorksheet $ws @params
}
try {
#Allow single switch or two seperate ones.
if ($FreezeTopRowFirstColumn -or ($FreezeTopRow -and $FreezeFirstColumn)) {
if ($Title) {
$ws.View.FreezePanes(3, 2)
Write-Verbose -Message "Froze title and header rows and first column"
}
else {
$ws.View.FreezePanes(2, 2)
Write-Verbose -Message "Froze top row and first column"
}
}
elseif ($FreezeTopRow) {
if ($Title) {
$ws.View.FreezePanes(3, 1)
Write-Verbose -Message "Froze title and header rows"
}
else {
$ws.View.FreezePanes(2, 1)
Write-Verbose -Message "Froze top row"
}
}
elseif ($FreezeFirstColumn) {
$ws.View.FreezePanes(1, 2)
Write-Verbose -Message "Froze first column"
}
#Must be 1..maxrows or and array of 1..maxRows,1..MaxCols
if ($FreezePane) {
$freezeRow, $freezeColumn = $FreezePane
if (-not $freezeColumn -or $freezeColumn -eq 0) {
$freezeColumn = 1
}
if ($freezeRow -ge 1) {
$ws.View.FreezePanes($freezeRow, $freezeColumn)
Write-Verbose -Message "Froze panes at row $freezeRow and column $FreezeColumn"
}
}
}
catch { Write-Warning -Message "Failed adding Freezing the panes in worksheet '$WorksheetName': $_" }
if ($PSBoundParameters.ContainsKey("BoldTopRow")) {
#it sets bold as far as there are populated cells: for whole row could do $ws.row($x).style.font.bold = $true
try {
if ($Title) {
$range = $ws.Dimension.Address -replace '\d+', ($StartRow + 1)
}
else {
$range = $ws.Dimension.Address -replace '\d+', $StartRow
}
$ws.Cells[$range].Style.Font.Bold = [boolean]$BoldTopRow
Write-Verbose -Message "Set $range font style to bold."
}
catch { Write-Warning -Message "Failed setting the top row to bold in worksheet '$WorksheetName': $_" }
}
if ($AutoSize -and -not $env:NoAutoSize) {
try {
#Don't fit the all the columns in the sheet; if we are adding cells beside things with hidden columns, that unhides them
if ($MaxAutoSizeRows -and $MaxAutoSizeRows -lt $LastRow ) {
$AutosizeRange = [OfficeOpenXml.ExcelAddress]::GetAddress($startRow, $StartColumn, $MaxAutoSizeRows , $LastCol)
$ws.Cells[$AutosizeRange].AutoFitColumns()
}
else { $ws.Cells[$dataRange].AutoFitColumns() }
Write-Verbose -Message "Auto-sized columns"
}
catch { Write-Warning -Message "Failed autosizing columns of worksheet '$WorksheetName': $_" }
}
elseif ($AutoSize) { Write-Warning -Message "Auto-fitting columns is not available with this OS configuration." }
foreach ($Sheet in $HideSheet) {
try {
$pkg.Workbook.Worksheets.Where({ $_.Name -like $sheet }) | ForEach-Object {
$_.Hidden = 'Hidden'
Write-verbose -Message "Sheet '$($_.Name)' Hidden."
}
}
catch { Write-Warning -Message "Failed hiding worksheet '$sheet': $_" }
}
foreach ($Sheet in $UnHideSheet) {
try {
$pkg.Workbook.Worksheets.Where({ $_.Name -like $sheet }) | ForEach-Object {
$_.Hidden = 'Visible'
Write-verbose -Message "Sheet '$($_.Name)' shown"
}
}
catch { Write-Warning -Message "Failed showing worksheet '$sheet': $_" }
}
if (-not $pkg.Workbook.Worksheets.Where({ $_.Hidden -eq 'visible' })) {
Write-Verbose -Message "No Sheets were left visible, making $WorksheetName visible"
$ws.Hidden = 'Visible'
}
foreach ($chartDef in $ExcelChartDefinition) {
if ($chartDef -is [System.Management.Automation.PSCustomObject]) {
$params = @{}
$chartDef.PSObject.Properties | ForEach-Object { if ( $null -ne $_.value) { $params[$_.name] = $_.value } }
Add-ExcelChart -Worksheet $ws @params
}
elseif ($chartDef -is [hashtable] -or $chartDef -is [System.Collections.Specialized.OrderedDictionary]) {
Add-ExcelChart -Worksheet $ws @chartDef
}
}
if ($Calculate) {
try { [OfficeOpenXml.CalculationExtension]::Calculate($ws) }
catch { Write-Warning "One or more errors occured while calculating, save will continue, but there may be errors in the workbook. $_" }
}
if ($Barchart -or $PieChart -or $LineChart -or $ColumnChart) {
if ($NoHeader) { $FirstDataRow = $startRow }
else { $FirstDataRow = $startRow + 1 }
$range = [OfficeOpenXml.ExcelAddress]::GetAddress($FirstDataRow, $startColumn, $FirstDataRow, $lastCol )
$xCol = $ws.cells[$range] | Where-Object { $_.value -is [string] } | ForEach-Object { $_.start.column } | Sort-Object | Select-Object -first 1
if (-not $xcol) {
$xcol = $StartColumn
$range = [OfficeOpenXml.ExcelAddress]::GetAddress($FirstDataRow, ($startColumn + 1), $FirstDataRow, $lastCol )
}
$yCol = $ws.cells[$range] | Where-Object { $_.value -is [valueType] -or $_.Formula } | ForEach-Object { $_.start.column } | Sort-Object | Select-Object -first 1
if (-not ($xCol -and $ycol)) { Write-Warning -Message "Can't identify a string column and a number column to use as chart labels and data. " }
else {
$params = @{
XRange = [OfficeOpenXml.ExcelAddress]::GetAddress($FirstDataRow, $xcol , $lastrow, $xcol)
YRange = [OfficeOpenXml.ExcelAddress]::GetAddress($FirstDataRow, $ycol , $lastrow, $ycol)
Title = ''
Column = ($lastCol + 1)
Width = 800
}
if ($ShowPercent) { $params["ShowPercent"] = $true }
if ($ShowCategory) { $params["ShowCategory"] = $true }
if ($NoLegend) { $params["NoLegend"] = $true }
if (-not $NoHeader) { $params["SeriesHeader"] = $ws.Cells[$startRow, $YCol].Value }
if ($ColumnChart) { $Params["chartType"] = "ColumnStacked" }
elseif ($Barchart) { $Params["chartType"] = "BarStacked" }
elseif ($PieChart) { $Params["chartType"] = "PieExploded3D" }
elseif ($LineChart) { $Params["chartType"] = "Line" }
Add-ExcelChart -Worksheet $ws @params
}
}
# It now doesn't matter if the conditional formating rules are passed in $conditionalText or $conditional format.
# Just one with an alias for compatiblity it will break things for people who are using both at once
foreach ($c in (@() + $ConditionalText + $ConditionalFormat) ) {
try {
#we can take an object with a .ConditionalType property made by New-ConditionalText or with a .Formatter Property made by New-ConditionalFormattingIconSet or a hash table
if ($c.ConditionalType) {
$cfParams = @{RuleType = $c.ConditionalType; ConditionValue = $c.Text ;
BackgroundColor = $c.BackgroundColor; BackgroundPattern = $c.PatternType ;
ForeGroundColor = $c.ConditionalTextColor
}
if ($c.Range) { $cfParams.Range = $c.Range }
else { $cfParams.Range = $ws.Dimension.Address }
Add-ConditionalFormatting -Worksheet $ws @cfParams
Write-Verbose -Message "Added conditional formatting to range $($c.range)"
}
elseif ($c.formatter) {
switch ($c.formatter) {
"ThreeIconSet" { Add-ConditionalFormatting -Worksheet $ws -ThreeIconsSet $c.IconType -range $c.range -reverse:$c.reverse -ShowIconOnly:$c.ShowIconOnly}
"FourIconSet" { Add-ConditionalFormatting -Worksheet $ws -FourIconsSet $c.IconType -range $c.range -reverse:$c.reverse -ShowIconOnly:$c.ShowIconOnly}
"FiveIconSet" { Add-ConditionalFormatting -Worksheet $ws -FiveIconsSet $c.IconType -range $c.range -reverse:$c.reverse -ShowIconOnly:$c.ShowIconOnly}
}
Write-Verbose -Message "Added conditional formatting to range $($c.range)"
}
elseif ($c -is [hashtable] -or $c -is [System.Collections.Specialized.OrderedDictionary]) {
if (-not $c.Range -or $c.Address) { $c.Address = $ws.Dimension.Address }
Add-ConditionalFormatting -Worksheet $ws @c
}
}
catch { throw "Error applying conditional formatting to worksheet $_" }
}
foreach ($s in $Style) {
if (-not $s.Range) { $s["Range"] = $ws.Dimension.Address }
Set-ExcelRange -Worksheet $ws @s
}
if ($CellStyleSB) {
try {
$TotalRows = $ws.Dimension.Rows
$LastColumn = $ws.Dimension.Address -replace "^.*:(\w*)\d+$" , '$1'
& $CellStyleSB $ws $TotalRows $LastColumn
}
catch { Write-Warning -Message "Failed processing CellStyleSB in worksheet '$WorksheetName': $_" }
}
#Can only add password, may want to support -password $Null removing password.
if ($Password) {
try {
$ws.Protection.SetPassword($Password)
Write-Verbose -Message 'Set password on workbook'
}
catch { throw "Failed setting password for worksheet '$WorksheetName': $_" }
}
if ($PassThru) { $pkg }
else {
if ($ReturnRange) { $dataRange }
if ($Password) { $pkg.Save($Password) }
else { $pkg.Save() }
Write-Verbose -Message "Saved workbook $($pkg.File)"
if ($ReZip) {
Invoke-ExcelReZipFile -ExcelPackage $pkg
}
$pkg.Dispose()
if ($Show) { Invoke-Item $Path }
}
}
}