unhide support in Export-Excel, return range in Set-Row/Column

This commit is contained in:
jhoneill
2018-07-26 15:40:00 +01:00
parent 5082d2120d
commit af13cfa1d0
7 changed files with 57 additions and 29 deletions

View File

@@ -23,7 +23,7 @@ if (Get-Command -Name register-argumentCompleter -ErrorAction SilentlyContinue)
Register-ArgumentCompleter -CommandName Merge-MulipleSheets -ParameterName ChangeBackgroundColor -ScriptBlock $Function:ColorCompletion Register-ArgumentCompleter -CommandName Merge-MulipleSheets -ParameterName ChangeBackgroundColor -ScriptBlock $Function:ColorCompletion
Register-ArgumentCompleter -CommandName Merge-MulipleSheets ` -ParameterName DeleteBackgroundColor -ScriptBlock $Function:ColorCompletion Register-ArgumentCompleter -CommandName Merge-MulipleSheets ` -ParameterName DeleteBackgroundColor -ScriptBlock $Function:ColorCompletion
Register-ArgumentCompleter -CommandName Merge-MulipleSheets -ParameterName KeyFontColor -ScriptBlock $Function:ColorCompletion Register-ArgumentCompleter -CommandName Merge-MulipleSheets -ParameterName KeyFontColor -ScriptBlock $Function:ColorCompletion
Register-ArgumentCompleter -CommandName New-ConditionalText -ParameterName PatternColor -ScriptBlock $Function:ColorCompletion Register-ArgumentCompleter -CommandName New-ConditionalText -ParameterName PatternColor -ScriptBlock $Function:ColorCompletion
Register-ArgumentCompleter -CommandName New-ConditionalText -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion Register-ArgumentCompleter -CommandName New-ConditionalText -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion
Register-ArgumentCompleter -CommandName Set-Format -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion Register-ArgumentCompleter -CommandName Set-Format -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion
Register-ArgumentCompleter -CommandName Set-Format -ParameterName FontColor -ScriptBlock $Function:ColorCompletion Register-ArgumentCompleter -CommandName Set-Format -ParameterName FontColor -ScriptBlock $Function:ColorCompletion

View File

@@ -88,7 +88,9 @@
.PARAMETER ExcelChartDefinition .PARAMETER ExcelChartDefinition
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, supports wildcards. If all sheets would be hidden, the sheet being worked on will be revealed .
.PARAMETER UnHideSheet
Name(s) of Sheet(s) to Reveal in the workbook, supports wildcards.
.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.
@@ -425,6 +427,7 @@
[Switch]$ColumnChart , [Switch]$ColumnChart ,
[Object[]]$ExcelChartDefinition, [Object[]]$ExcelChartDefinition,
[String[]]$HideSheet, [String[]]$HideSheet,
[String[]]$UnHideSheet,
[Switch]$MoveToStart, [Switch]$MoveToStart,
[Switch]$MoveToEnd, [Switch]$MoveToEnd,
$MoveBefore , $MoveBefore ,
@@ -766,7 +769,7 @@
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
@@ -846,11 +849,26 @@
foreach ($Sheet in $HideSheet) { foreach ($Sheet in $HideSheet) {
try { try {
$pkg.Workbook.WorkSheets[$Sheet].Hidden = 'Hidden' $pkg.Workbook.WorkSheets.Where({$_.Name -like $sheet}) | ForEach-Object {
Write-verbose -Message "Sheet '$sheet' Hidden." $_.Hidden = 'Hidden'
Write-verbose -Message "Sheet '$($_.Name)' Hidden."
}
} }
catch {Write-Warning -Message "Failed hiding worksheet '$sheet': $_"} catch {Write-Warning -Message "Failed hiding worksheet '$sheet': $_"}
} }
foreach ($Sheet in $UnHideSheet) {
try {
$pkg.Workbook.WorkSheets.Where({$_.Name -like $sheet}) | ForEach-Object {
$_.Hidden = 'Visible'
Write-verbose -Message "Sheet '$($_.Name)' shown"
}
}
catch {Write-Warning -Message "Failed showing worksheet '$sheet': $_"}
}
if (-not $pkg.Workbook.Worksheets.Where({$_.Hidden -eq 'visible'})) {
Write-Verbose -Message "No Sheets were left visible, making $WorkSheetname visible"
$ws.Hidden = 'Visible'
}
foreach ($chartDef in $ExcelChartDefinition) { foreach ($chartDef in $ExcelChartDefinition) {
$params = @{} $params = @{}
@@ -1115,10 +1133,10 @@ function Add-WorkSheet {
} }
function Add-PivotTable { function Add-PivotTable {
<# <#
.Synopsis .Synopsis
Adds a Pivot table (and optional pivot chart) to a workbook Adds a Pivot table (and optional pivot chart) to a workbook
.Description .Description
If the pivot table already exists, the source data will be updated. If the pivot table already exists, the source data will be updated.
#> #>
param ( param (
#Name for the new Pivot table - this will be the name of a sheet in the workbook #Name for the new Pivot table - this will be the name of a sheet in the workbook
@@ -1178,6 +1196,9 @@ function Add-PivotTable {
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]
} }
elseif ($SourceWorkSheet -is [int]) {
$SourceWorkSheet = $ExcelPackage.Workbook.Worksheets[$SourceWorkSheet]
}
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"}

View File

@@ -46,11 +46,13 @@ iex (new-object System.Net.WebClient).DownloadString('https://raw.github.com/dfi
# New to 25th July # New to 25th July
- Added parameter completer to Add-ConditionalFormatting/PatternColor New-ConditionalText PatternColor&BackgroundColor - Added color parameter completer to a few places where it was missing (Add-ConditionalFormatting/PatternColor New-ConditionalText PatternColor&BackgroundColor)
- Changed charting.ps1 and examples\charts\*.ps1 to use New-ExcelChartDefinition instead of New-ExcelChart - Changed charting.ps1 and examples\charts\*.ps1 to use New-ExcelChartDefinition instead of New-ExcelChart
- Quick charts in Export-excel were too wide (now 800 pixels instead of 1200), and now support show percent, ShowCategory and NoLegend Parameters - Quick charts in Export-excel were too wide (now 800 pixels instead of 1200), and now support ShowPercent, ShowCategory and NoLegend Parameters
- Fixed bug in Add-ExcelChart where XAxisPosition and YAxisPostion would not be set correctly - Fixed bug in Add-ExcelChart where XAxisPosition and YAxisPostion would not be set correctly
- Fixed bug in Set-Format where enums with a value of zero, or zero numbers would not be set; added functionality to set-format to support -bold:$false -italic:$false etc. (see #400) - Fixed bug in Set-Format where enums with a value of zero, or zero numbers would not be set; added functionality to set-format to support -bold:$false -italic:$false etc. (see #400)
- Changed HideSheet in Export-Excel to support wildcards, and added UnhideSheet.
- Added returnRange support to set-Column and Set-row
- Added tests for better coverage (now at >80% average - set-row/colum set-format less than 80%) , and tweaked some tests to use few rows and/or columns for speed - Added tests for better coverage (now at >80% average - set-row/colum set-format less than 80%) , and tweaked some tests to use few rows and/or columns for speed
# What's new to 18th July 18 # What's new to 18th July 18

View File

@@ -79,6 +79,8 @@
[float]$Width, [float]$Width,
#Set the inserted data to be a named range (ignored if header is not specified) #Set the inserted data to be a named range (ignored if header is not specified)
[Switch]$AutoNameRange, [Switch]$AutoNameRange,
#If Sepecified returns the range of cells which affected
[switch]$ReturnRange,
#If Specified, return an ExcelPackage object to allow further work to be done on the file. #If Specified, return an ExcelPackage object to allow further work to be done on the file.
[switch]$PassThru [switch]$PassThru
) )
@@ -119,10 +121,12 @@
'BorderAround', 'BackgroundColor', 'BackgroundPattern', 'PatternColor')) { 'BorderAround', 'BackgroundColor', 'BackgroundPattern', 'PatternColor')) {
if ($PSBoundParameters.ContainsKey($p)) {$params[$p] = $PSBoundParameters[$p]} if ($PSBoundParameters.ContainsKey($p)) {$params[$p] = $PSBoundParameters[$p]}
} }
$theRange = "$ColumnName$startRow`:$ColumnName$endRow"
if ($params.Count) { if ($params.Count) {
Set-Format -WorkSheet $Worksheet -Range "$ColumnName$startRow`:$ColumnName$endRow" @params Set-Format -WorkSheet $Worksheet -Range $theRange @params
} }
#endregion #endregion
#return the new data if -passthru was specified. #return the new data if -passthru was specified.
if ($passThru) { $Worksheet.Column( $Column)} if ($passThru) { $Worksheet.Column( $Column)}
elseif ($ReturnRange) { $theRange}
} }

View File

@@ -78,6 +78,8 @@
[int]$TextRotation , [int]$TextRotation ,
#Set cells to a fixed hieght #Set cells to a fixed hieght
[float]$Height, [float]$Height,
#If Sepecified returns the range of cells which affected
[switch]$ReturnRange,
#If Specified, return a row object to allow further work to be done #If Specified, return a row object to allow further work to be done
[switch]$PassThru [switch]$PassThru
) )
@@ -92,7 +94,6 @@
$endColumn = $Worksheet.Dimension.End.Column $endColumn = $Worksheet.Dimension.End.Column
$endRow = $Worksheet.Dimension.End.Row $endRow = $Worksheet.Dimension.End.Row
if ($Row -lt 2 ) {$Row = $endRow + 1 } if ($Row -lt 2 ) {$Row = $endRow + 1 }
Write-Verbose -Message "Updating Row $Row" Write-Verbose -Message "Updating Row $Row"
#Add a row label #Add a row label
if ($Heading) { if ($Heading) {
@@ -120,11 +121,12 @@
'BorderAround', 'BackgroundColor', 'BackgroundPattern', 'PatternColor')) { 'BorderAround', 'BackgroundColor', 'BackgroundPattern', 'PatternColor')) {
if ($PSBoundParameters.ContainsKey($p)) {$params[$p] = $PSBoundParameters[$p]} if ($PSBoundParameters.ContainsKey($p)) {$params[$p] = $PSBoundParameters[$p]}
} }
$theRange = [OfficeOpenXml.ExcelAddress]::TranslateFromR1C1("R[$Row]C[$StartColumn]:R[$Row]C[$EndColumn]",0,0)
if ($params.Count) { if ($params.Count) {
$theRange = [OfficeOpenXml.ExcelAddress]::TranslateFromR1C1("R[$Row]C[$StartColumn]:R[$Row]C[$EndColumn]",0,0)
Set-Format -WorkSheet $Worksheet -Range $theRange @params Set-Format -WorkSheet $Worksheet -Range $theRange @params
} }
#endregion #endregion
#return the new data if -passthru was specified. #return the new data if -passthru was specified.
if ($passThru) {$Worksheet.Row($Row)} if ($passThru) {$Worksheet.Row($Row)}
elseif ($ReturnRange) {$theRange}
} }

11
ToDo.md
View File

@@ -1,12 +1,11 @@
- [ ] Create an autocomplete for WorkSheetName param on ImportExcel - [ ] Create an autocomplete for WorkSheetName param on ImportExcel and for number format ; where number format exists Translate "Date", "DateTime", "Currency"
- [ ] Add help text for parmaters which don't have it ( PivotDataToColumn , NoClobber and CellStyleSB ) in Export Excel, copy to Send-SQLDataToExcel - [ ] Add help text for parmaters which don't have it ( PivotDataToColumn , NoClobber and CellStyleSB ) in Export Excel, copy to Send-SQLDataToExcel
- [ ] Support "make worksheet active"
- [ ] Add checks for valid worksheet names (also check pivot names, range names and table names are valid) - [ ] Add checks for valid worksheet names (also check pivot names, range names and table names are valid)
- [ ] Investigate regional support for number conversion & possible date conversion - [ ] Investigate regional support for number conversion & possible date conversion
- [ ] Add help in ConvertToExcelXLSx.ps1, Get-HTMLTable.ps1, GetRange.PS1, GetExcelTable.Ps1, Import-HTML.PS1, New-ConditionalFormattingIconSet.Ps1, NewConditionalText.PS1, New-Psitem.PS1, Remove-Worksheet.ps1 and Add-ExcelChart - Copy parameter help from function Add-ExcelChart into New-ExcelChart.ps1 - [ ] Add help in ConvertToExcelXLSx.ps1, Get-HTMLTable.ps1, GetRange.PS1, GetExcelTable.Ps1, Import-HTML.PS1, New-ConditionalFormattingIconSet.Ps1, NewConditionalText.PS1, New-Psitem.PS1, Remove-Worksheet.ps1 and Add-ExcelChart - Copy parameter help from function Add-ExcelChart into New-ExcelChart.ps1
- [ ] Examples and tests for new "Quick charts" in Export Excel (replace examples) that use Charting.ps1, GetXYRange.ps1, InferData.PS1 (move these to deprecated). - [ ] Examples and tests for new "Quick charts" in Export Excel (replace examples) that use Charting.ps1, GetXYRange.ps1, InferData.PS1 (move these to deprecated).
- [ ] Add conditional format and return range support to Set-Row and Set-Column to use set-format and - [ ] Test Add PivotTable selecting source sheet by position , UnhideSheet, and Wildcard support for hideSheet
- [ ] Examples for set-Row and Set-column (e.g. from tests) - [ ] Test return range support to Set-Row and Set-Column and add examples to examples for set-Row and Set-column set-format, add-Conditional format (e.g. from tests)
- [ ] Add PivotTable can't select sheet by position.
- [ ] Increase code covereage for import-excel and Set-Format - [ ] Increase code covereage for import-excel and Set-Format
- [ ] Wildcard support for hide worksheet, add show-worksheet, make worksheet active in Export-Excel

View File

@@ -610,7 +610,7 @@ Describe ExportExcel {
$excel = Open-ExcelPackage $path $excel = Open-ExcelPackage $path
$sheet = $excel.Workbook.Worksheets["Processes"] $sheet = $excel.Workbook.Worksheets["Processes"]
it "Returned the rule when calling Add-ConditionalFormatting -passthur " { it "Returned the rule when calling Add-ConditionalFormatting -passthru " {
$rule | should not beNullOrEmpty $rule | should not beNullOrEmpty
$rule.getType().fullname | should be "OfficeOpenXml.ConditionalFormatting.ExcelConditionalFormattingTopPercent" $rule.getType().fullname | should be "OfficeOpenXml.ConditionalFormatting.ExcelConditionalFormattingTopPercent"
$rule.Style.Font.Strike | should be true $rule.Style.Font.Strike | should be true
@@ -631,15 +631,15 @@ Describe ExportExcel {
$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 3
$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].Style.Font.Strike | Should be $true
$sheet.ConditionalFormatting[1].Formula | Should be '104857600' $sheet.ConditionalFormatting[1].type | Should be "TopPercent"
$sheet.ConditionalFormatting[1].Style.Font.Color.Color.Name | Should be 'ffff0000' $sheet.ConditionalFormatting[2].type | Should be 'GreaterThan'
$sheet.ConditionalFormatting[2].Style.Font.Strike | Should be $true $sheet.ConditionalFormatting[2].Formula | Should be '104857600'
$sheet.ConditionalFormatting[2].type | Should be "TopPercent" $sheet.ConditionalFormatting[2].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