diff --git a/AddConditionalFormatting.ps1 b/AddConditionalFormatting.ps1 index c96e1e3..42f67e5 100644 --- a/AddConditionalFormatting.ps1 +++ b/AddConditionalFormatting.ps1 @@ -101,35 +101,48 @@ #If specified pass the rule back to the caller to allow additional customization. [switch]$Passthru ) + #Allow conditional formatting to work like Set-Format (with single ADDRESS parameter), split it to get worksheet and range of cells. If ($Address -and -not $WorkSheet -and -not $Range) { $WorkSheet = $Address.Worksheet[0] $Range = $Address.Address } - If ($ThreeIconsSet) {$rule = $WorkSheet.ConditionalFormatting.AddThreeIconSet($Range , $ThreeIconsSet)} - elseif ($FourIconsSet) {$rule = $WorkSheet.ConditionalFormatting.AddFourIconSet( $Range , $FourIconsSet) } - elseif ($FiveIconsSet) {$rule = $WorkSheet.ConditionalFormatting.AddFiveIconSet( $Range , $IconType) } - elseif ($DataBarColor) {$rule = $WorkSheet.ConditionalFormatting.AddDatabar( $Range , $DataBarColor) } - else {$rule = ($WorkSheet.ConditionalFormatting)."Add$RuleType"($Range)} - if ($reverse) {$rule.reverse = $true} - if ($ConditionValue -and $RuleType -match "Top|Botom") {$rule.Rank = $ConditionValue } - if ($ConditionValue -and $RuleType -match "StdDev") {$rule.StdDev = $ConditionValue } - if ($ConditionValue -and $RuleType -match "Than|Equal|Expression") {$rule.Formula = $ConditionValue } - if ($ConditionValue -and $RuleType -match "Text|With") {$rule.Text = $ConditionValue } - if ($ConditionValue -and - $ConditionValue2 -and $RuleType -match "Between") { - $rule.Formula = $ConditionValue - $rule.Formula2 = $ConditionValue2 + #region Create a rule of the right type + if ($PSBoundParameters.ContainsKey("ThreeIconsSet" ) ) {$rule = $WorkSheet.ConditionalFormatting.AddThreeIconSet($Range , $ThreeIconsSet)} + elseif ($PSBoundParameters.ContainsKey("FourIconsSet" ) ) {$rule = $WorkSheet.ConditionalFormatting.AddFourIconSet( $Range , $FourIconsSet) } + elseif ($PSBoundParameters.ContainsKey("FiveIconsSet" ) ) {$rule = $WorkSheet.ConditionalFormatting.AddFiveIconSet( $Range , $FiveIconsSet) } + elseif ($PSBoundParameters.ContainsKey("DataBarColor" ) ) {$rule = $WorkSheet.ConditionalFormatting.AddDatabar( $Range , $DataBarColor) } + else {$rule = ($WorkSheet.ConditionalFormatting)."Add$RuleType"($Range)} + if ($PSBoundParameters.ContainsKey("Reverse" ) ) {$rule.reverse = [boolean]$Reverse} + #endregion + #region set the rule conditions + if ($PSBoundParameters.ContainsKey("ConditionValue") -and + $RuleType -match "Top|Botom" ) {$rule.Rank = $ConditionValue } + if ($PSBoundParameters.ContainsKey("ConditionValue") -and + $RuleType -match "StdDev" ) {$rule.StdDev = $ConditionValue } + if ($PSBoundParameters.ContainsKey("ConditionValue") -and + $RuleType -match "Than|Equal|Expression" ) {$rule.Formula = $ConditionValue } + if ($PSBoundParameters.ContainsKey("ConditionValue") -and + $RuleType -match "Text|With" ) {$rule.Text = $ConditionValue } + if ($PSBoundParameters.ContainsKey("ConditionValue") -and + $PSBoundParameters.ContainsKey("ConditionValue") -and + $RuleType -match "Between" ) { + $rule.Formula = $ConditionValue; + $rule.Formula2 = $ConditionValue2 } - - if ($NumberFormat) {$rule.Style.NumberFormat.Format = (Expand-NumberFormat $NumberFormat) } - if ($Underline) {$rule.Style.Font.Underline = [OfficeOpenXml.Style.ExcelUnderLineType]::Single } - if ($Bold) {$rule.Style.Font.Bold = $true } - if ($Italic) {$rule.Style.Font.Italic = $true } - if ($StrikeThru) {$rule.Style.Font.Strike = $true } - if ($ForeGroundColor) {$rule.Style.Font.Color.color = $ForeGroundColor } - if ($BackgroundColor) {$rule.Style.Fill.BackgroundColor.color = $BackgroundColor } - if ($BackgroundPattern) {$rule.Style.Fill.PatternType = $BackgroundPattern } - if ($PatternColor) {$rule.Style.Fill.PatternColor.color = $PatternColor } - if ($Passthru) {$rule} + #endregion + #region set the rule format + if ($PSBoundParameters.ContainsKey("NumberFormat" ) ) {$rule.Style.NumberFormat.Format = (Expand-NumberFormat $NumberFormat) } + if ($Underline ) {$rule.Style.Font.Underline = [OfficeOpenXml.Style.ExcelUnderLineType]::Single } + elseif ($PSBoundParameters.ContainsKey("Underline" ) ) {$rule.Style.Font.Underline = [OfficeOpenXml.Style.ExcelUnderLineType]::None } + if ($PSBoundParameters.ContainsKey("Bold" ) ) {$rule.Style.Font.Bold = [boolean]$Bold } + if ($PSBoundParameters.ContainsKey("Italic" ) ) {$rule.Style.Font.Italic = [boolean]$Italic } + if ($PSBoundParameters.ContainsKey("StrikeThru") ) {$rule.Style.Font.Strike = [boolean]$StrikeThru } + if ($PSBoundParameters.ContainsKey("ForeGroundColor" ) ) {$rule.Style.Font.Color.color = $ForeGroundColor } + if ($PSBoundParameters.ContainsKey("BackgroundColor" ) ) {$rule.Style.Fill.BackgroundColor.color = $BackgroundColor } + if ($PSBoundParameters.ContainsKey("BackgroundPattern") ) {$rule.Style.Fill.PatternType = $BackgroundPattern } + if ($PSBoundParameters.ContainsKey("PatternColor" ) ) {$rule.Style.Fill.PatternColor.color = $PatternColor } + #endregion + #Allow further tweaking by returning the rule, if passthru specified + if ($Passthru) {$rule} } \ No newline at end of file diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index 32a1f0d..beaebb5 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -635,7 +635,8 @@ if ($isDataTypeValueType) { $ColumnIndex = $StartColumn $Row += 1 - Add-CellValue -TargetCell $ws.Cells[$Row, $ColumnIndex] -CellValue $TargetData + try {Add-CellValue -TargetCell $ws.Cells[$Row, $ColumnIndex] -CellValue $TargetData} + catch {Write-Warning "Could not insert value at Row $Row. "} } else { #region Add headers @@ -667,8 +668,8 @@ foreach ($Name in $script:Header) { #region Add non header values - Add-CellValue -TargetCell $ws.Cells[$Row, $ColumnIndex] -CellValue $TargetData.$Name - + try {Add-CellValue -TargetCell $ws.Cells[$Row, $ColumnIndex] -CellValue $TargetData.$Name} + catch {Write-Warning -Message "Could not insert the $Name property at Row $Row, Column $Column"} $ColumnIndex += 1 #endregion } diff --git a/README.md b/README.md index c4d4316..460995c 100644 --- a/README.md +++ b/README.md @@ -54,12 +54,22 @@ Install-Module ImportExcel # What's new to Aug 2018 - Set-Row and Set-Column will now create hyperlinks and insert dates correctly - Import-Excel now has an argument completer for Worksheet name - this can be slow on large files +<<<<<<< HEAD - The NumberFormat parameter (in Export-Excel, Set-Row, Set-Column, Set-Format and Add-ConditionalFormat) and X&YAxisNumberFormat parameters (in New-ExcelChartDefinition and Add-ExcelChart) now have an argument completer and the names Currency, Number, Percentage, Scientific, Fraction, Short Date ,Short time,Long time, Date-Time and Text will be converted to the correct Excel formatting strings. - Added new function Select-Worksheet to make a named sheet active: Added -Activate switch to Add-Worksheet, to make current sheet active, Export-Excel and Add-PivotTable support -Activate and pass it to Add-Worksheet, and New-PivotTableDefinition allows it to be part of the Pivot TableDefinition. - Fixed a bug in Set-Format which caused -Hidden not to work - Additional tests. +======= +- The NumberFormat parameter (in Export-Excel, Set-Row, Set-Column, Set-Format and Add-ConditionalFormat) and X&YAxisNumberFormat paramaters (in New-ExcelChartDefinition and Add-ExcelChart) now have an argument completer and the names Currency, Number, Percentage, Scientific, Fraction, Short Date ,Short time,Long time, Date-Time and Text will be converted to the correct Excel formatting strings. +- Added new function Select-Worksheet to make a named sheet active: Added -Activate switch to Add-Worksheet, to make current sheet active, Export-Excel and Add-PivotTable support -Activate and pass it to Add-Worksheet, and New-PivotTableDefinition allows it to be part of the Pivot TableDefinition. +- Fixed a bug in Set-Format which caused -Hidden not to work +- Made the same changes to Add-Conditional format as set format so -switch:$false is processed, and 0 enums and values are processed correctly +- In Export-Excel, wraped calls to Add-CellValue in a try catch so a value which causes an issue doesn't crash the whole export but generates a warning instead (#410) . +- Additional tests. +>>>>>>> 42e2b11a88475dbdd3dbd6f3b1703b33b4d0ba4b # What's new to July 18 +- Changed parameter evaluation in Set-Format to support -bold:$false (and other switches so that if false is specified the attribute will be removed ), and to bug were enums with a value of zero, and other zero parameters were not set. - Moved chart creation into its own function (Add-Excel chart) within Export-Excel.ps1. Renamed New-Excelchart to New-ExcelChartDefinition to make it clearer that it is not making anything in the workbook (but for compatibility put an alias of New-ExcelChart in so existing code does not break). Found that -Header does nothing, so it isn't Add-Excel chart and there is a message that does nothing in New-ExcelChartDefinition . - Added -BarChart -ColumnChart -LineChart -PieChart parameters to Export-Excel for quick charts without giving a full chart definition. - Added parameters for managing chart Axes and legend diff --git a/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 b/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 index acaf67e..b976a91 100644 --- a/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 +++ b/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 @@ -264,6 +264,22 @@ Describe "Set-Column, Set-Row and Set Format" { } } +Describe "Conditional Formatting" { + BeforeAll { + Remove-Item $path + $data = Get-Process | where company | select company,name,pm,handles,*mem* + $cfmt = New-ConditionalFormattingIconSet -Range "c:c" -ConditionalFormat ThreeIconSet -IconType Arrows + $data | Export-Excel -path $Path -AutoSize -ConditionalFormat $cfmt + $excel = Open-ExcelPackage -Path $path + $ws = $excel.Workbook.Worksheets[1] + } + Context "Using a pre-prepared 3 Arrows rule" { + it "Set the right type, IconSet and range " { + $ws.ConditionalFormatting[0].IconSet | Should be "Arrows" + $ws.ConditionalFormatting[0].Address.Address | Should be "c:c" + $ws.ConditionalFormatting[0].Type.ToString() | Should be "ThreeIconSet" + } + } - +}