Better handling of autosize (again)

This commit is contained in:
jhoneill
2019-11-01 01:57:04 +00:00
parent 4d17a09537
commit 6b626e8f5f
5 changed files with 9 additions and 13 deletions

View File

@@ -909,7 +909,7 @@
}
catch {Write-Warning -Message "Failed setting the top row to bold in worksheet '$WorksheetName': $_"}
}
if ($AutoSize -and ([environment]::OSVersion.Platform -like "win*" -or $env:AUTOSIZE)) {
if ($AutoSize -and -not $env:NoAutoSize) {
try {
#Don't fit the all the columns in the sheet; if we are adding cells beside things with hidden columns, that unhides them
if ($MaxAutoSizeRows -and $MaxAutoSizeRows -lt $LastRow ) {

View File

@@ -60,25 +60,23 @@ else {
Write-Warning 'PowerShell 5 is required for plot.ps1'
Write-Warning 'PowerShell Excel is ready, except for that functionality'
}
if (($IsLinux -or $IsMacOS) -and -not $env:AUTOSIZE) {
if (($IsLinux -or $IsMacOS) -or $env:NoAutoSize) {
$ExcelPackage = [OfficeOpenXml.ExcelPackage]::new()
$Cells = ($ExcelPackage | Add-WorkSheet).Cells['A1']
$Cells.Value = 'Test'
try {
$Cells.AutoFitColumns()
Write-Warning -Message ('The library needed for Autosize is present but the environment variable for it has not been set' + [environment]::newline +
'Set $env:AUTOSIZE="True"')
if ($env:NoAutoSize) {Remove-Item Env:\NoAutoSize}
}
catch {
$env:NoAutoSize = $true
if ($IsLinux) {
Write-Warning -Message ('ImportExcel Module Cannot Autosize. Please run the following command to install dependencies:' + [environment]::newline +
' "sudo apt-get install -y --no-install-recommends libgdiplus libc6-dev"' +[environment]::newline +
'and then set the environment variable AUTOSIZE to True.')
'"sudo apt-get install -y --no-install-recommends libgdiplus libc6-dev"')
}
if ($IsMacOS) {
Write-Warning -Message ('ImportExcel Module Cannot Autosize. Please run the following command to install dependencies:' + [environment]::newline +
'"brew install mono-libgdiplus"' +[environment]::newline +
'and then set the environment variable AUTOSIZE to True.')
'"brew install mono-libgdiplus"')
}
}
finally {

View File

@@ -512,7 +512,7 @@ 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
if ([environment]::OSVersion.Platform -notlike "win*" -and -not $env:AUTOSIZE) {Write-Warning "Autofit is not available with this OS configuration."}
if ($env:NoAutoSize) {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)

View File

@@ -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 -and ([environment]::OSVersion.Platform -like "win*" -or $env:AUTOSIZE)) {
if ($Autosize -and -not $env:NoAutoSize) {
try {
if ($Range -is [OfficeOpenXml.ExcelColumn]) {$Range.AutoFit() }
elseif ($Range -is [OfficeOpenXml.ExcelRange] ) {

View File

@@ -1,5 +1,3 @@
if ($null -eq $IsWindows) {$IsWindows = [environment]::OSVersion.Platform -like "win*"}
$path = "TestDrive:\test.xlsx"
$data = ConvertFrom-Csv -InputObject @"
@@ -144,7 +142,7 @@ 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
if ($IsWindows) {
if (-not $env:NoAutoSize) {
Set-ExcelRange -Address $ws.Column(2) -AutoFit
Set-ExcelRange -Address $ws.Cells["E:E"] -AutoFit
}