Merge remote-tracking branch 'upstream/master'

This commit is contained in:
jhoneill
2018-07-02 09:18:44 +01:00
29 changed files with 536 additions and 376 deletions

1
.gitignore vendored
View File

@@ -55,4 +55,3 @@ test.xlsx
testCCFMT.ps1 testCCFMT.ps1
testHide.ps1 testHide.ps1
ImportExcel.zip ImportExcel.zip
*.xlsx

26
.vscode/spellright.dict vendored Normal file
View File

@@ -0,0 +1,26 @@
databar
appveyor
SqlDataToExcel
xlsm
jameseholt
params
robinmalik
scriptblock
headsphere
xelsirko
importexcel
Mihalicz
idx
Möller
redoz
dir
pivotables
WorkSheetname
Lachance-Guillemette
ints
pscookiemonster
ps
pwd
Nuget
EPPLus
intellisense

7
DoTests.ps1 Normal file
View File

@@ -0,0 +1,7 @@
$PSVersionTable.PSVersion
if ((Get-Module -ListAvailable pester) -eq $null) {
Install-Module -Name Pester -Repository PSGallery -Force
}
Invoke-Pester -Script $PSScriptRoot\__tests__

BIN
Examples/Charts/Tools.xlsx Normal file

Binary file not shown.

View File

@@ -0,0 +1,21 @@
Remove-Item -Path .\test.xlsx -ErrorAction Ignore
$excel = Get-Process |
Select-Object -Property Name,Company,Handles,CPU,PM,NPM,WS |
Export-Excel -Path .\test.xlsx -ClearSheet -WorkSheetname "Processes" -PassThru
$sheet = $excel.Workbook.Worksheets["Processes"]
$sheet.Column(1) | Set-Format -Bold -AutoFit
$sheet.Column(2) | Set-Format -Width 29 -WrapText
$sheet.Column(3) | Set-Format -HorizontalAlignment Right -NFormat "#,###"
Set-Format -Address $sheet.Cells["E1:H1048576"] -HorizontalAlignment Right -NFormat "#,###"
Set-Format -Address $sheet.Column(4) -HorizontalAlignment Right -NFormat "#,##0.0" -Bold
Set-Format -Address $sheet.Row(1) -Bold -HorizontalAlignment Center
Add-ConditionalFormatting -WorkSheet $sheet -Range "D2:D1048576" -DataBarColor Red
Add-ConditionalFormatting -WorkSheet $sheet -Range "G2:G1048576" -RuleType GreaterThan -ConditionValue "104857600" -ForeGroundColor Red
foreach ($c in 5..9) {Set-Format -Address $sheet.Column($c) -AutoFit }
Export-Excel -ExcelPackage $excel -WorkSheetname "Processes" -IncludePivotChart -ChartType ColumnClustered -NoLegend -PivotRows company -PivotData @{'Name'='Count'} -Show

Binary file not shown.

View File

@@ -0,0 +1,7 @@
Import-Module ..\..\ImportExcel.psd1 -Force
ConvertFrom-ExcelToSQLInsert -TableName "Movies" -Path ".\Movies.xlsx" -ConvertEmptyStringsToNull
''
'# UseMSSQLSyntax'
ConvertFrom-ExcelToSQLInsert -UseMSSQLSyntax -TableName "Movies" -Path ".\Movies.xlsx" -ConvertEmptyStringsToNull

Binary file not shown.

BIN
Examples/Fibonacci/fib.xlsx Normal file

Binary file not shown.

View File

@@ -0,0 +1,38 @@
if(!(gcm ig -ErrorAction SilentlyContinue)) {
"Use ``Install-Module NameIT`` to get the needed module from the gallery to support running this script"
return
}
$sign=@{sign=echo + -}
$location=@{location=echo Atlanta Newark Washington Chicago Philadelphia Houston Phoneix}
$(1..6 | % {
$from=$to=""
while($from -eq $to) {
$from=ig "[location]" -CustomData $location
$to=ig "[location]" -CustomData $location
}
[double]$a=ig "########"
[double]$b=ig ".####"
[double]$c=ig "#######"
[double]$d=ig "[sign].##" -CustomData $sign
[double]$e=ig "###"
[double]$f=ig "[sign]##" -CustomData $sign
#"{0},{1},{2},{3},{4},{5},{6},{7}" -f $from, $to, $a, $b, $c, $d, $e, $f
[PSCustomObject][Ordered]@{
From=$from
To=$to
RDollars=$a
RPercent=$b
MDollars=$c
MPercent=$d
Revenue=$e
Margin=$f
}
} | ConvertTo-Csv -NoTypeInformation) -replace '"','' # | Export-Excel

View File

@@ -1,9 +1,9 @@
ColumnChart -Title "Central Limit Theorem" ($( ColumnChart -Title "Central Limit Theorem" -NoLegend ($(
for ($i = 1; $i -le 500; $i++) { for ($i = 1; $i -le 500; $i++) {
$s=0 $s = 0
for ($j = 1; $j -le 100; $j++){ for ($j = 1; $j -le 100; $j++) {
$s+=Get-Random -Minimum 0 -Maximum 2 $s += Get-Random -Minimum 0 -Maximum 2
}
$s
} }
$s ) | Sort-Object | Group-Object | Select-Object Count, Name)
}
) | Sort | Group | select Count, Name)

Binary file not shown.

View File

@@ -0,0 +1,15 @@
Import-Module ..\..\ImportExcel.psd1 -Force
Remove-Item .\test1.xlsx -ErrorAction Ignore
$ExcelParams = @{
Path = ".\test1.xlsx"
IncludePivotTable = $true
PivotRows = 'Company'
PivotTableName = 'MyTable'
PivotData = @{'Handles' = 'sum'}
Show = $true
}
Get-Process | Select-Object Company, Handles |
Export-Excel @ExcelParams

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,12 @@
Remove-Item .\testFormula.xlsx -ErrorAction Ignore
@"
id,item,units,cost
12001,Nails,37,3.99
12002,Hammer,5,12.10
12003,Saw,12,15.37
12010,Drill,20,8
12011,Crowbar,7,23.48
"@ | ConvertFrom-Csv |
Add-Member -PassThru -MemberType NoteProperty -Name Total -Value "=units*cost" |
Export-Excel -Path .\testFormula.xlsx -Show -AutoSize -AutoNameRange

View File

@@ -21,7 +21,7 @@
.PARAMETER NoAliasOrScriptPropeties .PARAMETER NoAliasOrScriptPropeties
Some objects duplicate properties with aliases, or have Script properties which take a long time to return a value and slow the export down, if specified this removes these properties Some objects duplicate properties with aliases, or have Script properties which take a long time to return a value and slow the export down, if specified this removes these properties
.PARAMETER DisplayPropertySet, .PARAMETER DisplayPropertySet,
Many (but not all) objects have a hidden property named psStandardmembers with a child property DefaultDisplayPropertySet ; this parameter reduces the properties exported to those in this set. Many (but not all) objects have a hidden property named psStandardmembers with a child property DefaultDisplayPropertySet ; this parameter reduces the properties exported to those in this set.
.PARAMETER Title .PARAMETER Title
Text of a title to be placed in Cell A1. Text of a title to be placed in Cell A1.
.PARAMETER TitleBold .PARAMETER TitleBold
@@ -70,18 +70,18 @@
A hash table containing ChartType, Title, NoLegend, ShowCategory, ShowPercent, Yrange, Xrange and SeriesHeader for one or more [non-pivot] charts. A hash table containing ChartType, Title, NoLegend, ShowCategory, ShowPercent, Yrange, Xrange and SeriesHeader for one or more [non-pivot] charts.
.PARAMETER HideSheet .PARAMETER HideSheet
Name(s) of Sheet(s) to hide in the workbook. Name(s) of Sheet(s) to hide in the workbook.
.PARAMETER MoveToStart .PARAMETER MoveToStart
If specified, the worksheet will be moved to the start of the workbook. If specified, the worksheet will be moved to the start of the workbook.
MoveToStart takes precedence over MoveToEnd, Movebefore and MoveAfter if more than one is specified. MoveToStart takes precedence over MoveToEnd, Movebefore and MoveAfter if more than one is specified.
.PARAMETER MoveToEnd .PARAMETER MoveToEnd
If specified, the worksheet will be moved to the end of the workbook. If specified, the worksheet will be moved to the end of the workbook.
(This is the default position for newly created sheets, but this can be used to move existing sheets.) (This is the default position for newly created sheets, but this can be used to move existing sheets.)
.PARAMETER MoveBefore .PARAMETER MoveBefore
If specified, the worksheet will be moved before the nominated one (which can be a postion starting from 1, or a name). If specified, the worksheet will be moved before the nominated one (which can be a postion starting from 1, or a name).
MoveBefore takes precedence over MoveAfter if both are specified. MoveBefore takes precedence over MoveAfter if both are specified.
.PARAMETER MoveAfter .PARAMETER MoveAfter
If specified, the worksheet will be moved after the nominated one (which can be a postion starting from 1, or a name or *). If specified, the worksheet will be moved after the nominated one (which can be a postion starting from 1, or a name or *).
If * is used, the worksheet names will be examined starting with the first one, and the sheet placed after the last sheet which comes before it alphabetically. If * is used, the worksheet names will be examined starting with the first one, and the sheet placed after the last sheet which comes before it alphabetically.
.PARAMETER KillExcel .PARAMETER KillExcel
Closes Excel - prevents errors writing to the file because Excel has it open. Closes Excel - prevents errors writing to the file because Excel has it open.
.PARAMETER AutoNameRange .PARAMETER AutoNameRange
@@ -312,6 +312,8 @@
It then uses the package object to apply formatting, and then saves the workbook and disposes of the object before loading the document in Excel. It then uses the package object to apply formatting, and then saves the workbook and disposes of the object before loading the document in Excel.
.EXAMPLE .EXAMPLE
Remove-Item -Path .\test.xlsx -ErrorAction Ignore
$excel = Get-Process | Select-Object -Property Name,Company,Handles,CPU,PM,NPM,WS | Export-Excel -Path .\test.xlsx -ClearSheet -WorkSheetname "Processes" -PassThru $excel = Get-Process | Select-Object -Property Name,Company,Handles,CPU,PM,NPM,WS | Export-Excel -Path .\test.xlsx -ClearSheet -WorkSheetname "Processes" -PassThru
$sheet = $excel.Workbook.Worksheets["Processes"] $sheet = $excel.Workbook.Worksheets["Processes"]
$sheet.Column(1) | Set-Format -Bold -AutoFit $sheet.Column(1) | Set-Format -Bold -AutoFit
@@ -322,7 +324,7 @@
Set-Format -Address $sheet.Row(1) -Bold -HorizontalAlignment Center Set-Format -Address $sheet.Row(1) -Bold -HorizontalAlignment Center
Add-ConditionalFormatting -WorkSheet $sheet -Range "D2:D1048576" -DataBarColor Red Add-ConditionalFormatting -WorkSheet $sheet -Range "D2:D1048576" -DataBarColor Red
Add-ConditionalFormatting -WorkSheet $sheet -Range "G2:G1048576" -RuleType GreaterThan -ConditionValue "104857600" -ForeGroundColor Red Add-ConditionalFormatting -WorkSheet $sheet -Range "G2:G1048576" -RuleType GreaterThan -ConditionValue "104857600" -ForeGroundColor Red
foreach ($c in 5..9) {Set-Format $sheet.Column($c) -AutoFit } foreach ($c in 5..9) {Set-Format -Address $sheet.Column($c) -AutoFit }
Export-Excel -ExcelPackage $excel -WorkSheetname "Processes" -IncludePivotChart -ChartType ColumnClustered -NoLegend -PivotRows company -PivotData @{'Name'='Count'} -Show Export-Excel -ExcelPackage $excel -WorkSheetname "Processes" -IncludePivotChart -ChartType ColumnClustered -NoLegend -PivotRows company -PivotData @{'Name'='Count'} -Show
This a more sophisticated version of the previous example showing different ways of using Set-Format, and also adding conditional formatting. This a more sophisticated version of the previous example showing different ways of using Set-Format, and also adding conditional formatting.
@@ -354,7 +356,7 @@
[Int]$TitleSize = 22, [Int]$TitleSize = 22,
[System.Drawing.Color]$TitleBackgroundColor, [System.Drawing.Color]$TitleBackgroundColor,
[Switch]$IncludePivotTable, [Switch]$IncludePivotTable,
[String]$PivotTableName, [String]$PivotTableName,
[String[]]$PivotRows, [String[]]$PivotRows,
[String[]]$PivotColumns, [String[]]$PivotColumns,
$PivotData, $PivotData,
@@ -396,8 +398,8 @@
[OfficeOpenXml.Table.TableStyles]$TableStyle = 'Medium6', [OfficeOpenXml.Table.TableStyles]$TableStyle = 'Medium6',
[Object[]]$ExcelChartDefinition, [Object[]]$ExcelChartDefinition,
[String[]]$HideSheet, [String[]]$HideSheet,
[Switch]$MoveToStart, [Switch]$MoveToStart,
[Switch]$MoveToEnd, [Switch]$MoveToEnd,
$MoveBefore , $MoveBefore ,
$MoveAfter , $MoveAfter ,
[Switch]$KillExcel, [Switch]$KillExcel,
@@ -439,7 +441,7 @@
[Object]$TargetCell, [Object]$TargetCell,
[Object]$CellValue [Object]$CellValue
) )
#The write-verbose commands have been commented out below - even if verbose is silenced they cause a significiant performance impact and if it's on they will cause a flood of messages. #The write-verbose commands have been commented out below - even if verbose is silenced they cause a significiant performance impact and if it's on they will cause a flood of messages.
Switch ($CellValue) { Switch ($CellValue) {
{ $_ -is [DateTime]} { { $_ -is [DateTime]} {
# Save a date with an international valid format # Save a date with an international valid format
@@ -447,10 +449,10 @@
$TargetCell.Style.Numberformat.Format = 'm/d/yy h:mm' # This is not a custom format, but a preset recognized as date and localized. $TargetCell.Style.Numberformat.Format = 'm/d/yy h:mm' # This is not a custom format, but a preset recognized as date and localized.
#Write-Verbose "Cell '$Row`:$ColumnIndex' header '$Name' add value '$_' as date" #Write-Verbose "Cell '$Row`:$ColumnIndex' header '$Name' add value '$_' as date"
break break
} }
{ $_ -is [System.ValueType]} { { $_ -is [System.ValueType]} {
# Save numerics, setting format if need be. # Save numerics, setting format if need be.
$TargetCell.Value = $_ $TargetCell.Value = $_
if ($setNumformat) {$targetCell.Style.Numberformat.Format = $Numberformat } if ($setNumformat) {$targetCell.Style.Numberformat.Format = $Numberformat }
#Write-Verbose "Cell '$Row`:$ColumnIndex' header '$Name' add value '$_' as value" #Write-Verbose "Cell '$Row`:$ColumnIndex' header '$Name' add value '$_' as value"
@@ -473,16 +475,16 @@
{ $_ -is [Uri] } { { $_ -is [Uri] } {
# Save a hyperlink # Save a hyperlink
$TargetCell.Value = $_.AbsoluteUri $TargetCell.Value = $_.AbsoluteUri
$TargetCell.HyperLink = $_ $TargetCell.HyperLink = $_
$TargetCell.Style.Font.Color.SetColor([System.Drawing.Color]::Blue) $TargetCell.Style.Font.Color.SetColor([System.Drawing.Color]::Blue)
$TargetCell.Style.Font.UnderLine = $true $TargetCell.Style.Font.UnderLine = $true
#Write-Verbose "Cell '$Row`:$ColumnIndex' header '$Name' add value '$($_.AbsoluteUri)' as Hyperlink" #Write-Verbose "Cell '$Row`:$ColumnIndex' header '$Name' add value '$($_.AbsoluteUri)' as Hyperlink"
break break
} }
Default { Default {
#Save a value as a number if possible #Save a value as a number if possible
$number = $null $number = $null
if ([Double]::TryParse( $_ , [ref]$number)) { if ([Double]::TryParse( $_ , [ref]$number)) {
#was [Double]::TryParse([String]$_, [System.Globalization.NumberStyles]::Any,[System.Globalization.NumberFormatInfo]::CurrentInfo, [Ref]$number)) { #was [Double]::TryParse([String]$_, [System.Globalization.NumberStyles]::Any,[System.Globalization.NumberFormatInfo]::CurrentInfo, [Ref]$number)) {
$TargetCell.Value = $number $TargetCell.Value = $number
@@ -516,11 +518,11 @@
$Path = $pkg.File $Path = $pkg.File
} }
Else { $pkg = Open-ExcelPackage -Path $Path -Create -KillExcel:$KillExcel} Else { $pkg = Open-ExcelPackage -Path $Path -Create -KillExcel:$KillExcel}
$params = @{} $params = @{}
if ($NoClobber) {Write-Warning -Message "-NoClobber parameter is no longer used" } if ($NoClobber) {Write-Warning -Message "-NoClobber parameter is no longer used" }
foreach ($p in @("WorkSheetname", "ClearSheet", "MoveToStart", "MoveToEnd", "MoveBefore", "MoveAfter")) {if ($PSBoundParameters[$p]) {$params[$p] = $PSBoundParameters[$p]}} foreach ($p in @("WorkSheetname", "ClearSheet", "MoveToStart", "MoveToEnd", "MoveBefore", "MoveAfter")) {if ($PSBoundParameters[$p]) {$params[$p] = $PSBoundParameters[$p]}}
$ws = $pkg | Add-WorkSheet @params $ws = $pkg | Add-WorkSheet @params
foreach ($format in $ConditionalFormat ) { foreach ($format in $ConditionalFormat ) {
$target = "Add$($format.Formatter)" $target = "Add$($format.Formatter)"
@@ -549,8 +551,8 @@
} }
#Can only set TitleBackgroundColor if TitleFillPattern is something other than None. #Can only set TitleBackgroundColor if TitleFillPattern is something other than None.
if ($TitleBackgroundColor -and ($TitleFillPattern -ne 'None')) { if ($TitleBackgroundColor -and ($TitleFillPattern -ne 'None')) {
$TitleFillPattern = [OfficeOpenXml.Style.ExcelFillStyle]::Solid $TitleFillPattern = [OfficeOpenXml.Style.ExcelFillStyle]::Solid
} }
$ws.Cells[$Row, $StartColumn].Style.Fill.PatternType = $TitleFillPattern $ws.Cells[$Row, $StartColumn].Style.Fill.PatternType = $TitleFillPattern
if ($TitleBackgroundColor ) { if ($TitleBackgroundColor ) {
@@ -560,8 +562,8 @@
} }
else { $Row = $StartRow } else { $Row = $StartRow }
$ColumnIndex = $StartColumn $ColumnIndex = $StartColumn
$setNumformat = ($numberformat -ne $ws.Cells.Style.Numberformat.Format) $setNumformat = ($numberformat -ne $ws.Cells.Style.Numberformat.Format)
$firstTimeThru = $true $firstTimeThru = $true
$isDataTypeValueType = $false $isDataTypeValueType = $false
} }
@@ -581,7 +583,7 @@
Try { Try {
if ($firstTimeThru) { if ($firstTimeThru) {
$firstTimeThru = $false $firstTimeThru = $false
$isDataTypeValueType = $TargetData.GetType().name -match 'string|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort' $isDataTypeValueType = $TargetData.GetType().name -match 'string|bool|byte|char|decimal|double|float|int|long|sbyte|short|uint|ulong|ushort'
Write-Debug "DataTypeName is '$($TargetData.GetType().name)' isDataTypeValueType '$isDataTypeValueType'" Write-Debug "DataTypeName is '$($TargetData.GetType().name)' isDataTypeValueType '$isDataTypeValueType'"
} }
@@ -597,10 +599,10 @@
if (-not $script:Header) { if (-not $script:Header) {
$ColumnIndex = $StartColumn $ColumnIndex = $StartColumn
if ($DisplayPropertySet -and $TargetData.psStandardmembers.DefaultDisplayPropertySet.ReferencedPropertyNames) { if ($DisplayPropertySet -and $TargetData.psStandardmembers.DefaultDisplayPropertySet.ReferencedPropertyNames) {
$script:Header = $TargetData.psStandardmembers.DefaultDisplayPropertySet.ReferencedPropertyNames.Where( {$_ -notin $ExcludeProperty}) $script:Header = $TargetData.psStandardmembers.DefaultDisplayPropertySet.ReferencedPropertyNames.Where( {$_ -notin $ExcludeProperty})
} }
else { else {
if ($NoAliasOrScriptPropeties) {$propType = "Property"} else {$propType = "*"} if ($NoAliasOrScriptPropeties) {$propType = "Property"} else {$propType = "*"}
$script:Header = $TargetData.PSObject.Properties.where( {$_.MemberType -like $propType -and $_.Name -notin $ExcludeProperty}).Name $script:Header = $TargetData.PSObject.Properties.where( {$_.MemberType -like $propType -and $_.Name -notin $ExcludeProperty}).Name
} }
if ($NoHeader) { if ($NoHeader) {
@@ -639,7 +641,7 @@
if ($AutoNameRange) { if ($AutoNameRange) {
Try { Try {
if (-not $script:header) { 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. # 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 $headerRange = $ws.Dimension.Address -replace "\d+$", $StartRow
#using a slightly odd syntax otherwise header ends up as a 2D array #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 += $_ } $ws.Cells[$headerRange].Value | ForEach-Object -Begin {$Script:header = @()} -Process {$Script:header += $_ }
@@ -648,13 +650,13 @@
} }
else { else {
#if there is a header, start the range and the next row down. #if there is a header, start the range and the next row down.
$targetRow = $StartRow + 1 $targetRow = $StartRow + 1
} }
#Dimension.start.row always seems to be one so we work out the target row #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. #, 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... # 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 # but we have to add the start column on when referencing positions
foreach ($c in 0..($ws.Dimension.Columns - 1)) { foreach ($c in 0..($ws.Dimension.Columns - 1)) {
$targetRangeName = $script:Header[$c] -replace '\W' , '_' $targetRangeName = $script:Header[$c] -replace '\W' , '_'
$targetColumn = $c + $StartColumn $targetColumn = $c + $StartColumn
@@ -688,7 +690,7 @@
} }
#If named range exists, update it, else create it #If named range exists, update it, else create it
if ($ws.Names[$RangeName]) { $ws.Names[$rangename].Address = $ws.Cells[$dataRange].FullAddressAbsolute } if ($ws.Names[$RangeName]) { $ws.Names[$rangename].Address = $ws.Cells[$dataRange].FullAddressAbsolute }
else {$ws.Names.Add($RangeName, $ws.Cells[$dataRange]) | Out-Null } else {$ws.Names.Add($RangeName, $ws.Cells[$dataRange]) | Out-Null }
} }
} }
Catch { Write-Warning -Message "Failed adding range '$RangeName' to worksheet '$WorkSheetname': $_" } Catch { Write-Warning -Message "Failed adding range '$RangeName' to worksheet '$WorkSheetname': $_" }
@@ -706,7 +708,7 @@
$targetRange = $ws.Cells[$csr, $csc, $cer, $cec] $targetRange = $ws.Cells[$csr, $csc, $cer, $cec]
#if the table exists, update it. #if the table exists, update it.
if ($ws.Tables[$TableName]) { if ($ws.Tables[$TableName]) {
$ws.Tables[$TableName].TableXml.table.ref = $targetRange.Address $ws.Tables[$TableName].TableXml.table.ref = $targetRange.Address
$ws.Tables[$TableName].TableStyle = $TableStyle $ws.Tables[$TableName].TableStyle = $TableStyle
} }
else { else {
@@ -715,23 +717,23 @@
} }
Write-Verbose -Message "Defined table '$TableName' at $($targetRange.Address)" Write-Verbose -Message "Defined table '$TableName' at $($targetRange.Address)"
} }
catch {Write-Warning -Message "Failed adding table '$TableName' to worksheet '$WorkSheetname': $_"} catch {Write-Warning -Message "Failed adding table '$TableName' to worksheet '$WorkSheetname': $_"}
} }
if ($PivotTableDefinition) { if ($PivotTableDefinition) {
foreach ($item in $PivotTableDefinition.GetEnumerator()) { foreach ($item in $PivotTableDefinition.GetEnumerator()) {
$params = $item.value $params = $item.value
if ($params.keys -notcontains "SourceRange" -and if ($params.keys -notcontains "SourceRange" -and
($params.Keys -notcontains "SourceWorkSheet" -or $params.SourceWorkSheet -eq $WorkSheetname)) {$params.SourceRange = $dataRange} ($params.Keys -notcontains "SourceWorkSheet" -or $params.SourceWorkSheet -eq $WorkSheetname)) {$params.SourceRange = $dataRange}
if ($params.Keys -notcontains "SourceWorkSheet") {$params.SourceWorkSheet = $ws } if ($params.Keys -notcontains "SourceWorkSheet") {$params.SourceWorkSheet = $ws }
if ($params.Keys -notcontains "NoTotalsInPivot" -and $NoTotalsInPivot ) {$params.NoTotalsInPivot = $true} if ($params.Keys -notcontains "NoTotalsInPivot" -and $NoTotalsInPivot ) {$params.NoTotalsInPivot = $true}
if ($params.Keys -notcontains "PivotDataToColumn" -and $PivotDataToColumn) {$params.PivotDataToColumn = $true} if ($params.Keys -notcontains "PivotDataToColumn" -and $PivotDataToColumn) {$params.PivotDataToColumn = $true}
Add-PivotTable -ExcelPackage $pkg -PivotTableName $item.key @Params Add-PivotTable -ExcelPackage $pkg -PivotTableName $item.key @Params
} }
} }
if ($IncludePivotTable -or $IncludePivotChart) { if ($IncludePivotTable -or $IncludePivotChart) {
$params = @{ $params = @{
"SourceRange" = $dataRange "SourceRange" = $dataRange
} }
if ($PivotTableName) {$params.PivotTableName = $PivotTableName} if ($PivotTableName) {$params.PivotTableName = $PivotTableName}
else {$params.PivotTableName = $WorkSheetname + 'PivotTable'} else {$params.PivotTableName = $WorkSheetname + 'PivotTable'}
@@ -743,7 +745,7 @@
if ($PivotDataToColumn) {$params.PivotDataToColumn = $true} if ($PivotDataToColumn) {$params.PivotDataToColumn = $true}
if ($IncludePivotChart) { if ($IncludePivotChart) {
$params.IncludePivotChart = $true $params.IncludePivotChart = $true
$Params.ChartType = $ChartType $Params.ChartType = $ChartType
if ($ShowCategory) {$params.ShowCategory = $true} if ($ShowCategory) {$params.ShowCategory = $true}
if ($ShowPercent) {$params.ShowPercent = $true} if ($ShowPercent) {$params.ShowPercent = $true}
if ($NoLegend) {$params.NoLegend = $true} if ($NoLegend) {$params.NoLegend = $true}
@@ -753,25 +755,25 @@
if ($AutoFilter) { if ($AutoFilter) {
try { try {
$ws.Cells[$dataRange].AutoFilter = $true $ws.Cells[$dataRange].AutoFilter = $true
Write-Verbose -Message "Enabeld autofilter. " Write-Verbose -Message "Enabeld autofilter. "
} }
catch {Write-Warning -Message "Failed adding autofilter to worksheet '$WorkSheetname': $_"} catch {Write-Warning -Message "Failed adding autofilter to worksheet '$WorkSheetname': $_"}
} }
try { try {
if ($FreezeTopRow) { if ($FreezeTopRow) {
$ws.View.FreezePanes(2, 1) $ws.View.FreezePanes(2, 1)
Write-Verbose -Message "Froze top row" Write-Verbose -Message "Froze top row"
} }
if ($FreezeTopRowFirstColumn) { if ($FreezeTopRowFirstColumn) {
$ws.View.FreezePanes(2, 2) $ws.View.FreezePanes(2, 2)
Write-Verbose -Message "Froze top row and first column" Write-Verbose -Message "Froze top row and first column"
} }
if ($FreezeFirstColumn) { if ($FreezeFirstColumn) {
$ws.View.FreezePanes(1, 2) $ws.View.FreezePanes(1, 2)
Write-Verbose -Message "Froze first column" Write-Verbose -Message "Froze first column"
} }
@@ -788,7 +790,7 @@
} }
} }
catch {Write-Warning -Message "Failed adding Freezing the panes in worksheet '$WorkSheetname': $_"} catch {Write-Warning -Message "Failed adding Freezing the panes in worksheet '$WorkSheetname': $_"}
if ($BoldTopRow) { if ($BoldTopRow) {
try { try {
if ($Title) { if ($Title) {
@@ -800,41 +802,41 @@
$ws.Cells[$range].Style.Font.Bold = $true $ws.Cells[$range].Style.Font.Bold = $true
Write-Verbose -Message "Set $range font style to bold." Write-Verbose -Message "Set $range font style to bold."
} }
catch {Write-Warning -Message "Failed setting the top row to bold in worksheet '$WorkSheetname': $_"} catch {Write-Warning -Message "Failed setting the top row to bold in worksheet '$WorkSheetname': $_"}
} }
if ($AutoSize) { if ($AutoSize) {
try { try {
$ws.Cells.AutoFitColumns() $ws.Cells.AutoFitColumns()
Write-Verbose -Message "Auto-sized columns" Write-Verbose -Message "Auto-sized columns"
} }
catch { Write-Warning -Message "Failed autosizing columns of worksheet '$WorkSheetname': $_"} catch { Write-Warning -Message "Failed autosizing columns of worksheet '$WorkSheetname': $_"}
} }
foreach ($Sheet in $HideSheet) { foreach ($Sheet in $HideSheet) {
try { try {
$pkg.Workbook.WorkSheets[$Sheet].Hidden = 'Hidden' $pkg.Workbook.WorkSheets[$Sheet].Hidden = 'Hidden'
Write-verbose -Message "Sheet '$sheet' Hidden." Write-verbose -Message "Sheet '$sheet' Hidden."
} }
catch {Write-Warning -Message "Failed hiding worksheet '$sheet': $_"} catch {Write-Warning -Message "Failed hiding worksheet '$sheet': $_"}
} }
foreach ($chartDef in $ExcelChartDefinition) { foreach ($chartDef in $ExcelChartDefinition) {
$params = @{} $params = @{}
$chardef.PSObject.Properties | ForEach-Object {if ($_.value -ne $null) {$params[$_.name] = $_.value}} $chardef.PSObject.Properties | ForEach-Object {if ($_.value -ne $null) {$params[$_.name] = $_.value}}
Add-ExcelChart $params Add-ExcelChart $params
} }
foreach ($ct in $ConditionalText) { foreach ($ct in $ConditionalText) {
try { try {
$cfParams = @{RuleType = $ct.ConditionalType; ConditionValue = $ct.text ; $cfParams = @{RuleType = $ct.ConditionalType; ConditionValue = $ct.text ;
BackgroundColor = $ct.BackgroundColor; BackgroundPattern = $ct.PatternType ; BackgroundColor = $ct.BackgroundColor; BackgroundPattern = $ct.PatternType ;
ForeGroundColor = $ct.ConditionalTextColor ForeGroundColor = $ct.ConditionalTextColor
} }
if ($ct.Range) {$cfParams.range = $ct.range} else { $cfParams.Range = $ws.Dimension.Address } if ($ct.Range) {$cfParams.range = $ct.range} else { $cfParams.Range = $ws.Dimension.Address }
Add-ConditionalFormatting -WorkSheet $ws @cfParams Add-ConditionalFormatting -WorkSheet $ws @cfParams
Write-Verbose -Message "Added conditional formatting to range $($ct.range)" Write-Verbose -Message "Added conditional formatting to range $($ct.range)"
} }
catch {Write-Warning -Message "Failed adding conditional formatting to worksheet '$WorkSheetname': $_"} catch {Write-Warning -Message "Failed adding conditional formatting to worksheet '$WorkSheetname': $_"}
} }
if ($CellStyleSB) { if ($CellStyleSB) {
@@ -843,16 +845,16 @@
$LastColumn = (Get-ExcelColumnName $ws.Dimension.Columns).ColumnName $LastColumn = (Get-ExcelColumnName $ws.Dimension.Columns).ColumnName
& $CellStyleSB $ws $TotalRows $LastColumn & $CellStyleSB $ws $TotalRows $LastColumn
} }
catch {Write-Warning -Message "Failed processing CellStyleSB in worksheet '$WorkSheetname': $_"} catch {Write-Warning -Message "Failed processing CellStyleSB in worksheet '$WorkSheetname': $_"}
} }
if ($Password) { if ($Password) {
try { try {
$ws.Protection.SetPassword($Password) $ws.Protection.SetPassword($Password)
Write-Verbose -Message "Set password on workbook" Write-Verbose -Message "Set password on workbook"
} }
catch {throw "Failed setting password for worksheet '$WorkSheetname': $_"} catch {throw "Failed setting password for worksheet '$WorkSheetname': $_"}
} }
if ($PassThru) { $pkg } if ($PassThru) { $pkg }
@@ -864,8 +866,8 @@
if ($ReZip) { if ($ReZip) {
Write-Verbose -Message "Re-Zipping $($pkg.file) using .NET ZIP library" Write-Verbose -Message "Re-Zipping $($pkg.file) using .NET ZIP library"
try { try {
Add-Type -AssemblyName "System.IO.Compression.Filesystem" -ErrorAction stop Add-Type -AssemblyName "System.IO.Compression.Filesystem" -ErrorAction stop
} }
catch { catch {
Write-Error "The -ReZip parameter requires .NET Framework 4.5 or later to be installed. Recommend to install Powershell v4+" Write-Error "The -ReZip parameter requires .NET Framework 4.5 or later to be installed. Recommend to install Powershell v4+"
continue continue
@@ -876,55 +878,55 @@
Remove-Item $pkg.File -Force Remove-Item $pkg.File -Force
[io.compression.zipfile]::CreateFromDirectory($TempZipPath, $pkg.File) | Out-Null [io.compression.zipfile]::CreateFromDirectory($TempZipPath, $pkg.File) | Out-Null
} }
catch {throw "Error resizipping $path : $_"} catch {throw "Error resizipping $path : $_"}
} }
$pkg.Dispose() $pkg.Dispose()
if ($Show) { Invoke-Item $Path } if ($Show) { Invoke-Item $Path }
} }
} }
} }
function New-PivotTableDefinition { function New-PivotTableDefinition {
<# <#
.Synopsis .Synopsis
Creates Pivot table definitons for export excel Creates Pivot table definitons for export excel
.Description .Description
Export-Excel allows a single Pivot table to be defined using the parameters -IncludePivotTable, -PivotColumns -PivotRows, Export-Excel allows a single Pivot table to be defined using the parameters -IncludePivotTable, -PivotColumns -PivotRows,
=PivotData, -PivotFilter, -NoTotalsInPivot, -PivotDataToColumn, -IncludePivotChart and -ChartType. =PivotData, -PivotFilter, -NoTotalsInPivot, -PivotDataToColumn, -IncludePivotChart and -ChartType.
Its -PivotTableDefintion paramater allows multiple pivot tables to be defined, with additional parameters. Its -PivotTableDefintion paramater allows multiple pivot tables to be defined, with additional parameters.
New-PivotTableDefinition is a convenient way to build these definitions. New-PivotTableDefinition is a convenient way to build these definitions.
.Example .Example
$pt = New-PivotTableDefinition -PivotTableName "PT1" -SourceWorkSheet "Sheet1" -PivotRows "Status" -PivotData @{Status='Count' } -PivotFilter 'StartType' -IncludePivotChart -ChartType BarClustered3D $pt = New-PivotTableDefinition -PivotTableName "PT1" -SourceWorkSheet "Sheet1" -PivotRows "Status" -PivotData @{Status='Count' } -PivotFilter 'StartType' -IncludePivotChart -ChartType BarClustered3D
$Pt += New-PivotTableDefinition -PivotTableName "PT2" -SourceWorkSheet "Sheet2" -PivotRows "Company" -PivotData @{Company='Count'} -IncludePivotChart -ChartType PieExploded3D -ShowPercent -ChartTitle "Breakdown of processes by company" $Pt += New-PivotTableDefinition -PivotTableName "PT2" -SourceWorkSheet "Sheet2" -PivotRows "Company" -PivotData @{Company='Count'} -IncludePivotChart -ChartType PieExploded3D -ShowPercent -ChartTitle "Breakdown of processes by company"
Get-Service | Select-Object -Property Status,Name,DisplayName,StartType | Export-Excel -Path .\test.xlsx -AutoSize Get-Service | Select-Object -Property Status,Name,DisplayName,StartType | Export-Excel -Path .\test.xlsx -AutoSize
Get-Process | Select-Object -Property Name,Company,Handles,CPU,VM | Export-Excel -Path .\test.xlsx -AutoSize -WorkSheetname 'sheet2' Get-Process | Select-Object -Property Name,Company,Handles,CPU,VM | Export-Excel -Path .\test.xlsx -AutoSize -WorkSheetname 'sheet2'
$excel = Export-Excel -Path .\test.xlsx -PivotTableDefinition $pt -Show $excel = Export-Excel -Path .\test.xlsx -PivotTableDefinition $pt -Show
This is a re-work of one of the examples in Export-Excel - instead of writing out the pivot definition hash table it is built by calling New-PivotTableDefinition. This is a re-work of one of the examples in Export-Excel - instead of writing out the pivot definition hash table it is built by calling New-PivotTableDefinition.
#> #>
param( param(
[Parameter(Mandatory)] [Parameter(Mandatory)]
[Alias("PivtoTableName")]#Previous typo - use alias to avoid breaking scripts [Alias("PivtoTableName")]#Previous typo - use alias to avoid breaking scripts
$PivotTableName, $PivotTableName,
#Worksheet where the data is found #Worksheet where the data is found
$SourceWorkSheet, $SourceWorkSheet,
#Address range in the worksheet e.g "A10:F20" - the first row must be column names: if not specified the whole sheet will be used/ #Address range in the worksheet e.g "A10:F20" - the first row must be column names: if not specified the whole sheet will be used/
$SourceRange, $SourceRange,
#Fields to set as rows in the Pivot table #Fields to set as rows in the Pivot table
$PivotRows, $PivotRows,
#A hash table in form "FieldName"="Function", where function is one of #A hash table in form "FieldName"="Function", where function is one of
#Average, Count, CountNums, Max, Min, Product, None, StdDev, StdDevP, Sum, Var, VarP #Average, Count, CountNums, Max, Min, Product, None, StdDev, StdDevP, Sum, Var, VarP
[hashtable]$PivotData, [hashtable]$PivotData,
#Fields to set as columns in the Pivot table #Fields to set as columns in the Pivot table
$PivotColumns, $PivotColumns,
#Fields to use to filter in the Pivot table #Fields to use to filter in the Pivot table
$PivotFilter, $PivotFilter,
[Switch]$PivotDataToColumn, [Switch]$PivotDataToColumn,
[Switch]$NoTotalsInPivot, [Switch]$NoTotalsInPivot,
#If specified a chart Will be included. #If specified a chart Will be included.
[Switch]$IncludePivotChart, [Switch]$IncludePivotChart,
#Optional title for the pivot chart, by default the title omitted. #Optional title for the pivot chart, by default the title omitted.
[String]$ChartTitle, [String]$ChartTitle,
@@ -932,9 +934,9 @@ function New-PivotTableDefinition {
[int]$ChartHeight = 400 , [int]$ChartHeight = 400 ,
#Width of the chart in Pixels (600 by default) #Width of the chart in Pixels (600 by default)
[int]$ChartWidth = 600, [int]$ChartWidth = 600,
#Cell position of the top left corner of the chart, there will be this number of rows above the top edge of the chart (default is 0, chart starts at top edge of row 1). #Cell position of the top left corner of the chart, there will be this number of rows above the top edge of the chart (default is 0, chart starts at top edge of row 1).
[Int]$ChartRow = 0 , [Int]$ChartRow = 0 ,
#Cell position of the top left corner of the chart, there will be this number of cells to the left of the chart (default is 4, chart starts at left edge of column E) #Cell position of the top left corner of the chart, there will be this number of cells to the left of the chart (default is 4, chart starts at left edge of column E)
[Int]$ChartColumn = 4, [Int]$ChartColumn = 4,
#Vertical offset of the chart from the cell corner. #Vertical offset of the chart from the cell corner.
[Int]$ChartRowOffSetPixels = 0 , [Int]$ChartRowOffSetPixels = 0 ,
@@ -944,7 +946,7 @@ function New-PivotTableDefinition {
[OfficeOpenXml.Drawing.Chart.eChartType]$ChartType = 'Pie', [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType = 'Pie',
#If specified hides the chart legend #If specified hides the chart legend
[Switch]$NoLegend, [Switch]$NoLegend,
#if specified attaches the category to slices in a pie chart : not supported on all chart types, this may give errors if applied to an unsupported type. #if specified attaches the category to slices in a pie chart : not supported on all chart types, this may give errors if applied to an unsupported type.
[Switch]$ShowCategory, [Switch]$ShowCategory,
#If specified attaches percentages to slices in a pie chart. #If specified attaches percentages to slices in a pie chart.
[Switch]$ShowPercent [Switch]$ShowPercent
@@ -953,7 +955,7 @@ function New-PivotTableDefinition {
if ($PivotData.values.Where({$_ -notin $validDataFuntions}) ) { if ($PivotData.values.Where({$_ -notin $validDataFuntions}) ) {
Write-Warning -Message ("Pivot data functions might not be valid, they should be one of " + ($validDataFuntions -join ", ") + ".") Write-Warning -Message ("Pivot data functions might not be valid, they should be one of " + ($validDataFuntions -join ", ") + ".")
} }
$parameters = @{} + $PSBoundParameters $parameters = @{} + $PSBoundParameters
$parameters.Remove('PivotTableName') $parameters.Remove('PivotTableName')
@@ -961,35 +963,35 @@ function New-PivotTableDefinition {
@{$PivotTableName = $parameters} @{$PivotTableName = $parameters}
} }
function Add-WorkSheet { function Add-WorkSheet {
[cmdletBinding()] [cmdletBinding()]
[OutputType([OfficeOpenXml.ExcelWorksheet])] [OutputType([OfficeOpenXml.ExcelWorksheet])]
param( param(
#An object representing an Excel Package. #An object representing an Excel Package.
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "Package", Position = 0)] [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "Package", Position = 0)]
[OfficeOpenXml.ExcelPackage]$ExcelPackage, [OfficeOpenXml.ExcelPackage]$ExcelPackage,
#An Excel workbook to which the Worksheet will be added - a package contains one workbook so you can use whichever fits at the time. #An Excel workbook to which the Worksheet will be added - a package contains one workbook so you can use whichever fits at the time.
[Parameter(Mandatory = $true, ParameterSetName = "WorkBook")] [Parameter(Mandatory = $true, ParameterSetName = "WorkBook")]
[OfficeOpenXml.ExcelWorkbook]$ExcelWorkbook, [OfficeOpenXml.ExcelWorkbook]$ExcelWorkbook,
#The name of the worksheet 'Sheet1' by default. #The name of the worksheet 'Sheet1' by default.
[string]$WorkSheetname = 'Sheet1', [string]$WorkSheetname = 'Sheet1',
#If the worksheet already exists, by default it will returned, unless -ClearSheet is specified in which case it will be deleted and re-created. #If the worksheet already exists, by default it will returned, unless -ClearSheet is specified in which case it will be deleted and re-created.
[switch]$ClearSheet, [switch]$ClearSheet,
#If specified, the worksheet will be moved to the start of the workbook. #If specified, the worksheet will be moved to the start of the workbook.
#MoveToStart takes precedence over MoveToEnd, Movebefore and MoveAfter if more than one is specified. #MoveToStart takes precedence over MoveToEnd, Movebefore and MoveAfter if more than one is specified.
[Switch]$MoveToStart, [Switch]$MoveToStart,
#If specified, the worksheet will be moved to the end of the workbook. #If specified, the worksheet will be moved to the end of the workbook.
#(This is the default position for newly created sheets, but this can be used to move existing sheets.) #(This is the default position for newly created sheets, but this can be used to move existing sheets.)
[Switch]$MoveToEnd, [Switch]$MoveToEnd,
#If specified, the worksheet will be moved before the nominated one (which can be a postion starting from 1, or a name). #If specified, the worksheet will be moved before the nominated one (which can be a postion starting from 1, or a name).
#MoveBefore takes precedence over MoveAfter if both are specified. #MoveBefore takes precedence over MoveAfter if both are specified.
$MoveBefore , $MoveBefore ,
# If specified, the worksheet will be moved after the nominated one (which can be a postion starting from 1, or a name or *). # If specified, the worksheet will be moved after the nominated one (which can be a postion starting from 1, or a name or *).
# If * is used, the worksheet names will be examined starting with the first one, and the sheet placed after the last sheet which comes before it alphabetically. # If * is used, the worksheet names will be examined starting with the first one, and the sheet placed after the last sheet which comes before it alphabetically.
$MoveAfter , $MoveAfter ,
#If worksheet is provided as a copy source the new worksheet will be a copy of it. The source can be in the same workbook, or in a different file. #If worksheet is provided as a copy source the new worksheet will be a copy of it. The source can be in the same workbook, or in a different file.
[OfficeOpenXml.ExcelWorksheet]$CopySource, [OfficeOpenXml.ExcelWorksheet]$CopySource,
#Ignored but retained for backwards compatibility. #Ignored but retained for backwards compatibility.
[Switch] $NoClobber [Switch] $NoClobber
) )
if ($ExcelPackage -and -not $ExcelWorkbook) {$ExcelWorkbook = $ExcelPackage.Workbook} if ($ExcelPackage -and -not $ExcelWorkbook) {$ExcelWorkbook = $ExcelPackage.Workbook}
@@ -1011,7 +1013,7 @@ function Add-WorkSheet {
if ($ExcelWorkbook.Worksheets[$MoveBefore]) { if ($ExcelWorkbook.Worksheets[$MoveBefore]) {
if ($MoveBefore -is [int]) { if ($MoveBefore -is [int]) {
$ExcelWorkbook.Worksheets.MoveBefore($ws.Index, $MoveBefore) $ExcelWorkbook.Worksheets.MoveBefore($ws.Index, $MoveBefore)
} }
else {$ExcelWorkbook.Worksheets.MoveBefore($worksheetname, $MoveBefore)} else {$ExcelWorkbook.Worksheets.MoveBefore($worksheetname, $MoveBefore)}
} }
else {Write-Warning "Can't find worksheet '$MoveBefore'; worsheet '$WorkSheetname' will not be moved."} else {Write-Warning "Can't find worksheet '$MoveBefore'; worsheet '$WorkSheetname' will not be moved."}
@@ -1024,11 +1026,11 @@ function Add-WorkSheet {
While ($i -lt $ExcelWorkbook.Worksheets.Count -and ($ExcelWorkbook.Worksheets[$i + 1].Name -le $worksheetname) ) { $i++} While ($i -lt $ExcelWorkbook.Worksheets.Count -and ($ExcelWorkbook.Worksheets[$i + 1].Name -le $worksheetname) ) { $i++}
$ExcelWorkbook.Worksheets.MoveAfter($ws.Index, $i) $ExcelWorkbook.Worksheets.MoveAfter($ws.Index, $i)
} }
} }
elseif ($ExcelWorkbook.Worksheets[$MoveAfter]) { elseif ($ExcelWorkbook.Worksheets[$MoveAfter]) {
if ($MoveAfter -is [int]) { if ($MoveAfter -is [int]) {
$ExcelWorkbook.Worksheets.MoveAfter($ws.Index, $MoveAfter) $ExcelWorkbook.Worksheets.MoveAfter($ws.Index, $MoveAfter)
} }
else { else {
$ExcelWorkbook.Worksheets.MoveAfter($worksheetname, $MoveAfter) $ExcelWorkbook.Worksheets.MoveAfter($worksheetname, $MoveAfter)
} }
@@ -1044,22 +1046,22 @@ function Add-PivotTable {
[Parameter(Mandatory = $true)] [Parameter(Mandatory = $true)]
$PivotTableName, $PivotTableName,
$ExcelPackage, $ExcelPackage,
#Worksheet where the data is found #Worksheet where the data is found
$SourceWorkSheet, $SourceWorkSheet,
#Address range in the worksheet e.g "A10:F20" - the first row must be column names: if not specified the whole sheet will be used/ #Address range in the worksheet e.g "A10:F20" - the first row must be column names: if not specified the whole sheet will be used/
$SourceRange, $SourceRange,
#Fields to set as rows in the Pivot table #Fields to set as rows in the Pivot table
$PivotRows, $PivotRows,
#A hash table in form "FieldName"="Function", where function is one of #A hash table in form "FieldName"="Function", where function is one of
#Average, Count, CountNums, Max, Min, Product, None, StdDev, StdDevP, Sum, Var, VarP #Average, Count, CountNums, Max, Min, Product, None, StdDev, StdDevP, Sum, Var, VarP
$PivotData, $PivotData,
#Fields to set as columns in the Pivot table #Fields to set as columns in the Pivot table
$PivotColumns, $PivotColumns,
#Fields to use to filter in the Pivot table #Fields to use to filter in the Pivot table
$PivotFilter, $PivotFilter,
[Switch]$PivotDataToColumn, [Switch]$PivotDataToColumn,
[Switch]$NoTotalsInPivot, [Switch]$NoTotalsInPivot,
#If specified a chart Will be included. #If specified a chart Will be included.
[Switch]$IncludePivotChart, [Switch]$IncludePivotChart,
#Optional title for the pivot chart, by default the title omitted. #Optional title for the pivot chart, by default the title omitted.
[String]$ChartTitle, [String]$ChartTitle,
@@ -1067,9 +1069,9 @@ function Add-PivotTable {
[int]$ChartHeight = 400 , [int]$ChartHeight = 400 ,
#Width of the chart in Pixels (600 by default) #Width of the chart in Pixels (600 by default)
[int]$ChartWidth = 600, [int]$ChartWidth = 600,
#Cell position of the top left corner of the chart, there will be this number of rows above the top edge of the chart (default is 0, chart starts at top edge of row 1). #Cell position of the top left corner of the chart, there will be this number of rows above the top edge of the chart (default is 0, chart starts at top edge of row 1).
[Int]$ChartRow = 0 , [Int]$ChartRow = 0 ,
#Cell position of the top left corner of the chart, there will be this number of cells to the left of the chart (default is 4, chart starts at left edge of column E) #Cell position of the top left corner of the chart, there will be this number of cells to the left of the chart (default is 4, chart starts at left edge of column E)
[Int]$ChartColumn = 4, [Int]$ChartColumn = 4,
#Vertical offset of the chart from the cell corner. #Vertical offset of the chart from the cell corner.
[Int]$ChartRowOffSetPixels = 0 , [Int]$ChartRowOffSetPixels = 0 ,
@@ -1079,44 +1081,44 @@ function Add-PivotTable {
[OfficeOpenXml.Drawing.Chart.eChartType]$ChartType = 'Pie', [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType = 'Pie',
#If specified hides the chart legend #If specified hides the chart legend
[Switch]$NoLegend, [Switch]$NoLegend,
#if specified attaches the category to slices in a pie chart : not supported on all chart types, this may give errors if applied to an unsupported type. #if specified attaches the category to slices in a pie chart : not supported on all chart types, this may give errors if applied to an unsupported type.
[Switch]$ShowCategory, [Switch]$ShowCategory,
#If specified attaches percentages to slices in a pie chart. #If specified attaches percentages to slices in a pie chart.
[Switch]$ShowPercent [Switch]$ShowPercent
) )
$pivotTableDataName = $pivotTableName + 'PivotTableData'
[OfficeOpenXml.ExcelWorksheet]$wsPivot = Add-WorkSheet -ExcelPackage $ExcelPackage -WorkSheetname $pivotTableName
# $wsPivot.View.TabSelected = $true
#if the pivot doesn't exist, create it. $pivotTableDataName = $pivotTableName + 'PivotTableData'
[OfficeOpenXml.ExcelWorksheet]$wsPivot = Add-WorkSheet -ExcelPackage $ExcelPackage -WorkSheetname $pivotTableName
# $wsPivot.View.TabSelected = $true
#if the pivot doesn't exist, create it.
if (-not $wsPivot.PivotTables[$pivotTableDataName] ) { if (-not $wsPivot.PivotTables[$pivotTableDataName] ) {
try { try {
#Accept a string or a worksheet object as $Source Worksheet. #Accept a string or a worksheet object as $Source Worksheet.
if ($SourceWorkSheet -is [string]) { if ($SourceWorkSheet -is [string]) {
$SourceWorkSheet = $ExcelPackage.Workbook.Worksheets.where( {$_.name -match $SourceWorkSheet})[0] $SourceWorkSheet = $ExcelPackage.Workbook.Worksheets.where( {$_.name -match $SourceWorkSheet})[0]
} }
if (-not ($SourceWorkSheet -is [OfficeOpenXml.ExcelWorksheet])) {Write-Warning -Message "Could not find source Worksheet for pivot-table '$pivotTableName'." } if (-not ($SourceWorkSheet -is [OfficeOpenXml.ExcelWorksheet])) {Write-Warning -Message "Could not find source Worksheet for pivot-table '$pivotTableName'." }
else { else {
if ($PivotFilter) {$PivotTableStartCell = "A3"} else { $PivotTableStartCell = "A1"} if ($PivotFilter) {$PivotTableStartCell = "A3"} else { $PivotTableStartCell = "A1"}
if (-not $SourceRange) { $SourceRange = $SourceWorkSheet.Dimension.Address} if (-not $SourceRange) { $SourceRange = $SourceWorkSheet.Dimension.Address}
$pivotTable = $wsPivot.PivotTables.Add($wsPivot.Cells[$PivotTableStartCell], $SourceWorkSheet.Cells[ $SourceRange], $pivotTableDataName) $pivotTable = $wsPivot.PivotTables.Add($wsPivot.Cells[$PivotTableStartCell], $SourceWorkSheet.Cells[ $SourceRange], $pivotTableDataName)
} }
foreach ($Row in $PivotRows) { foreach ($Row in $PivotRows) {
try {$null = $pivotTable.RowFields.Add($pivotTable.Fields[$Row]) } try {$null = $pivotTable.RowFields.Add($pivotTable.Fields[$Row]) }
catch {Write-Warning -message "Could not add '$row' to Rows in PivotTable $pivotTableName." } catch {Write-Warning -message "Could not add '$row' to Rows in PivotTable $pivotTableName." }
} }
foreach ($Column in $PivotColumns) { foreach ($Column in $PivotColumns) {
try {$null = $pivotTable.ColumnFields.Add($pivotTable.Fields[$Column])} try {$null = $pivotTable.ColumnFields.Add($pivotTable.Fields[$Column])}
catch {Write-Warning -message "Could not add '$Column' to Columns in PivotTable $pivotTableName." } catch {Write-Warning -message "Could not add '$Column' to Columns in PivotTable $pivotTableName." }
} }
if ($PivotData -is [HashTable] -or $PivotData -is [System.Collections.Specialized.OrderedDictionary]) { if ($PivotData -is [HashTable] -or $PivotData -is [System.Collections.Specialized.OrderedDictionary]) {
$PivotData.Keys | ForEach-Object { $PivotData.Keys | ForEach-Object {
try { try {
$df = $pivotTable.DataFields.Add($pivotTable.Fields[$_]) $df = $pivotTable.DataFields.Add($pivotTable.Fields[$_])
$df.Function = $PivotData.$_ $df.Function = $PivotData.$_
} }
catch {Write-Warning -message "Problem adding data fields to PivotTable $pivotTableName." } catch {Write-Warning -message "Problem adding data fields to PivotTable $pivotTableName." }
} }
} }
else { else {
@@ -1125,29 +1127,29 @@ function Add-PivotTable {
$df = $pivotTable.DataFields.Add($pivotTable.Fields[$field]) $df = $pivotTable.DataFields.Add($pivotTable.Fields[$field])
$df.Function = 'Count' $df.Function = 'Count'
} }
catch {Write-Warning -message "Problem adding data field '$field' to PivotTable $pivotTableName." } catch {Write-Warning -message "Problem adding data field '$field' to PivotTable $pivotTableName." }
} }
} }
foreach ( $pFilter in $PivotFilter) { foreach ( $pFilter in $PivotFilter) {
try { $null = $pivotTable.PageFields.Add($pivotTable.Fields[$pFilter])} try { $null = $pivotTable.PageFields.Add($pivotTable.Fields[$pFilter])}
catch {Write-Warning -message "Could not add '$pFilter' to Filter/Page fields in PivotTable $pivotTableName." } catch {Write-Warning -message "Could not add '$pFilter' to Filter/Page fields in PivotTable $pivotTableName." }
} }
if ($NoTotalsInPivot) { $pivotTable.RowGrandTotals = $false } if ($NoTotalsInPivot) { $pivotTable.RowGrandTotals = $false }
if ($PivotDataToColumn ) { $pivotTable.DataOnRows = $false } if ($PivotDataToColumn ) { $pivotTable.DataOnRows = $false }
} }
catch {Write-Warning -Message "Failed adding PivotTable '$pivotTableName': $_"} catch {Write-Warning -Message "Failed adding PivotTable '$pivotTableName': $_"}
} }
else { else {
Write-Warning -Message "Pivot table defined in $($pivotTableName) already exists, only the data range will be changed." Write-Warning -Message "Pivot table defined in $($pivotTableName) already exists, only the data range will be changed."
$pivotTable = $wsPivot.PivotTables[$pivotTableDataName] $pivotTable = $wsPivot.PivotTables[$pivotTableDataName]
$pivotTable.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref = $SourceRange $pivotTable.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref = $SourceRange
} }
#Create the chart if it doesn't exist, leave alone if it does. #Create the chart if it doesn't exist, leave alone if it does.
if ($IncludePivotChart -and -not $wsPivot.Drawings['PivotChart'] ) { if ($IncludePivotChart -and -not $wsPivot.Drawings['PivotChart'] ) {
try { try {
[OfficeOpenXml.Drawing.Chart.ExcelChart] $chart = $wsPivot.Drawings.AddChart('PivotChart', $ChartType, $pivotTable) [OfficeOpenXml.Drawing.Chart.ExcelChart] $chart = $wsPivot.Drawings.AddChart('PivotChart', $ChartType, $pivotTable)
$chart.SetPosition($ChartRow , $ChartRowOffSetPixels , $ChartColumn, $ChartColumnOffSetPixels) $chart.SetPosition($ChartRow , $ChartRowOffSetPixels , $ChartColumn, $ChartColumnOffSetPixels)
$chart.SetSize( $ChartWidth, $ChartHeight) $chart.SetSize( $ChartWidth, $ChartHeight)
if ($chart.DataLabel) { if ($chart.DataLabel) {
$chart.DataLabel.ShowCategory = [boolean]$ShowCategory $chart.DataLabel.ShowCategory = [boolean]$ShowCategory
@@ -1158,7 +1160,7 @@ function Add-PivotTable {
} }
catch { catch {Write-Warning -Message "Failed adding chart for pivotable '$pivotTableName': $_"} catch { catch {Write-Warning -Message "Failed adding chart for pivotable '$pivotTableName': $_"}
} }
} }
} }
function Add-ExcelChart { function Add-ExcelChart {
@@ -1197,7 +1199,7 @@ function Add-ExcelChart {
$chartDefCount = @($YRange).Count $chartDefCount = @($YRange).Count
if ($chartDefCount -eq 1) { if ($chartDefCount -eq 1) {
$Series = $chart.Series.Add($YRange, $XRange) $Series = $chart.Series.Add($YRange, $XRange)
if ($SeriesHeader) { $Series.Header = $SeriesHeader} if ($SeriesHeader) { $Series.Header = $SeriesHeader}
else { $Series.Header = 'Series 1'} else { $Series.Header = 'Series 1'}
} }
@@ -1209,5 +1211,5 @@ function Add-ExcelChart {
} }
} }
} }
catch {Write-Warning -Message "Failed adding Chart to worksheet '$($WorkSheet).name': $_"} catch {Write-Warning -Message "Failed adding Chart to worksheet '$($WorkSheet).name': $_"}
} }

View File

@@ -4,7 +4,7 @@
RootModule = 'ImportExcel.psm1' RootModule = 'ImportExcel.psm1'
# Version number of this module. # Version number of this module.
ModuleVersion = '4.0.14' ModuleVersion = '5.0.1'
# ID used to uniquely identify this module # ID used to uniquely identify this module
GUID = '60dd4136-feff-401a-ba27-a84458c57ede' GUID = '60dd4136-feff-401a-ba27-a84458c57ede'

View File

@@ -93,14 +93,14 @@ function Import-Excel {
When the parameters -NoHeader and -HeaderName are not provided, this row will contain the column headers that will be used as property names. When one of both parameters are provided, the property names are automatically created and this row will be treated as a regular row containing data. When the parameters -NoHeader and -HeaderName are not provided, this row will contain the column headers that will be used as property names. When one of both parameters are provided, the property names are automatically created and this row will be treated as a regular row containing data.
.PARAMETER EndRow .PARAMETER EndRow
By default all rows up to the last cell in the sheet will be imported. If specified, import stops at this row. By default all rows up to the last cell in the sheet will be imported. If specified, import stops at this row.
.PARAMETER StartColumn .PARAMETER StartColumn
The number of the first column to read data from (1 by default). The number of the first column to read data from (1 by default).
.PARAMETER EndColumn .PARAMETER EndColumn
By default the import reads up to the last populated column, -EndColumn tells the import to stop at an earlier number. By default the import reads up to the last populated column, -EndColumn tells the import to stop at an earlier number.
.PARAMETER Password .PARAMETER Password
Accepts a string that will be used to open a password protected Excel file. Accepts a string that will be used to open a password protected Excel file.
@@ -325,12 +325,12 @@ function Import-Excel {
#region Open file #region Open file
$Path = (Resolve-Path $Path).ProviderPath $Path = (Resolve-Path $Path).ProviderPath
Write-Verbose "Import Excel workbook '$Path' with worksheet '$Worksheetname'" Write-Verbose "Import Excel workbook '$Path' with worksheet '$Worksheetname'"
$Stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path, 'Open', 'Read', 'ReadWrite' $Stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path, 'Open', 'Read', 'ReadWrite'
if ($Password) { if ($Password) {
$Excel = New-Object -TypeName OfficeOpenXml.ExcelPackage $Excel = New-Object -TypeName OfficeOpenXml.ExcelPackage
Try { Try {
$Excel.Load($Stream,$Password) $Excel.Load($Stream,$Password)
} }
@@ -342,7 +342,7 @@ function Import-Excel {
$Excel = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Stream $Excel = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Stream
} }
#endregion #endregion
#region Select worksheet #region Select worksheet
if ($WorksheetName) { if ($WorksheetName) {
if (-not ($Worksheet = $Excel.Workbook.Worksheets[$WorkSheetName])) { if (-not ($Worksheet = $Excel.Workbook.Worksheets[$WorkSheetName])) {
@@ -482,7 +482,7 @@ function ConvertFrom-ExcelSheet {
$stream.Close() $stream.Close()
$stream.Dispose() $stream.Dispose()
$xl.Dispose() $xl.Dispose()
} }
function Export-MultipleExcelSheets { function Export-MultipleExcelSheets {

View File

@@ -44,6 +44,7 @@ Begin {
'Import-Html.ps1', 'Import-Html.ps1',
'InferData.ps1', 'InferData.ps1',
'Invoke-Sum.ps1', 'Invoke-Sum.ps1',
'Merge-Worksheet.ps1',
'New-ConditionalFormattingIconSet.ps1', 'New-ConditionalFormattingIconSet.ps1',
'New-ConditionalText.ps1', 'New-ConditionalText.ps1',
'New-ExcelChart.ps1', 'New-ExcelChart.ps1',

View File

@@ -1,4 +1,4 @@
Import-Module .\ImportExcel.psd1 -Force Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
$xlFile = ".\testSQL.xlsx" $xlFile = ".\testSQL.xlsx"

View File

@@ -1,56 +1,56 @@
#Requires -Modules Pester #Requires -Modules Pester
$here = Split-Path -Parent $MyInvocation.MyCommand.Path # $here = Split-Path -Parent $MyInvocation.MyCommand.Path
# Import-Module $here -Force -Verbose
Import-Module $here -Force -Verbose Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force
if (Get-process -Name Excel,xlim -ErrorAction SilentlyContinue) { Write-Warning -Message "You need to close Excel before running the tests." ; return} if (Get-process -Name Excel,xlim -ErrorAction SilentlyContinue) { Write-Warning -Message "You need to close Excel before running the tests." ; return}
Describe ExportExcel { Describe ExportExcel {
Context "#Example 1 # Creates and opens a file with the right number of rows and columns" { Context "#Example 1 # Creates and opens a file with the right number of rows and columns" {
$path = "$env:TEMP\Test.xlsx" $path = "$env:TEMP\Test.xlsx"
Remove-item -Path $path -ErrorAction SilentlyContinue Remove-item -Path $path -ErrorAction SilentlyContinue
$processes = Get-Process $processes = Get-Process
$propertyNames = $Processes[0].psobject.properties.name $propertyNames = $Processes[0].psobject.properties.name
$rowcount = $Processes.Count $rowcount = $Processes.Count
$Processes | Export-Excel $path -show $Processes | Export-Excel $path -show
it "Created a new file " { it "Created a new file " {
Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true
} }
it "Started Excel to display the file " { it "Started Excel to display the file " {
Get-process -Name Excel,xlim -ErrorAction SilentlyContinue | should not benullorempty Get-process -Name Excel,xlim -ErrorAction SilentlyContinue | should not benullorempty
} }
Start-Sleep -Seconds 5 ;
#Open-ExcelPackage with -Create is tested in Export-Excel Start-Sleep -Seconds 5 ;
#This is a test of using it with -KillExcel
#TODO Need to test opening pre-existing file with no -create switch (and graceful failure when file does not exist) somewhere else #Open-ExcelPackage with -Create is tested in Export-Excel
#This is a test of using it with -KillExcel
#TODO Need to test opening pre-existing file with no -create switch (and graceful failure when file does not exist) somewhere else
$Excel = Open-ExcelPackage -Path $path -KillExcel $Excel = Open-ExcelPackage -Path $path -KillExcel
it "Killed Excel when Open-Excelpackage was told to " { it -Skip "Killed Excel when Open-Excelpackage was told to " {
Get-process -Name Excel,xlim -ErrorAction SilentlyContinue | should benullorempty Get-process -Name Excel,xlim -ErrorAction SilentlyContinue | should benullorempty
} }
it "Created 1 worksheet " { it "Created 1 worksheet " {
$Excel.Workbook.Worksheets.count | should be 1 $Excel.Workbook.Worksheets.count | should be 1
} }
$ws = $Excel.Workbook.Worksheets[1] $ws = $Excel.Workbook.Worksheets[1]
it "Created the worksheet with the expected name, number of rows and number of columns " { it "Created the worksheet with the expected name, number of rows and number of columns " {
$ws.Name | should be "sheet1" $ws.Name | should be "sheet1"
$ws.Dimension.Columns | should be $propertyNames.Count $ws.Dimension.Columns | should be $propertyNames.Count
$ws.Dimension.Rows | should be ($rowcount + 1) $ws.Dimension.Rows | should be ($rowcount + 1)
} }
$headingNames = $ws.cells["1:1"].Value $headingNames = $ws.cells["1:1"].Value
it "Created the worksheet with the correct header names " { it "Created the worksheet with the correct header names " {
foreach ($p in $propertyNames) { foreach ($p in $propertyNames) {
$headingnames -contains $p | should be $true $headingnames -contains $p | should be $true
} }
} }
it "Formatted the process StartTime field as 'local short date' " { it "Formatted the process StartTime field as 'local short date' " {
$STHeader = $ws.cells["1:1"].where({$_.Value -eq "StartTime"})[0] $STHeader = $ws.cells["1:1"].where({$_.Value -eq "StartTime"})[0]
$STCell = $STHeader.Address -replace '1$','2' $STCell = $STHeader.Address -replace '1$','2'
@@ -65,89 +65,89 @@ Describe ExportExcel {
} }
Context " # NoAliasOrScriptPropeties -ExcludeProperty and -DisplayPropertySet work" { Context " # NoAliasOrScriptPropeties -ExcludeProperty and -DisplayPropertySet work" {
$path = "$env:TEMP\Test.xlsx" $path = "$env:TEMP\Test.xlsx"
Remove-item -Path $path -ErrorAction SilentlyContinue Remove-item -Path $path -ErrorAction SilentlyContinue
$processes = Get-Process $processes = Get-Process
$propertyNames = $Processes[0].psobject.properties.where( {$_.MemberType -eq 'Property'}).name $propertyNames = $Processes[0].psobject.properties.where( {$_.MemberType -eq 'Property'}).name
$rowcount = $Processes.Count $rowcount = $Processes.Count
#TestCreating a range with a name which needs illegal chars removing #TestCreating a range with a name which needs illegal chars removing
$warnVar = $null $warnVar = $null
$Processes | Export-Excel $path -NoAliasOrScriptPropeties -RangeName "No Spaces" -WarningVariable warnvar -WarningAction SilentlyContinue $Processes | Export-Excel $path -NoAliasOrScriptPropeties -RangeName "No Spaces" -WarningVariable warnvar -WarningAction SilentlyContinue
$Excel = Open-ExcelPackage -Path $path $Excel = Open-ExcelPackage -Path $path
$ws = $Excel.Workbook.Worksheets[1] $ws = $Excel.Workbook.Worksheets[1]
it "Created a new file with alias & Script Properties removed. " { it "Created a new file with alias & Script Properties removed. " {
$ws.Name | should be "sheet1" $ws.Name | should be "sheet1"
$ws.Dimension.Columns | should be $propertyNames.Count $ws.Dimension.Columns | should be $propertyNames.Count
$ws.Dimension.Rows | should be ($rowcount + 1 ) # +1 for the header. $ws.Dimension.Rows | should be ($rowcount + 1 ) # +1 for the header.
} }
it "Created a Range - even though the name given was invalid. " { it "Created a Range - even though the name given was invalid. " {
$ws.Names["No_spaces"] | should not beNullOrEmpty $ws.Names["No_spaces"] | should not beNullOrEmpty
$ws.Names["No_spaces"].End.Column | should be $propertyNames.Count $ws.Names["No_spaces"].End.Column | should be $propertyNames.Count
$ws.names["No_spaces"].End.Row | should be ($rowcount + 1 ) # +1 for the header. $ws.names["No_spaces"].End.Row | should be ($rowcount + 1 ) # +1 for the header.
$warnVar.Count | should be 1 $warnVar.Count | should be 1
} }
#This time use clearsheet instead of deleting the file #This time use clearsheet instead of deleting the file
$Processes | Export-Excel $path -NoAliasOrScriptPropeties -ExcludeProperty SafeHandle, modules, MainModule, StartTime, Threads -ClearSheet $Processes | Export-Excel $path -NoAliasOrScriptPropeties -ExcludeProperty SafeHandle, modules, MainModule, StartTime, Threads -ClearSheet
$Excel = Open-ExcelPackage -Path $path $Excel = Open-ExcelPackage -Path $path
$ws = $Excel.Workbook.Worksheets[1] $ws = $Excel.Workbook.Worksheets[1]
it "Created a new file with a further 5 properties excluded and cleared the old sheet " { it "Created a new file with a further 5 properties excluded and cleared the old sheet " {
$ws.Name | should be "sheet1" $ws.Name | should be "sheet1"
$ws.Dimension.Columns | should be ($propertyNames.Count - 5) $ws.Dimension.Columns | should be ($propertyNames.Count - 5)
$ws.Dimension.Rows | should be ($rowcount + 1) # +1 for the header $ws.Dimension.Rows | should be ($rowcount + 1) # +1 for the header
} }
$propertyNames = $Processes[0].psStandardmembers.DefaultDisplayPropertySet.ReferencedPropertyNames $propertyNames = $Processes[0].psStandardmembers.DefaultDisplayPropertySet.ReferencedPropertyNames
Remove-item -Path $path -ErrorAction SilentlyContinue Remove-item -Path $path -ErrorAction SilentlyContinue
$Processes | Export-Excel $path -DisplayPropertySet $Processes | Export-Excel $path -DisplayPropertySet
$Excel = Open-ExcelPackage -Path $path $Excel = Open-ExcelPackage -Path $path
$ws = $Excel.Workbook.Worksheets[1] $ws = $Excel.Workbook.Worksheets[1]
it "Created a new file with just the members of the Display Property Set " { it "Created a new file with just the members of the Display Property Set " {
$ws.Name | should be "sheet1" $ws.Name | should be "sheet1"
$ws.Dimension.Columns | should be $propertyNames.Count $ws.Dimension.Columns | should be $propertyNames.Count
$ws.Dimension.Rows | should be ($rowcount + 1) $ws.Dimension.Rows | should be ($rowcount + 1)
} }
} }
Context "#Example 2 # Exports a list of numbers and applies number format " { Context "#Example 2 # Exports a list of numbers and applies number format " {
$path = "$env:TEMP\Test.xlsx" $path = "$env:TEMP\Test.xlsx"
Remove-item -Path $path -ErrorAction SilentlyContinue Remove-item -Path $path -ErrorAction SilentlyContinue
#testing -ReturnRange switch #testing -ReturnRange switch
$returnedRange = Write-Output -1 668 34 777 860 -0.5 119 -0.1 234 788 | Export-Excel -NumberFormat '[Blue]$#,##0.00;[Red]-$#,##0.00' -Path $path -ReturnRange $returnedRange = Write-Output -1 668 34 777 860 -0.5 119 -0.1 234 788 | Export-Excel -NumberFormat '[Blue]$#,##0.00;[Red]-$#,##0.00' -Path $path -ReturnRange
it "Created a new file and returned the expected range " { it "Created a new file and returned the expected range " {
Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true
$returnedRange | should be "A1:A10" $returnedRange | should be "A1:A10"
} }
$Excel = Open-ExcelPackage -Path $path $Excel = Open-ExcelPackage -Path $path
it "Created 1 worksheet " { it "Created 1 worksheet " {
$Excel.Workbook.Worksheets.count | should be 1 $Excel.Workbook.Worksheets.count | should be 1
} }
$ws = $Excel.Workbook.Worksheets[1] $ws = $Excel.Workbook.Worksheets[1]
it "Created the worksheet with the expected name, number of rows and number of columns " { it "Created the worksheet with the expected name, number of rows and number of columns " {
$ws.Name | should be "sheet1" $ws.Name | should be "sheet1"
$ws.Dimension.Columns | should be 1 $ws.Dimension.Columns | should be 1
$ws.Dimension.Rows | should be 10 $ws.Dimension.Rows | should be 10
} }
it "Set the default style for the sheet as expected " { it "Set the default style for the sheet as expected " {
$ws.cells.Style.Numberformat.Format | should be '[Blue]$#,##0.00;[Red]-$#,##0.00' $ws.cells.Style.Numberformat.Format | should be '[Blue]$#,##0.00;[Red]-$#,##0.00'
} }
it "Set the default style and value for Cell A1 as expected " { it "Set the default style and value for Cell A1 as expected " {
$ws.cells[1,1].Style.Numberformat.Format | should be '[Blue]$#,##0.00;[Red]-$#,##0.00' $ws.cells[1,1].Style.Numberformat.Format | should be '[Blue]$#,##0.00;[Red]-$#,##0.00'
$ws.cells[1,1].Value | should be -1 $ws.cells[1,1].Value | should be -1
} }
} }
Context "#Examples 3 & 4 # Setting cells for different data types Also added test for URI type" { Context "#Examples 3 & 4 # Setting cells for different data types Also added test for URI type" {
$path = "$env:TEMP\Test.xlsx" $path = "$env:TEMP\Test.xlsx"
Remove-item -Path $path -ErrorAction SilentlyContinue Remove-item -Path $path -ErrorAction SilentlyContinue
[PSCustOmobject][Ordered]@{ [PSCustOmobject][Ordered]@{
Date = Get-Date Date = Get-Date
Formula1 = '=SUM(F2:G2)' Formula1 = '=SUM(F2:G2)'
@@ -165,28 +165,28 @@ Describe ExportExcel {
Link = [uri]"https://github.com/dfinke/ImportExcel" Link = [uri]"https://github.com/dfinke/ImportExcel"
} | Export-Excel -NoNumberConversion IPAddress, Number1 -Path $path } | Export-Excel -NoNumberConversion IPAddress, Number1 -Path $path
it "Created a new file " { it "Created a new file " {
Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true
} }
$Excel = Open-ExcelPackage -Path $path $Excel = Open-ExcelPackage -Path $path
it "Created 1 worksheet " { it "Created 1 worksheet " {
$Excel.Workbook.Worksheets.count | should be 1 $Excel.Workbook.Worksheets.count | should be 1
} }
$ws = $Excel.Workbook.Worksheets[1] $ws = $Excel.Workbook.Worksheets[1]
it "Created the worksheet with the expected name, number of rows and number of columns " { it "Created the worksheet with the expected name, number of rows and number of columns " {
$ws.Name | should be "sheet1" $ws.Name | should be "sheet1"
$ws.Dimension.Columns | should be 14 $ws.Dimension.Columns | should be 14
$ws.Dimension.Rows | should be 2 $ws.Dimension.Rows | should be 2
} }
it "Set a date in Cell A2 " { it "Set a date in Cell A2 " {
$ws.Cells[2,1].Value.Gettype().name | should be 'DateTime' $ws.Cells[2,1].Value.Gettype().name | should be 'DateTime'
} }
it "Set a formula in Cell B2 " { it "Set a formula in Cell B2 " {
$ws.Cells[2,2].Formula | should be '=SUM(F2:G2)' $ws.Cells[2,2].Formula | should be '=SUM(F2:G2)'
} }
it "Set strings in Cells E2 and F2 " { it "Set strings in Cells E2 and F2 " {
$ws.Cells[2,5].Value.GetType().name | should be 'String' $ws.Cells[2,5].Value.GetType().name | should be 'String'
@@ -195,18 +195,18 @@ Describe ExportExcel {
it "Set a number in Cell I2 " { it "Set a number in Cell I2 " {
($ws.Cells[2,9].Value -is [valuetype] ) | should be $true ($ws.Cells[2,9].Value -is [valuetype] ) | should be $true
} }
it "Set a hyperlink in Cell N2 " { it "Set a hyperlink in Cell N2 " {
$ws.Cells[2,14].Hyperlink | should be "https://github.com/dfinke/ImportExcel" $ws.Cells[2,14].Hyperlink | should be "https://github.com/dfinke/ImportExcel"
} }
} }
Context "# # Setting cells for different data types with -noHeader" { Context "# # Setting cells for different data types with -noHeader" {
$path = "$env:TEMP\Test.xlsx" $path = "$env:TEMP\Test.xlsx"
Remove-item -Path $path -ErrorAction SilentlyContinue Remove-item -Path $path -ErrorAction SilentlyContinue
[PSCustOmobject][Ordered]@{ [PSCustOmobject][Ordered]@{
Date = Get-Date Date = Get-Date
Formula1 = '=SUM(F1:G1)' Formula1 = '=SUM(F1:G1)'
@@ -224,28 +224,28 @@ Describe ExportExcel {
Link = [uri]"https://github.com/dfinke/ImportExcel" Link = [uri]"https://github.com/dfinke/ImportExcel"
} | Export-Excel -NoNumberConversion IPAddress, Number1 -Path $path -NoHeader } | Export-Excel -NoNumberConversion IPAddress, Number1 -Path $path -NoHeader
it "Created a new file " { it "Created a new file " {
Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true
} }
$Excel = Open-ExcelPackage -Path $path $Excel = Open-ExcelPackage -Path $path
it "Created 1 worksheet " { it "Created 1 worksheet " {
$Excel.Workbook.Worksheets.count | should be 1 $Excel.Workbook.Worksheets.count | should be 1
} }
$ws = $Excel.Workbook.Worksheets[1] $ws = $Excel.Workbook.Worksheets[1]
it "Created the worksheet with the expected name, number of rows and number of columns " { it "Created the worksheet with the expected name, number of rows and number of columns " {
$ws.Name | should be "sheet1" $ws.Name | should be "sheet1"
$ws.Dimension.Columns | should be 14 $ws.Dimension.Columns | should be 14
$ws.Dimension.Rows | should be 1 $ws.Dimension.Rows | should be 1
} }
it "Set a date in Cell A1 " { it "Set a date in Cell A1 " {
$ws.Cells[1,1].Value.Gettype().name | should be 'DateTime' $ws.Cells[1,1].Value.Gettype().name | should be 'DateTime'
} }
it "Set a formula in Cell B1 " { it "Set a formula in Cell B1 " {
$ws.Cells[1,2].Formula | should be '=SUM(F1:G1)' $ws.Cells[1,2].Formula | should be '=SUM(F1:G1)'
} }
it "Set strings in Cells E1 and F1 " { it "Set strings in Cells E1 and F1 " {
$ws.Cells[1,5].Value.GetType().name | should be 'String' $ws.Cells[1,5].Value.GetType().name | should be 'String'
@@ -254,58 +254,58 @@ Describe ExportExcel {
it "Set a number in Cell I1 " { it "Set a number in Cell I1 " {
($ws.Cells[1,9].Value -is [valuetype] ) | should be $true ($ws.Cells[1,9].Value -is [valuetype] ) | should be $true
} }
it "Set a hyperlink in Cell N1 " { it "Set a hyperlink in Cell N1 " {
$ws.Cells[1,14].Hyperlink | should be "https://github.com/dfinke/ImportExcel" $ws.Cells[1,14].Hyperlink | should be "https://github.com/dfinke/ImportExcel"
} }
} }
Context "#Example 5 # Adding a single conditional format " { Context "#Example 5 # Adding a single conditional format " {
### TODO New-ConditionalText doesn't a lot of options in Add-ConditionalFormat. ### TODO New-ConditionalText doesn't a lot of options in Add-ConditionalFormat.
# It would be good to pull the logic out of Export-Excel and have EE call Add-ConditionalFormat. # It would be good to pull the logic out of Export-Excel and have EE call Add-ConditionalFormat.
$ct = New-ConditionalText -ConditionalType GreaterThan 525 -ConditionalTextColor DarkRed -BackgroundColor LightPink $ct = New-ConditionalText -ConditionalType GreaterThan 525 -ConditionalTextColor DarkRed -BackgroundColor LightPink
it "Created a Conditional format description " { it "Created a Conditional format description " {
$ct.BackgroundColor -is [System.Drawing.Color] | should be $true $ct.BackgroundColor -is [System.Drawing.Color] | should be $true
$ct.ConditionalTextColor -is [System.Drawing.Color] | should be $true $ct.ConditionalTextColor -is [System.Drawing.Color] | should be $true
$ct.ConditionalType -in [enum]::GetNames( [OfficeOpenXml.ConditionalFormatting.eExcelConditionalFormattingRuleType] ) | $ct.ConditionalType -in [enum]::GetNames( [OfficeOpenXml.ConditionalFormatting.eExcelConditionalFormattingRuleType] ) |
should be $true should be $true
} }
$path = "$env:TEMP\Test.xlsx" $path = "$env:TEMP\Test.xlsx"
Remove-item -Path $path -ErrorAction SilentlyContinue Remove-item -Path $path -ErrorAction SilentlyContinue
Write-Output 489 668 299 777 860 151 119 497 234 788 | Export-Excel -Path $path -ConditionalText $ct Write-Output 489 668 299 777 860 151 119 497 234 788 | Export-Excel -Path $path -ConditionalText $ct
it "Created a new file " { it "Created a new file " {
Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true Test-Path -Path $path -ErrorAction SilentlyContinue | should be $true
} }
#ToDo need to test applying conitional formatting to a pre-existing worksheet #ToDo need to test applying conitional formatting to a pre-existing worksheet
$Excel = Open-ExcelPackage -Path $path $Excel = Open-ExcelPackage -Path $path
$ws = $Excel.Workbook.Worksheets[1] $ws = $Excel.Workbook.Worksheets[1]
it "Added one block of conditional formating for the data range " { it "Added one block of conditional formating for the data range " {
$ws.ConditionalFormatting.Count | should be 1 $ws.ConditionalFormatting.Count | should be 1
$ws.ConditionalFormatting[0].Address | should be ($ws.Dimension.Address) $ws.ConditionalFormatting[0].Address | should be ($ws.Dimension.Address)
} }
$cf = $ws.ConditionalFormatting[0] $cf = $ws.ConditionalFormatting[0]
it "Set the conditional formatting properties correctly " { it "Set the conditional formatting properties correctly " {
$cf.Formula | should be $ct.Text $cf.Formula | should be $ct.Text
$cf.Type.ToString() | should be $ct.ConditionalType $cf.Type.ToString() | should be $ct.ConditionalType
#$cf.Style.Fill.BackgroundColor | should be $ct.BackgroundColor #$cf.Style.Fill.BackgroundColor | should be $ct.BackgroundColor
# $cf.Style.Font.Color | should be $ct.ConditionalTextColor - have to compare r.g.b # $cf.Style.Font.Color | should be $ct.ConditionalTextColor - have to compare r.g.b
} }
} }
Context "#Example 6 # Adding multiple conditional formats using short form syntax. " { Context "#Example 6 # Adding multiple conditional formats using short form syntax. " {
#this is a test of adding more than one conditional block and using the minimal syntax for new-ConditionalText = #this is a test of adding more than one conditional block and using the minimal syntax for new-ConditionalText =
$path = "$env:TEMP\Test.xlsx" $path = "$env:TEMP\Test.xlsx"
Remove-item -Path $path -ErrorAction SilentlyContinue Remove-item -Path $path -ErrorAction SilentlyContinue
#Testing -Passthrough #Testing -Passthrough
$Excel = Get-Service | Select-Object Name, Status, DisplayName, ServiceName | $Excel = Get-Service | Select-Object Name, Status, DisplayName, ServiceName |
Export-Excel $path -PassThru -ConditionalText $( Export-Excel $path -PassThru -ConditionalText $(
New-ConditionalText Stop DarkRed LightPink New-ConditionalText Stop DarkRed LightPink
@@ -314,15 +314,15 @@ Describe ExportExcel {
$ws = $Excel.Workbook.Worksheets[1] $ws = $Excel.Workbook.Worksheets[1]
it "Added two blocks of conditional formating for the data range " { it "Added two blocks of conditional formating for the data range " {
$ws.ConditionalFormatting.Count | should be 2 $ws.ConditionalFormatting.Count | should be 2
$ws.ConditionalFormatting[0].Address | should be ($ws.Dimension.Address) $ws.ConditionalFormatting[0].Address | should be ($ws.Dimension.Address)
$ws.ConditionalFormatting[1].Address | should be ($ws.Dimension.Address) $ws.ConditionalFormatting[1].Address | should be ($ws.Dimension.Address)
} }
it "Set the conditional formatting properties correctly " { it "Set the conditional formatting properties correctly " {
$ws.ConditionalFormatting[0].Text | should be "Stop" $ws.ConditionalFormatting[0].Text | should be "Stop"
$ws.ConditionalFormatting[1].Text | should be "Running" $ws.ConditionalFormatting[1].Text | should be "Running"
$ws.ConditionalFormatting[0].Type | should be "ContainsText" $ws.ConditionalFormatting[0].Type | should be "ContainsText"
$ws.ConditionalFormatting[1].Type | should be "ContainsText" $ws.ConditionalFormatting[1].Type | should be "ContainsText"
#Add RGB Comparison #Add RGB Comparison
} }
Close-ExcelPackage -ExcelPackage $Excel Close-ExcelPackage -ExcelPackage $Excel
} }
@@ -349,7 +349,7 @@ Describe ExportExcel {
} }
$Array = $Obj1, $Obj2, $Obj3 $Array = $Obj1, $Obj2, $Obj3
$newarray = $Array | Update-FirstObjectProperties $newarray = $Array | Update-FirstObjectProperties
it "Outputs as many objects as it input " { it "Outputs as many objects as it input " {
$newarray.Count | should be $Array.Count $newarray.Count | should be $Array.Count
} }
@@ -359,66 +359,66 @@ Describe ExportExcel {
$newarray[0].Member2 | should be 'Second' $newarray[0].Member2 | should be 'Second'
$newarray[0].Member3 | should beNullOrEmpty $newarray[0].Member3 | should beNullOrEmpty
$newarray[0].Member4 | should beNullOrEmpty $newarray[0].Member4 | should beNullOrEmpty
} }
} }
Context "#Examples 8 & 9 # Adding Pivot tables and charts from parameters" { Context "#Examples 8 & 9 # Adding Pivot tables and charts from parameters" {
$path = "$env:TEMP\Test.xlsx" $path = "$env:TEMP\Test.xlsx"
#This time we are not deleting the XLSX file so this should create a new, named, sheet. #This time we are not deleting the XLSX file so this should create a new, named, sheet.
$Excel = Get-Process | Select-Object -first 50 -Property Name,cpu,pm,handles,company | Export-Excel $path -WorkSheetname Processes -PassThru $Excel = Get-Process | Select-Object -first 50 -Property Name,cpu,pm,handles,company | Export-Excel $path -WorkSheetname Processes -PassThru
#Testing -passthru and adding the Pivot as a second step. Want to save and re-open it ... #Testing -passthru and adding the Pivot as a second step. Want to save and re-open it ...
Export-Excel -ExcelPackage $Excel -WorkSheetname Processes -IncludePivotTable -PivotRows Company -PivotData PM Export-Excel -ExcelPackage $Excel -WorkSheetname Processes -IncludePivotTable -PivotRows Company -PivotData PM
$Excel = Open-ExcelPackage $path $Excel = Open-ExcelPackage $path
$PTws = $Excel.Workbook.Worksheets["ProcessesPivotTable"] $PTws = $Excel.Workbook.Worksheets["ProcessesPivotTable"]
$wCount = $Excel.Workbook.Worksheets.Count $wCount = $Excel.Workbook.Worksheets.Count
it "Added the named sheet and pivot table to the workbook " { it "Added the named sheet and pivot table to the workbook " {
$PTws | should not beNullOrEmpty $PTws | should not beNullOrEmpty
$PTws.PivotTables.Count | should be 1 $PTws.PivotTables.Count | should be 1
$Excel.Workbook.Worksheets["Processes"] | should not beNullOrEmpty $Excel.Workbook.Worksheets["Processes"] | should not beNullOrEmpty
$Excel.Workbook.Worksheets.Count | should beGreaterThan 2 $Excel.Workbook.Worksheets.Count | should beGreaterThan 2
$excel.Workbook.Worksheets["Processes"].Dimension.rows | should be 51 #50 data + 1 header $excel.Workbook.Worksheets["Processes"].Dimension.rows | should be 51 #50 data + 1 header
} }
$pt = $PTws.PivotTables[0] $pt = $PTws.PivotTables[0]
it "Built the expected Pivot table " { it "Built the expected Pivot table " {
$pt.RowFields.Count | should be 1 $pt.RowFields.Count | should be 1
$pt.RowFields[0].Name | should be "Company" $pt.RowFields[0].Name | should be "Company"
$pt.DataFields.Count | should be 1 $pt.DataFields.Count | should be 1
$pt.DataFields[0].Function | should be "Count" $pt.DataFields[0].Function | should be "Count"
$pt.DataFields[0].Field.Name | should be "PM" $pt.DataFields[0].Field.Name | should be "PM"
$PTws.Drawings.Count | should be 0 $PTws.Drawings.Count | should be 0
} }
#using the already open sheet add the pivot chart #using the already open sheet add the pivot chart
$warnvar = $null $warnvar = $null
Export-Excel -ExcelPackage $Excel -WorkSheetname Processes -IncludePivotTable -PivotRows Company -PivotData PM -IncludePivotChart -ChartType PieExploded3D -WarningAction SilentlyContinue -WarningVariable warnvar Export-Excel -ExcelPackage $Excel -WorkSheetname Processes -IncludePivotTable -PivotRows Company -PivotData PM -IncludePivotChart -ChartType PieExploded3D -WarningAction SilentlyContinue -WarningVariable warnvar
$Excel = Open-ExcelPackage $path $Excel = Open-ExcelPackage $path
it "Added a chart to the pivot table without rebuilding " { it "Added a chart to the pivot table without rebuilding " {
$ws = $Excel.Workbook.Worksheets["ProcessesPivotTable"] $ws = $Excel.Workbook.Worksheets["ProcessesPivotTable"]
$Excel.Workbook.Worksheets.Count | should be $wCount $Excel.Workbook.Worksheets.Count | should be $wCount
$ws.Drawings.count | should be 1 $ws.Drawings.count | should be 1
$ws.Drawings[0].ChartType.ToString() | should be "PieExploded3D" $ws.Drawings[0].ChartType.ToString() | should be "PieExploded3D"
} }
it "Generated a message on re-processing the Pivot table " { it "Generated a message on re-processing the Pivot table " {
$warnVar | Should not beNullOrEmpty $warnVar | Should not beNullOrEmpty
} }
$warnVar = $null $warnVar = $null
Get-Process | Select-Object -Last 50 -Property Name,cpu,pm,handles,company | Export-Excel $path -WorkSheetname Processes -Append -IncludePivotTable -PivotRows Company -PivotData PM -IncludePivotChart -ChartType PieExploded3D -WarningAction SilentlyContinue -WarningVariable warnvar Get-Process | Select-Object -Last 50 -Property Name,cpu,pm,handles,company | Export-Excel $path -WorkSheetname Processes -Append -IncludePivotTable -PivotRows Company -PivotData PM -IncludePivotChart -ChartType PieExploded3D -WarningAction SilentlyContinue -WarningVariable warnvar
$Excel = Open-ExcelPackage $path $Excel = Open-ExcelPackage $path
$pt = $Excel.Workbook.Worksheets["ProcessesPivotTable"].PivotTables[0] $pt = $Excel.Workbook.Worksheets["ProcessesPivotTable"].PivotTables[0]
it "Appended to the Worksheet and Extended the Pivot table " { it "Appended to the Worksheet and Extended the Pivot table " {
$Excel.Workbook.Worksheets.Count | should be $wCount $Excel.Workbook.Worksheets.Count | should be $wCount
$excel.Workbook.Worksheets["Processes"].Dimension.rows | should be 101 #appended 50 rows to the previous total $excel.Workbook.Worksheets["Processes"].Dimension.rows | should be 101 #appended 50 rows to the previous total
$pt.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | $pt.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref |
should be "A1:E101" should be "A1:E101"
} }
it "Generated a message on extending the Pivot table " { it "Generated a message on extending the Pivot table " {
$warnVar | Should not beNullOrEmpty $warnVar | Should not beNullOrEmpty
} }
} }
Context " # Add-Worksheet inserted sheets, moved them correctly, and copied a sheet" { Context " # Add-Worksheet inserted sheets, moved them correctly, and copied a sheet" {
$path = "$env:TEMP\Test.xlsx" $path = "$env:TEMP\Test.xlsx"
$Excel = Open-ExcelPackage $path $Excel = Open-ExcelPackage $path
#At this point Sheets should be in the order Sheet1, Processes, ProcessesPivotTable #At this point Sheets should be in the order Sheet1, Processes, ProcessesPivotTable
$null = Add-WorkSheet -ExcelPackage $Excel -WorkSheetname "Processes" -MoveToEnd # order now Sheet1, ProcessesPivotTable, Processes $null = Add-WorkSheet -ExcelPackage $Excel -WorkSheetname "Processes" -MoveToEnd # order now Sheet1, ProcessesPivotTable, Processes
@@ -426,12 +426,12 @@ Describe ExportExcel {
$null = Add-WorkSheet -ExcelPackage $Excel -WorkSheetname "Sheet1" -MoveAfter "*" # Now its NewSheet, ProcessesPivotTable, Processes, Sheet1 $null = Add-WorkSheet -ExcelPackage $Excel -WorkSheetname "Sheet1" -MoveAfter "*" # Now its NewSheet, ProcessesPivotTable, Processes, Sheet1
$null = Add-WorkSheet -ExcelPackage $Excel -WorkSheetname "Another" -MoveToStart # Now its Another, NewSheet, ProcessesPivotTable, Processes, Sheet1 $null = Add-WorkSheet -ExcelPackage $Excel -WorkSheetname "Another" -MoveToStart # Now its Another, NewSheet, ProcessesPivotTable, Processes, Sheet1
$null = Add-WorkSheet -ExcelPackage $Excel -WorkSheetname "OneLast" -MoveBefore "ProcessesPivotTable" # Now its Another, NewSheet, Onelast, ProcessesPivotTable, Processes, Sheet1 $null = Add-WorkSheet -ExcelPackage $Excel -WorkSheetname "OneLast" -MoveBefore "ProcessesPivotTable" # Now its Another, NewSheet, Onelast, ProcessesPivotTable, Processes, Sheet1
Close-ExcelPackage $Excel Close-ExcelPackage $Excel
$Excel = Open-ExcelPackage $path $Excel = Open-ExcelPackage $path
it "Got the Sheets in the right order " { it "Got the Sheets in the right order " {
$excel.Workbook.Worksheets[1].Name | should be "Another" $excel.Workbook.Worksheets[1].Name | should be "Another"
$excel.Workbook.Worksheets[2].Name | should be "NewSheet" $excel.Workbook.Worksheets[2].Name | should be "NewSheet"
$excel.Workbook.Worksheets[3].Name | should be "Onelast" $excel.Workbook.Worksheets[3].Name | should be "Onelast"
$excel.Workbook.Worksheets[4].Name | should be "ProcessesPivotTable" $excel.Workbook.Worksheets[4].Name | should be "ProcessesPivotTable"
@@ -447,14 +447,14 @@ Describe ExportExcel {
$newWs.ConditionalFormatting[0].Formula | should be ($excel.Workbook.Worksheets["Sheet1"].ConditionalFormatting[0].Formula) $newWs.ConditionalFormatting[0].Formula | should be ($excel.Workbook.Worksheets["Sheet1"].ConditionalFormatting[0].Formula)
} }
} }
Context " # Create and append with Start row and Start Column, inc ranges and Pivot table" { Context " # Create and append with Start row and Start Column, inc ranges and Pivot table" {
$path = "$env:TEMP\Test.xlsx" $path = "$env:TEMP\Test.xlsx"
#Catch warning #Catch warning
$warnVar = $null $warnVar = $null
#Test Append with no existing sheet. Test adding a named pivot table from a command line parameter #Test Append with no existing sheet. Test adding a named pivot table from a command line parameter
get-process | Select-Object -first 10 -Property Name,cpu,pm,handles,company | export-excel -StartRow 3 -StartColumn 3 -AutoFilter -AutoNameRange -BoldTopRow -IncludePivotTable -PivotRows Company -PivotData PM -PivotTableName 'PTOffset' -Path $path -WorkSheetname withOffset -append get-process | Select-Object -first 10 -Property Name,cpu,pm,handles,company | export-excel -StartRow 3 -StartColumn 3 -AutoFilter -AutoNameRange -BoldTopRow -IncludePivotTable -PivotRows Company -PivotData PM -PivotTableName 'PTOffset' -Path $path -WorkSheetname withOffset -append
get-process | Select-Object -last 10 -Property Name,cpu,pm,handles,company | export-excel -StartRow 3 -StartColumn 3 -AutoFilter -AutoNameRange -BoldTopRow -IncludePivotTable -PivotRows Company -PivotData PM -PivotTableName 'PTOffset' -Path $path -WorkSheetname withOffset -append -WarningAction SilentlyContinue -WarningVariable warnvar get-process | Select-Object -last 10 -Property Name,cpu,pm,handles,company | export-excel -StartRow 3 -StartColumn 3 -AutoFilter -AutoNameRange -BoldTopRow -IncludePivotTable -PivotRows Company -PivotData PM -PivotTableName 'PTOffset' -Path $path -WorkSheetname withOffset -append -WarningAction SilentlyContinue -WarningVariable warnvar
$Excel = Open-ExcelPackage $path $Excel = Open-ExcelPackage $path
$dataWs = $Excel.Workbook.Worksheets["withOffset"] $dataWs = $Excel.Workbook.Worksheets["withOffset"]
@@ -464,12 +464,12 @@ Describe ExportExcel {
$dataWs.Cells[2,2].Value | Should beNullOrEmpty $dataWs.Cells[2,2].Value | Should beNullOrEmpty
$dataWs.Cells[3,3].Value | Should not beNullOrEmpty $dataWs.Cells[3,3].Value | Should not beNullOrEmpty
$dataWs.Cells[3,3].Style.Font.Bold | Should be $true $dataWs.Cells[3,3].Style.Font.Bold | Should be $true
$dataWs.Dimension.End.Row | Should be 23 $dataWs.Dimension.End.Row | Should be 23
$dataWs.names[0].end.row | Should be 23 $dataWs.names[0].end.row | Should be 23
$dataWs.names[0].name | Should be 'Name' $dataWs.names[0].name | Should be 'Name'
$dataWs.names.Count | Should be 6 $dataWs.names.Count | Should be 6
$dataWs.cells[$dataws.Dimension].AutoFilter | Should be true $dataWs.cells[$dataws.Dimension].AutoFilter | Should be true
$pt.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | $pt.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref |
Should be "C3:G23" Should be "C3:G23"
} }
it "Generated a message on extending the Pivot table " { it "Generated a message on extending the Pivot table " {
@@ -478,36 +478,36 @@ Describe ExportExcel {
} }
Context "#Example 11 # Create and append with title, inc ranges and Pivot table" { Context "#Example 11 # Create and append with title, inc ranges and Pivot table" {
$path = "$env:TEMP\Test.xlsx" $path = "$env:TEMP\Test.xlsx"
$ptDef = [ordered]@{} $ptDef = [ordered]@{}
$ptDef += New-PivotTableDefinition -PivotTableName "PT1" -SourceWorkSheet 'Sheet1' -PivotRows "Status" -PivotData @{'Status' = 'Count'} -PivotFilter "StartType" -IncludePivotChart -ChartType BarClustered3D -ChartTitle "Services by status" -ChartHeight 512 -ChartWidth 768 -ChartRow 10 -ChartColumn 0 -NoLegend $ptDef += New-PivotTableDefinition -PivotTableName "PT1" -SourceWorkSheet 'Sheet1' -PivotRows "Status" -PivotData @{'Status' = 'Count'} -PivotFilter "StartType" -IncludePivotChart -ChartType BarClustered3D -ChartTitle "Services by status" -ChartHeight 512 -ChartWidth 768 -ChartRow 10 -ChartColumn 0 -NoLegend
$ptDef += New-PivotTableDefinition -PivotTableName "PT2" -SourceWorkSheet 'Sheet2' -PivotRows "Company" -PivotData @{'Company' = 'Count'} -IncludePivotChart -ChartType PieExploded3D -ShowPercent -WarningAction SilentlyContinue $ptDef += New-PivotTableDefinition -PivotTableName "PT2" -SourceWorkSheet 'Sheet2' -PivotRows "Company" -PivotData @{'Company' = 'Count'} -IncludePivotChart -ChartType PieExploded3D -ShowPercent -WarningAction SilentlyContinue
it "Built a pivot definition using New-PivotTableDefinition " { it "Built a pivot definition using New-PivotTableDefinition " {
$ptDef.PT1.SourceWorkSheet | Should be 'Sheet1' $ptDef.PT1.SourceWorkSheet | Should be 'Sheet1'
$ptDef.PT1.PivotRows | Should be 'Status' $ptDef.PT1.PivotRows | Should be 'Status'
$ptDef.PT1.PivotData.Status | Should be 'Count' $ptDef.PT1.PivotData.Status | Should be 'Count'
$ptDef.PT1.PivotFilter | Should be 'StartType' $ptDef.PT1.PivotFilter | Should be 'StartType'
$ptDef.PT1.IncludePivotChart | Should be $true $ptDef.PT1.IncludePivotChart | Should be $true
$ptDef.PT1.ChartType.tostring() | Should be 'BarClustered3D' $ptDef.PT1.ChartType.tostring() | Should be 'BarClustered3D'
} }
Remove-Item -Path $path Remove-Item -Path $path
#Catch warning #Catch warning
$warnvar = $null $warnvar = $null
Get-Service | Select-Object -Property Status, Name, DisplayName, StartType | Export-Excel -Path $path -AutoSize -TableName "All Services" -TableStyle Medium1 -WarningAction SilentlyContinue -WarningVariable warnvar Get-Service | Select-Object -Property Status, Name, DisplayName, StartType | Export-Excel -Path $path -AutoSize -TableName "All Services" -TableStyle Medium1 -WarningAction SilentlyContinue -WarningVariable warnvar
Get-Process | Select-Object -Property Name, Company, Handles, CPU, VM | Export-Excel -Path $path -AutoSize -WorkSheetname 'sheet2' -TableName "Processes" -TableStyle Light1 -Title "Processes" -TitleFillPattern Solid -TitleBackgroundColor AliceBlue -TitleBold -TitleSize 22 -PivotTableDefinition $ptDef Get-Process | Select-Object -Property Name, Company, Handles, CPU, VM | Export-Excel -Path $path -AutoSize -WorkSheetname 'sheet2' -TableName "Processes" -TableStyle Light1 -Title "Processes" -TitleFillPattern Solid -TitleBackgroundColor AliceBlue -TitleBold -TitleSize 22 -PivotTableDefinition $ptDef
$Excel = Open-ExcelPackage $path $Excel = Open-ExcelPackage $path
$ws1 = $Excel.Workbook.Worksheets["Sheet1"] $ws1 = $Excel.Workbook.Worksheets["Sheet1"]
$ws2 = $Excel.Workbook.Worksheets["Sheet2"] $ws2 = $Excel.Workbook.Worksheets["Sheet2"]
it "Set Column widths (with autosize) " { it "Set Column widths (with autosize) " {
$ws1.Column(2).Width | Should not be $ws1.DefaultColWidth $ws1.Column(2).Width | Should not be $ws1.DefaultColWidth
$ws2.Column(1).width | Should not be $ws2.DefaultColWidth $ws2.Column(1).width | Should not be $ws2.DefaultColWidth
} }
it "Added tables to both sheets (handling illegal chars) and a title in sheet 2 " { it "Added tables to both sheets (handling illegal chars) and a title in sheet 2 " {
$warnvar.count | Should be 1 $warnvar.count | Should be 1
$ws1.tables.Count | Should be 1 $ws1.tables.Count | Should be 1
$ws2.tables.Count | Should be 1 $ws2.tables.Count | Should be 1
$ws1.Tables[0].Address.Start.Row | Should be 1 $ws1.Tables[0].Address.Start.Row | Should be 1
@@ -515,46 +515,46 @@ Describe ExportExcel {
$ws1.Tables[0].Address.End.Address | Should be $ws1.Dimension.End.Address $ws1.Tables[0].Address.End.Address | Should be $ws1.Dimension.End.Address
$ws2.Tables[0].Address.End.Address | Should be $ws2.Dimension.End.Address $ws2.Tables[0].Address.End.Address | Should be $ws2.Dimension.End.Address
$ws2.Tables[0].Name | Should be "Processes" $ws2.Tables[0].Name | Should be "Processes"
$ws2.Tables[0].StyleName | Should be "TableStyleLight1" $ws2.Tables[0].StyleName | Should be "TableStyleLight1"
$ws2.Cells["A1"].Value | Should be "Processes" $ws2.Cells["A1"].Value | Should be "Processes"
$ws2.Cells["A1"].Style.Font.Bold | Should be $true $ws2.Cells["A1"].Style.Font.Bold | Should be $true
$ws2.Cells["A1"].Style.Font.Size | Should be 22 $ws2.Cells["A1"].Style.Font.Size | Should be 22
$ws2.Cells["A1"].Style.Fill.PatternType.tostring() | Should be "solid" $ws2.Cells["A1"].Style.Fill.PatternType.tostring() | Should be "solid"
$ws2.Cells["A1"].Style.Fill.BackgroundColor.Rgb | Should be "fff0f8ff" $ws2.Cells["A1"].Style.Fill.BackgroundColor.Rgb | Should be "fff0f8ff"
} }
$ptsheet1 = $Excel.Workbook.Worksheets["Pt1"] $ptsheet1 = $Excel.Workbook.Worksheets["Pt1"]
$ptsheet2 = $Excel.Workbook.Worksheets["Pt2"] $ptsheet2 = $Excel.Workbook.Worksheets["Pt2"]
$PT1 = $ptsheet1.PivotTables[0] $PT1 = $ptsheet1.PivotTables[0]
$PT2 = $ptsheet2.PivotTables[0] $PT2 = $ptsheet2.PivotTables[0]
$PC1 = $ptsheet1.Drawings[0] $PC1 = $ptsheet1.Drawings[0]
$PC2 = $ptsheet2.Drawings[0] $PC2 = $ptsheet2.Drawings[0]
it "Created the correct pivot tables and charts from the definitions. " { it "Created the correct pivot tables and charts from the definitions. " {
$PT1.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | $PT1.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref |
Should be ("A1:" + $ws1.Dimension.End.Address) Should be ("A1:" + $ws1.Dimension.End.Address)
$PT2.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | $PT2.CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref |
Should be ("A2:" + $ws2.Dimension.End.Address) #Title in row 1 Should be ("A2:" + $ws2.Dimension.End.Address) #Title in row 1
$pt1.PageFields[0].Name | Should be 'StartType' $pt1.PageFields[0].Name | Should be 'StartType'
$pt1.RowFields[0].Name | Should be 'Status' $pt1.RowFields[0].Name | Should be 'Status'
$pt1.DataFields[0].Field.name | Should be 'Status' $pt1.DataFields[0].Field.name | Should be 'Status'
$pt1.DataFields[0].Function | Should be 'Count' $pt1.DataFields[0].Function | Should be 'Count'
$pc1.ChartType | Should be 'BarClustered3D' $pc1.ChartType | Should be 'BarClustered3D'
$pc1.From.Column | Should be 0 #chart 1 at 0,10 chart 2 at 4,0 (default) $pc1.From.Column | Should be 0 #chart 1 at 0,10 chart 2 at 4,0 (default)
$pc2.From.Column | Should be 4 $pc2.From.Column | Should be 4
$pc1.From.Row | Should be 10 $pc1.From.Row | Should be 10
$pc2.From.Row | Should be 0 $pc2.From.Row | Should be 0
$pc1.Legend.Font | should beNullOrEmpty #Best check for legend removed. $pc1.Legend.Font | should beNullOrEmpty #Best check for legend removed.
$pc2.Legend.Font | should not beNullOrEmpty $pc2.Legend.Font | should not beNullOrEmpty
$pc1.Title.Text | Should be 'Services by status' $pc1.Title.Text | Should be 'Services by status'
$pc2.DataLabel.ShowPercent | should be $true $pc2.DataLabel.ShowPercent | should be $true
} }
} }
Context "#Example 13 # Formatting and another way to do a pivot. " { Context "#Example 13 # Formatting and another way to do a pivot. " {
$path = "$env:TEMP\Test.xlsx" $path = "$env:TEMP\Test.xlsx"
Remove-Item $path Remove-Item $path
$excel = Get-Process | Select-Object -Property Name,Company,Handles,CPU,PM,NPM,WS | Export-Excel -Path $path -ClearSheet -WorkSheetname "Processes" -FreezeTopRowFirstColumn -PassThru $excel = Get-Process | Select-Object -Property Name,Company,Handles,CPU,PM,NPM,WS | Export-Excel -Path $path -ClearSheet -WorkSheetname "Processes" -FreezeTopRowFirstColumn -PassThru
$sheet = $excel.Workbook.Worksheets["Processes"] $sheet = $excel.Workbook.Worksheets["Processes"]
$sheet.Column(1) | Set-Format -Bold -AutoFit $sheet.Column(1) | Set-Format -Bold -AutoFit
@@ -566,55 +566,55 @@ Describe ExportExcel {
Add-ConditionalFormatting -WorkSheet $sheet -Range "D2:D1048576" -DataBarColor Red Add-ConditionalFormatting -WorkSheet $sheet -Range "D2:D1048576" -DataBarColor Red
Add-ConditionalFormatting -WorkSheet $sheet -Range "G2:G1048576" -RuleType GreaterThan -ConditionValue "104857600" -ForeGroundColor Red Add-ConditionalFormatting -WorkSheet $sheet -Range "G2:G1048576" -RuleType GreaterThan -ConditionValue "104857600" -ForeGroundColor Red
foreach ($c in 5..9) {Set-Format $sheet.Column($c) -AutoFit } foreach ($c in 5..9) {Set-Format $sheet.Column($c) -AutoFit }
Add-PivotTable -PivotTableName "PT_Procs" -ExcelPackage $excel -SourceWorkSheet "Processes" -PivotRows Company -PivotData @{'Name'='Count'} -IncludePivotChart -ChartType ColumnClustered -NoLegend Add-PivotTable -PivotTableName "PT_Procs" -ExcelPackage $excel -SourceWorkSheet "Processes" -PivotRows Company -PivotData @{'Name'='Count'} -IncludePivotChart -ChartType ColumnClustered -NoLegend
Close-ExcelPackage $excel Close-ExcelPackage $excel
$excel = Open-ExcelPackage $path $excel = Open-ExcelPackage $path
$sheet = $excel.Workbook.Worksheets["Processes"] $sheet = $excel.Workbook.Worksheets["Processes"]
it "Applied the formating" { it "Applied the formating" {
$sheet | should not beNullOrEmpty $sheet | should not beNullOrEmpty
$sheet.Column(1).wdith | should not be $sheet.DefaultColWidth $sheet.Column(1).wdith | should not be $sheet.DefaultColWidth
$sheet.Column(7).wdith | should not be $sheet.DefaultColWidth $sheet.Column(7).wdith | should not be $sheet.DefaultColWidth
$sheet.Column(1).style.font.bold | should be $true $sheet.Column(1).style.font.bold | should be $true
$sheet.Column(2).style.wraptext | should be $true $sheet.Column(2).style.wraptext | should be $true
$sheet.Column(2).width | should be 29 $sheet.Column(2).width | should be 29
$sheet.Column(3).style.horizontalalignment | should be 'right' $sheet.Column(3).style.horizontalalignment | should be 'right'
$sheet.Column(4).style.horizontalalignment | should be 'right' $sheet.Column(4).style.horizontalalignment | should be 'right'
$sheet.Cells["A1"].Style.HorizontalAlignment | should be 'Center' $sheet.Cells["A1"].Style.HorizontalAlignment | should be 'Center'
$sheet.Cells['E2'].Style.HorizontalAlignment | should be 'right' $sheet.Cells['E2'].Style.HorizontalAlignment | should be 'right'
$sheet.Cells['A1'].Style.Font.Bold | should be $true $sheet.Cells['A1'].Style.Font.Bold | should be $true
$sheet.Cells['D2'].Style.Font.Bold | should be $true $sheet.Cells['D2'].Style.Font.Bold | should be $true
$sheet.Cells['E2'].style.numberformat.format | should be '#,###' $sheet.Cells['E2'].style.numberformat.format | should be '#,###'
$sheet.Column(3).style.numberformat.format | should be '#,###' $sheet.Column(3).style.numberformat.format | should be '#,###'
$sheet.Column(4).style.numberformat.format | should be '#,##0.0' $sheet.Column(4).style.numberformat.format | should be '#,##0.0'
$sheet.ConditionalFormatting.Count | should be 2 $sheet.ConditionalFormatting.Count | should be 2
$sheet.ConditionalFormatting[0].type | should be 'Databar' $sheet.ConditionalFormatting[0].type | should be 'Databar'
$sheet.ConditionalFormatting[0].Color.name | should be 'ffff0000' $sheet.ConditionalFormatting[0].Color.name | should be 'ffff0000'
$sheet.ConditionalFormatting[0].Address.Address | should be 'D2:D1048576' $sheet.ConditionalFormatting[0].Address.Address | should be 'D2:D1048576'
$sheet.ConditionalFormatting[1].type | should be 'GreaterThan' $sheet.ConditionalFormatting[1].type | should be 'GreaterThan'
$sheet.ConditionalFormatting[1].Formula | should be '104857600' $sheet.ConditionalFormatting[1].Formula | should be '104857600'
$sheet.ConditionalFormatting[1].Style.Font.Color.Color.Name | should be 'ffff0000' $sheet.ConditionalFormatting[1].Style.Font.Color.Color.Name | should be 'ffff0000'
} }
it "Froze the panes" { it "Froze the panes" {
$sheet.view.Panes.Count | should be 3 $sheet.view.Panes.Count | should be 3
} }
$ptsheet1 = $Excel.Workbook.Worksheets["Pt_procs"] $ptsheet1 = $Excel.Workbook.Worksheets["Pt_procs"]
it "Created the pivot table" { it "Created the pivot table" {
$ptsheet1 | should not beNullOrEmpty $ptsheet1 | should not beNullOrEmpty
$ptsheet1.PivotTables[0].DataFields[0].Field.Name | should be "Name" $ptsheet1.PivotTables[0].DataFields[0].Field.Name | should be "Name"
$ptsheet1.PivotTables[0].DataFields[0].Function | should be "Count" $ptsheet1.PivotTables[0].DataFields[0].Function | should be "Count"
$ptsheet1.PivotTables[0].RowFields[0].Name | should be "Company" $ptsheet1.PivotTables[0].RowFields[0].Name | should be "Company"
$ptsheet1.PivotTables[0].CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref | $ptsheet1.PivotTables[0].CacheDefinition.CacheDefinitionXml.pivotCacheDefinition.cacheSource.worksheetSource.ref |
Should be $sheet.Dimension.address Should be $sheet.Dimension.address
} }
} }
## To do ## To do
## More pivot options & other FreezePanes settings ? ## More pivot options & other FreezePanes settings ?
## Charts ## Charts
## Style script block ## Style script block
## Rezip ? ## Rezip ?
} }

Binary file not shown.

View File

@@ -0,0 +1,32 @@
Import-Module $PSScriptRoot\..\..\ImportExcel.psd1
Describe "Tests" {
BeforeAll {
$data = $null
$timer = Measure-Command {
$data = Import-Excel $PSScriptRoot\Simple.xlsx
}
}
It "Should have two items" {
$data.count | Should be 2
}
It "Should have items a and b" {
$data[0].p1 | Should be "a"
$data[1].p1 | Should be "b"
}
It "Should read fast < 1000 milliseconds" {
$timer.TotalMilliseconds | should BeLessThan 1000
}
It "Should read larger xlsx, 4k rows 1 col < 2000 milliseconds" {
$timer = Measure-Command {
$null = Import-Excel $PSScriptRoot\LargerFile.xlsx
}
$timer.TotalMilliseconds | should BeLessThan 2000
}
}

Binary file not shown.

BIN
appveyor.yml Normal file

Binary file not shown.

BIN
dashboard.xlsx Normal file

Binary file not shown.

BIN
test1.xlsx Normal file

Binary file not shown.

BIN
testTable.xlsx Normal file

Binary file not shown.