Further refinements to Conditional format, made new test more robust

This commit is contained in:
jhoneill
2018-09-05 16:56:48 +01:00
parent 7edf5f8a3a
commit 378a20a094
4 changed files with 44 additions and 41 deletions

View File

@@ -168,10 +168,11 @@
if ($RuleType -match "Than|Equal|Between" ) {
if ($ConditionValue) {
$number = $Null
#if the condition type is not a value type, but parses as a number, make it the number
if ($ConditionValue -isnot [System.ValueType] -and [Double]::TryParse($ConditionValue, [System.Globalization.NumberStyles]::Any, [System.Globalization.NumberFormatInfo]::CurrentInfo, [Ref]$number) ) {
$ConditionValue = $number
}
elseif (($ConditionValue -notmatch '^=') -and ($ConditionValue -notmatch '^".*"$') ) {
} #else if it is not a value type, or a formula, or wrapped in quotes, wrap it in quotes.
elseif (($ConditionValue -isnot [System.ValueType])-and ($ConditionValue -notmatch '^=') -and ($ConditionValue -notmatch '^".*"$') ) {
$ConditionValue = '"' + $ConditionValue +'"'
}
}
@@ -180,7 +181,7 @@
if ($ConditionValue -isnot [System.ValueType] -and [Double]::TryParse($ConditionValue2, [System.Globalization.NumberStyles]::Any, [System.Globalization.NumberFormatInfo]::CurrentInfo, [Ref]$number) ) {
$ConditionValue2 = $number
}
elseif (($ConditionValue2 -notmatch '^=') -and ($ConditionValue2 -notmatch '^".*"$') ) {
elseif (($ConditionValue -isnot [System.ValueType]) -and ($ConditionValue2 -notmatch '^=') -and ($ConditionValue2 -notmatch '^".*"$') ) {
$ConditionValue2 = '"' + $ConditionValue2 + '"'
}
}

View File

@@ -594,16 +594,6 @@
}
}
catch {throw "Could not get worksheet $worksheetname"}
try {
foreach ($format in $ConditionalFormat ) {
switch ($format.formatter) {
"ThreeIconSet" {Add-ConditionalFormatting -WorkSheet $ws -ThreeIconsSet $format.IconType -range $format.range -reverse:$format.reverse }
"FourIconSet" {Add-ConditionalFormatting -WorkSheet $ws -FourIconsSet $format.IconType -range $format.range -reverse:$format.reverse }
"FiveIconSet" {Add-ConditionalFormatting -WorkSheet $ws -FiveIconsSet $format.IconType -range $format.range -reverse:$format.reverse }
}
}
}
catch {throw "Error applying confitional formatting to worksheet"}
try {
if ($Append -and $ws.Dimension) {
#if there is a title or anything else above the header row, append needs to be combined wih a suitable startrow parameter
@@ -961,20 +951,30 @@
Add-ExcelChart -Worksheet $ws @params
}
}
foreach ($ct in $ConditionalText) {
# It now doesn't matter if the conditional formating rules are passed in $conditionalText or $conditional format.
# Just one with an alias for compatiblity it will break things for people who are using both at once
foreach ($c in (@() + $ConditionalText + $ConditionalFormat) ) {
try {
$cfParams = @{RuleType = $ct.ConditionalType; ConditionValue = $ct.text ;
BackgroundColor = $ct.BackgroundColor; BackgroundPattern = $ct.PatternType ;
ForeGroundColor = $ct.ConditionalTextColor
}
if ($ct.Range) {$cfParams.range = $ct.range} else { $cfParams.Range = $ws.Dimension.Address }
if ($c.ConditionalType) {
$cfParams = @{RuleType = $c.ConditionalType; ConditionValue = $c.Text ;
BackgroundColor = $c.BackgroundColor; BackgroundPattern = $c.PatternType ;
ForeGroundColor = $c.ConditionalTextColor}
if ($c.Range) {$cfParams.Range = $c.Range}
else {$cfParams.Range = $ws.Dimension.Address }
Add-ConditionalFormatting -WorkSheet $ws @cfParams
Write-Verbose -Message "Added conditional formatting to range $($ct.range)"
Write-Verbose -Message "Added conditional formatting to range $($c.range)"
}
catch {Write-Warning -Message "Failed adding conditional formatting to worksheet '$WorksheetName': $_"}
elseif ($c.formatter) {
switch ($c.formatter) {
"ThreeIconSet" {Add-ConditionalFormatting -WorkSheet $ws -ThreeIconsSet $c.IconType -range $c.range -reverse:$c.reverse }
"FourIconSet" {Add-ConditionalFormatting -WorkSheet $ws -FourIconsSet $c.IconType -range $c.range -reverse:$c.reverse }
"FiveIconSet" {Add-ConditionalFormatting -WorkSheet $ws -FiveIconsSet $c.IconType -range $c.range -reverse:$c.reverse }
}
Write-Verbose -Message "Added conditional formatting to range $($c.range)"
}
}
catch {throw "Error applying confitional formatting to worksheet $_"}
}
if ($CellStyleSB) {
try {
$TotalRows = $ws.Dimension.Rows

View File

@@ -52,7 +52,7 @@ Install-Module ImportExcel -scope CurrentUser
Install-Module ImportExcel
```
- Lots of help improvements.
- Conditional formatting can now support named ranges, and booleans in the as a condition.
- Conditional formatting can now support named ranges, and booleans in the sheet as a condition. Also brought the two different kinds together inside export-excel
- Improved handling of hyperlinks.
- Moved logic for adding a named range out of Export-Excel and into new function named Add-ExcelName, and for adding a table into a function named Add-Excel table; this is to make it easier to do these things independently of Export-Excel but minimize duplication. Add-Table command can now toggle the options from table tools toolbar (show totals etc)
- Fixed issues where formatting could be reset when using Export-Excel to manipulate an existing sheet without appending data; this applied to number formats and tables.

View File

@@ -1,10 +1,12 @@
Describe "Creating small named ranges with hyperlinks" {
$scriptPath = Split-Path -Path $MyInvocation.MyCommand.path -Parent
$dataPath = Join-Path -Path $scriptPath -ChildPath "First10Races.csv"
Describe "Creating small named ranges with hyperlinks" {
BeforeAll {
$path = "$env:TEMP\Results.xlsx"
Remove-Item -Path $path -ErrorAction SilentlyContinue
#Read race results, and group by race name : export 1 row to get headers, leaving enough rows aboce to put in a link for each race
$results = Import-Csv -Path .\First10Races.csv | Group-Object -Property RACE
$results = Import-Csv -Path $dataPath | Group-Object -Property RACE
$topRow = $lastDataRow = 1 + $results.Count
$excel = $results[0].Group[0] | Export-Excel -Path $path -StartRow $TopRow -BoldTopRow -PassThru