From 2d228948584ef3db77a8fd21de7b02392de1a653 Mon Sep 17 00:00:00 2001 From: jhoneill Date: Thu, 26 Jul 2018 15:23:07 +0100 Subject: [PATCH] unhide support in Export-Excel, return range in Set-Row/Column --- ColorCompletion.ps1 | 2 +- Export-Excel.ps1 | 37 +++++++++++++++++++++++++------- README.md | 8 ++++--- Set-Column.ps1 | 6 +++++- Set-Row.ps1 | 8 ++++--- ToDo.md | 11 +++++----- __tests__/Export-Excel.Tests.ps1 | 4 ++-- 7 files changed, 52 insertions(+), 24 deletions(-) diff --git a/ColorCompletion.ps1 b/ColorCompletion.ps1 index ac55254..22ee985 100644 --- a/ColorCompletion.ps1 +++ b/ColorCompletion.ps1 @@ -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 DeleteBackgroundColor -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 Set-Format -ParameterName BackgroundColor -ScriptBlock $Function:ColorCompletion Register-ArgumentCompleter -CommandName Set-Format -ParameterName FontColor -ScriptBlock $Function:ColorCompletion diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index a03c650..e8d7659 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -88,7 +88,9 @@ .PARAMETER ExcelChartDefinition A hash table containing ChartType, Title, NoLegend, ShowCategory, ShowPercent, Yrange, Xrange and SeriesHeader for one or more [non-pivot] charts. .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 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. @@ -425,6 +427,7 @@ [Switch]$ColumnChart , [Object[]]$ExcelChartDefinition, [String[]]$HideSheet, + [String[]]$UnHideSheet, [Switch]$MoveToStart, [Switch]$MoveToEnd, $MoveBefore , @@ -766,7 +769,7 @@ if ($params.keys -notcontains "SourceRange" -and ($params.Keys -notcontains "SourceWorkSheet" -or $params.SourceWorkSheet -eq $WorkSheetname)) {$params.SourceRange = $dataRange} if ($params.Keys -notcontains "SourceWorkSheet") {$params.SourceWorkSheet = $ws } - if ($params.Keys -notcontains "NoTotalsInPivot" -and $NoTotalsInPivot ) {$params.NoTotalsInPivot = $true} + if ($params.Keys -notcontains "NoTotalsInPivot" -and $NoTotalsInPivot ) {$params.NoTotalsInPivot = $true} if ($params.Keys -notcontains "PivotDataToColumn" -and $PivotDataToColumn) {$params.PivotDataToColumn = $true} Add-PivotTable -ExcelPackage $pkg -PivotTableName $item.key @Params @@ -846,11 +849,26 @@ foreach ($Sheet in $HideSheet) { try { - $pkg.Workbook.WorkSheets[$Sheet].Hidden = 'Hidden' - Write-verbose -Message "Sheet '$sheet' Hidden." + $pkg.Workbook.WorkSheets.Where({$_.Name -like $sheet}) | ForEach-Object { + $_.Hidden = 'Hidden' + Write-verbose -Message "Sheet '$($_.Name)' Hidden." + } } catch {Write-Warning -Message "Failed hiding worksheet '$sheet': $_"} } + foreach ($Sheet in $UnHideSheet) { + try { + $pkg.Workbook.WorkSheets.Where({$_.Name -like $sheet}) | ForEach-Object { + $_.Hidden = 'Visible' + Write-verbose -Message "Sheet '$($_.Name)' shown" + } + } + catch {Write-Warning -Message "Failed showing worksheet '$sheet': $_"} + } + if (-not $pkg.Workbook.Worksheets.Where({$_.Hidden -eq 'visible'})) { + Write-Verbose -Message "No Sheets were left visible, making $WorkSheetname visible" + $ws.Hidden = 'Visible' + } foreach ($chartDef in $ExcelChartDefinition) { $params = @{} @@ -1115,10 +1133,10 @@ function Add-WorkSheet { } function Add-PivotTable { <# - .Synopsis - Adds a Pivot table (and optional pivot chart) to a workbook - .Description - If the pivot table already exists, the source data will be updated. + .Synopsis + Adds a Pivot table (and optional pivot chart) to a workbook + .Description + If the pivot table already exists, the source data will be updated. #> param ( #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]) { $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'." } else { if ($PivotFilter) {$PivotTableStartCell = "A3"} else { $PivotTableStartCell = "A1"} diff --git a/README.md b/README.md index 5fe5f93..53f8d77 100644 --- a/README.md +++ b/README.md @@ -46,11 +46,13 @@ iex (new-object System.Net.WebClient).DownloadString('https://raw.github.com/dfi # 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 -- 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 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 # What's new to 18th July 18 diff --git a/Set-Column.ps1 b/Set-Column.ps1 index 6487246..faed0d4 100644 --- a/Set-Column.ps1 +++ b/Set-Column.ps1 @@ -79,6 +79,8 @@ [float]$Width, #Set the inserted data to be a named range (ignored if header is not specified) [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. [switch]$PassThru ) @@ -119,10 +121,12 @@ 'BorderAround', 'BackgroundColor', 'BackgroundPattern', 'PatternColor')) { if ($PSBoundParameters.ContainsKey($p)) {$params[$p] = $PSBoundParameters[$p]} } + $theRange = "$ColumnName$startRow`:$ColumnName$endRow" if ($params.Count) { - Set-Format -WorkSheet $Worksheet -Range "$ColumnName$startRow`:$ColumnName$endRow" @params + Set-Format -WorkSheet $Worksheet -Range $theRange @params } #endregion #return the new data if -passthru was specified. if ($passThru) { $Worksheet.Column( $Column)} + elseif ($ReturnRange) { $theRange} } \ No newline at end of file diff --git a/Set-Row.ps1 b/Set-Row.ps1 index 1107f06..204e6ce 100644 --- a/Set-Row.ps1 +++ b/Set-Row.ps1 @@ -78,6 +78,8 @@ [int]$TextRotation , #Set cells to a fixed hieght [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 [switch]$PassThru ) @@ -92,7 +94,6 @@ $endColumn = $Worksheet.Dimension.End.Column $endRow = $Worksheet.Dimension.End.Row if ($Row -lt 2 ) {$Row = $endRow + 1 } - Write-Verbose -Message "Updating Row $Row" #Add a row label if ($Heading) { @@ -120,11 +121,12 @@ 'BorderAround', 'BackgroundColor', 'BackgroundPattern', 'PatternColor')) { 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) { - $theRange = [OfficeOpenXml.ExcelAddress]::TranslateFromR1C1("R[$Row]C[$StartColumn]:R[$Row]C[$EndColumn]",0,0) Set-Format -WorkSheet $Worksheet -Range $theRange @params } #endregion #return the new data if -passthru was specified. - if ($passThru) {$Worksheet.Row($Row)} + if ($passThru) {$Worksheet.Row($Row)} + elseif ($ReturnRange) {$theRange} } \ No newline at end of file diff --git a/ToDo.md b/ToDo.md index ded5393..338e45f 100644 --- a/ToDo.md +++ b/ToDo.md @@ -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 +- [ ] Support "make worksheet active" - [ ] 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 - [ ] 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). -- [ ] Add conditional format and return range support to Set-Row and Set-Column to use set-format and -- [ ] Examples for set-Row and Set-column (e.g. from tests) -- [ ] Add PivotTable can't select sheet by position. +- [ ] Test Add PivotTable selecting source sheet by position , UnhideSheet, and Wildcard support for hideSheet +- [ ] 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) - [ ] Increase code covereage for import-excel and Set-Format -- [ ] Wildcard support for hide worksheet, add show-worksheet, make worksheet active in Export-Excel - diff --git a/__tests__/Export-Excel.Tests.ps1 b/__tests__/Export-Excel.Tests.ps1 index cfd021e..f7929d5 100644 --- a/__tests__/Export-Excel.Tests.ps1 +++ b/__tests__/Export-Excel.Tests.ps1 @@ -610,7 +610,7 @@ Describe ExportExcel { $excel = Open-ExcelPackage $path $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.getType().fullname | should be "OfficeOpenXml.ConditionalFormatting.ExcelConditionalFormattingTopPercent" $rule.Style.Font.Strike | should be true @@ -631,7 +631,7 @@ Describe ExportExcel { $sheet.Cells['E2'].style.numberformat.format | Should be '#,###' $sheet.Column(3).style.numberformat.format | Should be '#,###' $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].Color.name | Should be 'ffff0000' $sheet.ConditionalFormatting[0].Address.Address | Should be 'D2:D1048576'