From 065fc2f1ad2944c48492c1b378df3b2768cc8aa8 Mon Sep 17 00:00:00 2001 From: jhoneill Date: Thu, 31 Oct 2019 15:48:49 +0000 Subject: [PATCH] Better handling of linux autosize issue. --- Export-Excel.ps1 | 5 +-- Merge-Worksheet.ps1 | 3 +- SetFormat.ps1 | 3 +- __tests__/Export-Excel.Tests.ps1 | 34 +++++++++++++------ __tests__/First10Races.tests.ps1 | 1 + .../Set-Row_Set-Column-SetFormat.tests.ps1 | 8 +++-- 6 files changed, 37 insertions(+), 17 deletions(-) diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index 1aad858..7f46f92 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -76,7 +76,7 @@ .PARAMETER ConditionalText Applies a Conditional formatting rule defined with New-ConditionalText. When specific conditions are met the format is applied. .PARAMETER NoNumberConversion - By default we convert all values to numbers if possible, but this isn't always desirable. NoNumberConversion allows you to add exceptions for the conversion. Wildcards (like '*') are allowed. + By default we convert all values to numbers if possible, but this isn't always desirable. NoNumberConversion allows you to add exceptions for the conversion. The only Wildcard allowed is * for all properties .PARAMETER BoldTopRow Makes the top row boldface. .PARAMETER NoHeader @@ -909,7 +909,7 @@ } catch {Write-Warning -Message "Failed setting the top row to bold in worksheet '$WorksheetName': $_"} } - if ($AutoSize) { + if ($AutoSize -and ([environment]::OSVersion.Platform -like "win*" -or $env:AUTOSIZE)) { try { #Don't fit the all the columns in the sheet; if we are adding cells beside things with hidden columns, that unhides them if ($MaxAutoSizeRows -and $MaxAutoSizeRows -lt $LastRow ) { @@ -921,6 +921,7 @@ } catch { Write-Warning -Message "Failed autosizing columns of worksheet '$WorksheetName': $_"} } + elseif ($AutoSize) {Write-Warning -Message "Auto-fitting columns is not available with this OS configuration." } foreach ($Sheet in $HideSheet) { try { diff --git a/Merge-Worksheet.ps1 b/Merge-Worksheet.ps1 index 01ae37e..2ddbb44 100644 --- a/Merge-Worksheet.ps1 +++ b/Merge-Worksheet.ps1 @@ -512,7 +512,8 @@ Function Merge-MultipleSheets { Add-ConditionalFormatting @condFormattingParams -ConditionValue ("AND(" +(($sameChecks -join ",") -replace '<>"Same"','="Changed"') +")" ) -BackgroundColor $ChangeBackgroundColor } #We've made a bunch of things wider so now is the time to autofit columns. Any hiding has to come AFTER this, because it unhides things - $sheet.Cells.AutoFitColumns() + if ([environment]::OSVersion.Platform -notlike "win*" -and -not $env:AUTOSIZE) {Write-Warning "Autofit is not available with this OS configuration."} + else {$sheet.Cells.AutoFitColumns()} #if we have a key field (we didn't concatenate all fields) use what we built up in $sameChecks to apply conditional formatting to it (Row no will be in column A, Key in Column B) if ($Key -ne '*') { diff --git a/SetFormat.ps1 b/SetFormat.ps1 index f59d50a..131de0c 100644 --- a/SetFormat.ps1 +++ b/SetFormat.ps1 @@ -229,7 +229,7 @@ } else {Write-Warning -Message ("Can set the height of a row or a range but not a {0} object" -f ($Range.GetType().name)) } } - if ($Autosize) { + if ($Autosize -and ([environment]::OSVersion.Platform -like "win*" -or $env:AUTOSIZE)) { try { if ($Range -is [OfficeOpenXml.ExcelColumn]) {$Range.AutoFit() } elseif ($Range -is [OfficeOpenXml.ExcelRange] ) { @@ -240,6 +240,7 @@ } catch {Write-Warning -Message "Failed autosizing columns of worksheet '$WorksheetName': $_"} } + elseif ($AutoSize) {Write-Warning -Message "Auto-fitting columns is not available with this OS configuration." } elseif ($PSBoundParameters.ContainsKey('Width')) { if ($Range -is [OfficeOpenXml.ExcelColumn]) {$Range.Width = $Width} elseif ($Range -is [OfficeOpenXml.ExcelRange] ) { diff --git a/__tests__/Export-Excel.Tests.ps1 b/__tests__/Export-Excel.Tests.ps1 index aae2da7..424284f 100644 --- a/__tests__/Export-Excel.Tests.ps1 +++ b/__tests__/Export-Excel.Tests.ps1 @@ -3,6 +3,8 @@ if (-not (get-command Import-Excel -ErrorAction SilentlyContinue)) { Import-Module $PSScriptRoot\..\ImportExcel.psd1 } +if ($null -eq $IsWindows) {$IsWindows = [environment]::OSVersion.Platform -like "win*"} +$WarningAction = "SilentlyContinue" Describe ExportExcel { . "$PSScriptRoot\Samples\Samples.ps1" if (Get-process -Name Excel,xlim -ErrorAction SilentlyContinue) { @@ -655,20 +657,21 @@ Describe ExportExcel { #Catch warning $warnvar = $null #Test create two data pages; as part of adding the second give both their own pivot table, test -autosize switch - Get-Service | Select-Object -Property Status, Name, DisplayName, StartType, CanPauseAndContinue | Export-Excel -Path $path -AutoSize -TableName "All Services" -TableStyle Medium1 -WarningAction SilentlyContinue -WarningVariable warnvar + Get-Service | Select-Object -Property Status, Name, DisplayName, StartType, CanPauseAndContinue | Export-Excel -Path $path -AutoSize -TableName "All Services" -TableStyle Medium1 -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 ([System.Drawing.Color]::AliceBlue) -TitleBold -TitleSize 22 -PivotTableDefinition $ptDef $Excel = Open-ExcelPackage $path $ws1 = $Excel.Workbook.Worksheets["Sheet1"] $ws2 = $Excel.Workbook.Worksheets["Sheet2"] - - it "Set Column widths (with autosize) " { + if ($isWindows) { + it "Set Column widths (with autosize) " { $ws1.Column(2).Width | Should not be $ws1.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 " { - $warnvar.count | Should be 1 + $warnvar.count | Should beGreaterThan 0 $ws1.tables.Count | Should be 1 $ws2.tables.Count | Should be 1 $ws1.Tables[0].Address.Start.Row | Should be 1 @@ -723,7 +726,8 @@ Describe ExportExcel { #Test freezing top row/first column, adding formats and a pivot table - from Add-Pivot table not a specification variable - after the export $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.Column(1) | Set-ExcelRange -Bold -AutoFit + if ($isWindows) {$sheet.Column(1) | Set-ExcelRange -Bold -AutoFit } + else {$sheet.Column(1) | Set-ExcelRange -Bold } $sheet.Column(2) | Set-ExcelRange -Width 29 -WrapText $sheet.Column(3) | Set-ExcelRange -HorizontalAlignment Right -NFormat "#,###" Set-ExcelRange -Address $sheet.Cells["E1:H1048576"] -HorizontalAlignment Right -NFormat "#,###" @@ -734,7 +738,7 @@ Describe ExportExcel { $rule = Add-ConditionalFormatting -passthru -Address $sheet.cells["C:C"] -RuleType TopPercent -ConditionValue 20 -Bold -StrikeThru Add-ConditionalFormatting -WorkSheet $sheet -Range "G2:G1048576" -RuleType GreaterThan -ConditionValue "104857600" -ForeGroundColor ([System.Drawing.Color]::Red) -Bold -Italic -Underline -BackgroundColor ([System.Drawing.Color]::Beige) -BackgroundPattern LightUp -PatternColor ([System.Drawing.Color]::Gray) #Test Set-ExcelRange with a column - foreach ($c in 5..9) {Set-ExcelRange $sheet.Column($c) -AutoFit } + if ($isWindows) { foreach ($c in 5..9) {Set-ExcelRange $sheet.Column($c) -AutoFit } } Add-PivotTable -PivotTableName "PT_Procs" -ExcelPackage $excel -SourceWorkSheet 1 -PivotRows Company -PivotData @{'Name' = 'Count'} -IncludePivotChart -ChartType ColumnClustered -NoLegend Export-Excel -ExcelPackage $excel -WorksheetName "Processes" -AutoNameRange #Test adding named ranges seperately from adding data. @@ -747,8 +751,10 @@ Describe ExportExcel { } it "Applied the formating " { $sheet | Should not beNullOrEmpty - $sheet.Column(1).wdith | Should not be $sheet.DefaultColWidth - $sheet.Column(7).wdith | Should not be $sheet.DefaultColWidth + if ($isWindows) { + $sheet.Column(1).width | Should not be $sheet.DefaultColWidth + $sheet.Column(7).width | Should not be $sheet.DefaultColWidth + } $sheet.Column(1).style.font.bold | Should be $true $sheet.Column(2).style.wraptext | Should be $true $sheet.Column(2).width | Should be 29 @@ -1036,7 +1042,9 @@ Describe ExportExcel { $ExcelPackage.File | Should BeLike ([IO.Path]::GetTempPath() + '*') $Worksheet.Tables[0].Name | Should Be 'Table1' $Worksheet.AutoFilterAddress | Should BeNullOrEmpty - $Worksheet.Column(5).Width | Should BeGreaterThan 9.5 + if ($isWindows) { + $Worksheet.Column(5).Width | Should BeGreaterThan 9.5 + } } it "Now allows override of Path and TableName".PadRight(87) { $ExcelPackage = $Processes | Export-Excel -Now -PassThru -Path $Path -TableName:$false @@ -1045,7 +1053,9 @@ Describe ExportExcel { $ExcelPackage.File | Should Be $Path $Worksheet.Tables | Should BeNullOrEmpty $Worksheet.AutoFilterAddress | Should BeNullOrEmpty - $Worksheet.Column(5).Width | Should BeGreaterThan 9.5 + if ($isWindows) { + $Worksheet.Column(5).Width | Should BeGreaterThan 9.5 + } } <# Mock looks unreliable need to check Mock -CommandName 'Invoke-Item' @@ -1072,7 +1082,9 @@ Describe ExportExcel { $Worksheet.Tables[0].Name | Should Be 'Data' $Worksheet.AutoFilterAddress | Should BeNullOrEmpty - $Worksheet.Column(5).Width | Should BeGreaterThan 9.5 + if ($isWindows) { + $Worksheet.Column(5).Width | Should BeGreaterThan 9.5 + } } } } diff --git a/__tests__/First10Races.tests.ps1 b/__tests__/First10Races.tests.ps1 index cb4902d..77d8a6f 100644 --- a/__tests__/First10Races.tests.ps1 +++ b/__tests__/First10Races.tests.ps1 @@ -1,5 +1,6 @@ $scriptPath = Split-Path -Path $MyInvocation.MyCommand.path -Parent $dataPath = Join-Path -Path $scriptPath -ChildPath "First10Races.csv" +$WarningAction = "SilentlyContinue" Describe "Creating small named ranges with hyperlinks" { BeforeAll { diff --git a/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 b/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 index f2d71c7..93a3bd0 100644 --- a/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 +++ b/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 @@ -1,3 +1,5 @@ +if ($null -eq $IsWindows) {$IsWindows = [environment]::OSVersion.Platform -like "win*"} + $path = "TestDrive:\test.xlsx" $data = ConvertFrom-Csv -InputObject @" @@ -142,8 +144,10 @@ Describe "Set-ExcelColumn, Set-ExcelRow and Set-ExcelRange" { Set-ExcelRange -WorkSheet $ws -Range "E1" -ResetFont -HorizontalAlignment General -FontName "Courier New" -fontSize 9 Set-ExcelRange -Address $ws.Cells["E7"] -ResetFont -WrapText -BackgroundColor ([System.Drawing.Color]::AliceBlue) -BackgroundPattern DarkTrellis -PatternColor ([System.Drawing.Color]::Red) -NumberFormat "£#,###.00" Set-ExcelRange -Address $ws.Column(1) -Width 0 - Set-ExcelRange -Address $ws.Column(2) -AutoFit - Set-ExcelRange -Address $ws.Cells["E:E"] -AutoFit + if ($IsWindows) { + Set-ExcelRange -Address $ws.Column(2) -AutoFit + Set-ExcelRange -Address $ws.Cells["E:E"] -AutoFit + } #Test alias Set-Format -Address $ws.row(5) -Height 0 $rr = $r.row