Fix 276 and 262

This commit is contained in:
dfinke
2018-01-12 19:32:19 -05:00
parent 2f70cd88e8
commit a5b9ddc257
4 changed files with 143 additions and 143 deletions

View File

@@ -4,20 +4,20 @@
Adds a column to the existing data area in an Excel sheet, fills values and sets formatting Adds a column to the existing data area in an Excel sheet, fills values and sets formatting
.DESCRIPTION .DESCRIPTION
Set-Column takes a value which is either string containing a value or formula or a scriptblock Set-Column takes a value which is either string containing a value or formula or a scriptblock
which evaluates to a string, and optionally a column number and fills that value down the column. which evaluates to a string, and optionally a column number and fills that value down the column.
A column name can be specified and the new column can be made a named range. A column name can be specified and the new column can be made a named range.
The column can be formatted. The column can be formatted.
.Example .Example
C:> Set-Column -Worksheet $ws -Heading "WinsToFastLaps" -Value {"=E$row/C$row"} -Column 7 -AutoSize -AutoNameRange C:> Set-Column -Worksheet $ws -Heading "WinsToFastLaps" -Value {"=E$row/C$row"} -Column 7 -AutoSize -AutoNameRange
Here $WS already contains a worksheet which contains counts of races won and fastest laps recorded by racing drivers (in columns C and E) Here $WS already contains a worksheet which contains counts of races won and fastest laps recorded by racing drivers (in columns C and E)
Set-Column specifies that Column 7 should have a heading of "WinsToFastLaps" and the data cells should contain =E2/C2 , =E3/C3 Set-Column specifies that Column 7 should have a heading of "WinsToFastLaps" and the data cells should contain =E2/C2 , =E3/C3
the data celss should become a named range, which will also be "WinsToFastLaps" the column width will be set automatically the data celss should become a named range, which will also be "WinsToFastLaps" the column width will be set automatically
#> #>
[cmdletbinding()] [cmdletbinding()]
Param ( Param (
[Parameter(ParameterSetName="Package",Mandatory=$true)] [Parameter(ParameterSetName="Package",Mandatory=$true)]
[OfficeOpenXml.ExcelPackage]$ExcelPackage, [OfficeOpenXml.ExcelPackage]$ExcelPackage,
#Sheet to update #Sheet to update
[Parameter(ParameterSetName="Package")] [Parameter(ParameterSetName="Package")]
$Worksheetname = "Sheet1", $Worksheetname = "Sheet1",
@@ -27,7 +27,7 @@
#Column to fill down - first column is 1. 0 will be interpreted as first unused column #Column to fill down - first column is 1. 0 will be interpreted as first unused column
$Column = 0 , $Column = 0 ,
[Int]$StartRow , [Int]$StartRow ,
#value, formula or script block for to fill in. Script block can use $row, $column [number], $ColumnName [letter(s)], $startRow, $startColumn, $endRow, $endColumn #value, formula or script block for to fill in. Script block can use $row, $column [number], $ColumnName [letter(s)], $startRow, $startColumn, $endRow, $endColumn
[parameter(Mandatory=$true)] [parameter(Mandatory=$true)]
$Value , $Value ,
#Optional column heading #Optional column heading
@@ -80,41 +80,41 @@
[Switch]$AutoNameRange, [Switch]$AutoNameRange,
[switch]$PassThru [switch]$PassThru
) )
#if we were passed a package object and a worksheet name , get the worksheet. #if we were passed a package object and a worksheet name , get the worksheet.
if ($ExcelPackage) {$Worksheet = $ExcelPackage.Workbook.Worksheets[$Worksheetname] } if ($ExcelPackage) {$Worksheet = $ExcelPackage.Workbook.Worksheets[$Worksheetname] }
#In a script block to build a formula, we may want any of corners or the columnname, #In a script block to build a formula, we may want any of corners or the columnname,
#if column and startrow aren't specified, assume first unused column, and first row #if column and startrow aren't specified, assume first unused column, and first row
if (-not $StartRow) {$startRow = $Worksheet.Dimension.Start.Row } if (-not $StartRow) {$startRow = $Worksheet.Dimension.Start.Row }
$StartColumn = $Worksheet.Dimension.Start.Column $StartColumn = $Worksheet.Dimension.Start.Column
$endColumn = $Worksheet.Dimension.End.Column $endColumn = $Worksheet.Dimension.End.Column
$endRow = $Worksheet.Dimension.End.Row $endRow = $Worksheet.Dimension.End.Row
if ($Column -lt 2 ) {$Column = $endColumn + 1 } if ($Column -lt 2 ) {$Column = $endColumn + 1 }
$ColumnName = [OfficeOpenXml.ExcelCellAddress]::new(1,$column).Address -replace "1","" $ColumnName = [OfficeOpenXml.ExcelCellAddress]::new(1,$column).Address -replace "1",""
Write-Verbose -Message "Updating Column $ColumnName" Write-Verbose -Message "Updating Column $ColumnName"
#If there is a heading, insert it and use it as the name for a range (if we're creating one) #If there is a heading, insert it and use it as the name for a range (if we're creating one)
if ($Heading) { if ($Heading) {
$Worksheet.Cells[$StartRow, $Column].Value = $heading $Worksheet.Cells[$StartRow, $Column].Value = $heading
$startRow ++ $startRow ++
if ($AutoNameRange) { $Worksheet.Names.Add( $heading, ($Worksheet.Cells[$startrow, $Column, $endRow, $Column]) ) | Out-Null } if ($AutoNameRange) { $Worksheet.Names.Add( $heading, ($Worksheet.Cells[$startrow, $Column, $endRow, $Column]) ) | Out-Null }
} }
#Fill in the data #Fill in the data
if ($value) { foreach ($row in ($StartRow.. $endRow)) { if ($value) { foreach ($row in ($StartRow.. $endRow)) {
if ($Value -is [scriptblock]) { #re-create the script block otherwise variables from this function are out of scope. if ($Value -is [scriptblock]) { #re-create the script block otherwise variables from this function are out of scope.
$cellData = & ([scriptblock]::create( $Value )) $cellData = & ([scriptblock]::create( $Value ))
Write-Verbose -Message $cellData Write-Verbose -Message $cellData
} }
else { $cellData = $Value} else { $cellData = $Value}
if ($cellData -match "^=") { $Worksheet.Cells[$Row, $Column].Formula = $cellData } if ($cellData -match "^=") { $Worksheet.Cells[$Row, $Column].Formula = $cellData }
else { $Worksheet.Cells[$Row, $Column].Value = $cellData } else { $Worksheet.Cells[$Row, $Column].Value = $cellData }
if ($cellData -is [datetime]) { $Worksheet.Cells[$Row, $Column].Style.Numberformat.Format = 'm/d/yy h:mm' } if ($cellData -is [datetime]) { $Worksheet.Cells[$Row, $Column].Style.Numberformat.Format = 'm/d/yy h:mm' }
}} }}
#region Apply formatting #region Apply formatting
if ($Underline) { if ($Underline) {
$Worksheet.Column( $Column).Style.Font.UnderLine = $true $Worksheet.Column( $Column).Style.Font.UnderLine = $true
$Worksheet.Column( $Column).Style.Font.UnderLineType = $UnderLineType $Worksheet.Column( $Column).Style.Font.UnderLineType = $UnderLineType
} }
if ($Bold) { $Worksheet.Column( $Column).Style.Font.Bold = $true } if ($Bold) { $Worksheet.Column( $Column).Style.Font.Bold = $true }
if ($Italic) { $Worksheet.Column( $Column).Style.Font.Italic = $true } if ($Italic) { $Worksheet.Column( $Column).Style.Font.Italic = $true }
if ($StrikeThru) { $Worksheet.Column( $Column).Style.Font.Strike = $true } if ($StrikeThru) { $Worksheet.Column( $Column).Style.Font.Strike = $true }
@@ -123,9 +123,9 @@
if ($TextRotation) { $Worksheet.Column( $Column).Style.TextRotation = $TextRotation } if ($TextRotation) { $Worksheet.Column( $Column).Style.TextRotation = $TextRotation }
if ($WrapText) { $Worksheet.Column( $Column).Style.WrapText = $true } if ($WrapText) { $Worksheet.Column( $Column).Style.WrapText = $true }
if ($HorizontalAlignment) { $Worksheet.Column( $Column).Style.HorizontalAlignment = $HorizontalAlignment} if ($HorizontalAlignment) { $Worksheet.Column( $Column).Style.HorizontalAlignment = $HorizontalAlignment}
if ($VerticalAlignment) { $Worksheet.Column( $Column).Style.VerticalAlignment = $VerticalAlignment } if ($VerticalAlignment) { $Worksheet.Column( $Column).Style.VerticalAlignment = $VerticalAlignment }
if ($FontColor) { $Worksheet.Column( $Column).Style.Font.Color.SetColor( $FontColor ) } if ($FontColor) { $Worksheet.Column( $Column).Style.Font.Color.SetColor( $FontColor ) }
if ($BorderRound) { $Worksheet.Column( $Column).Style.Border.BorderAround( $BorderAround ) } if ($BorderAround) { $Worksheet.Column( $Column).Style.Border.BorderAround( $BorderAround ) }
if ($BackgroundColor) { if ($BackgroundColor) {
$Worksheet.Column( $Column).Style.Fill.PatternType = $BackgroundPattern $Worksheet.Column( $Column).Style.Fill.PatternType = $BackgroundPattern
$Worksheet.Column( $Column).Style.Fill.BackgroundColor.SetColor($BackgroundColor ) $Worksheet.Column( $Column).Style.Fill.BackgroundColor.SetColor($BackgroundColor )
@@ -133,7 +133,7 @@
} }
if ($Autosize) { $Worksheet.Column( $Column).AutoFit() } if ($Autosize) { $Worksheet.Column( $Column).AutoFit() }
elseif ($Width) { $Worksheet.Column( $Column).Width = $Width } elseif ($Width) { $Worksheet.Column( $Column).Width = $Width }
#endregion #endregion
#return the new data if -passthru was specified. #return the new data if -passthru was specified.
if ($passThru) { $Worksheet.Column( $Column)} if ($passThru) { $Worksheet.Column( $Column)}
} }

View File

@@ -1,40 +1,40 @@
Function Set-Row { Function Set-Row {
<# <#
.Synopsis .Synopsis
Fills values into a row in a Excel spreadsheet Fills values into a row in a Excel spreadsheet
.Description .Description
Set-Row accepts either a Worksheet object or an Excel package object returned by Export-Excel and the name of a sheet, Set-Row accepts either a Worksheet object or an Excel package object returned by Export-Excel and the name of a sheet,
and inserts the chosen contents into a row of the sheet. and inserts the chosen contents into a row of the sheet.
The contents can be a constant "42" , a formula or a script block which is converted into a constant or formula. The contents can be a constant "42" , a formula or a script block which is converted into a constant or formula.
The first cell of the row can optional be given a heading. The first cell of the row can optional be given a heading.
.Example .Example
Set-row -Worksheet $ws -Heading Total -Value {"=sum($columnName`2:$columnName$endrow)" } Set-row -Worksheet $ws -Heading Total -Value {"=sum($columnName`2:$columnName$endrow)" }
$Ws contains a worksheet object, and no Row number is specified so Set-Row will select the next row after the end of the data in the sheet $Ws contains a worksheet object, and no Row number is specified so Set-Row will select the next row after the end of the data in the sheet
The first cell will contain "Total", and each other cell will contain The first cell will contain "Total", and each other cell will contain
=Sum(xx2:xx99) - where xx is the column name, and 99 is the last row of data. =Sum(xx2:xx99) - where xx is the column name, and 99 is the last row of data.
Note the use of `2 to Prevent 2 becoming part of the variable "ColumnName" Note the use of `2 to Prevent 2 becoming part of the variable "ColumnName"
The script block can use $row, $column, $ColumnName, $startRow/Column $endRow/Column The script block can use $row, $column, $ColumnName, $startRow/Column $endRow/Column
#> #>
[cmdletbinding()] [cmdletbinding()]
Param ( Param (
#An Excel package object - e.g. from Export-Excel -passthru - requires a sheet name #An Excel package object - e.g. from Export-Excel -passthru - requires a sheet name
[Parameter(ParameterSetName="Package",Mandatory=$true)] [Parameter(ParameterSetName="Package",Mandatory=$true)]
[OfficeOpenXml.ExcelPackage]$ExcelPackage, [OfficeOpenXml.ExcelPackage]$ExcelPackage,
#the name to update in the package #the name to update in the package
[Parameter(ParameterSetName="Package")] [Parameter(ParameterSetName="Package")]
$Worksheetname = "Sheet1", $Worksheetname = "Sheet1",
#A worksheet object #A worksheet object
[Parameter(ParameterSetName="sheet",Mandatory=$true)] [Parameter(ParameterSetName="sheet",Mandatory=$true)]
[OfficeOpenXml.Excelworksheet] [OfficeOpenXml.Excelworksheet]
$Worksheet, $Worksheet,
#Row to fill right - first row is 1. 0 will be interpreted as first unused row #Row to fill right - first row is 1. 0 will be interpreted as first unused row
$Row = 0 , $Row = 0 ,
#Position in the row to start from #Position in the row to start from
[Int]$StartColumn, [Int]$StartColumn,
#value, formula or script block for to fill in. Script block can use $row, $column [number], $ColumnName [letter(s)], $startRow, $startColumn, $endRow, $endColumn #value, formula or script block for to fill in. Script block can use $row, $column [number], $ColumnName [letter(s)], $startRow, $startColumn, $endRow, $endColumn
[parameter(Mandatory=$true)] [parameter(Mandatory=$true)]
$Value, $Value,
#Optional Row heading #Optional Row heading
@@ -78,43 +78,43 @@
#Degrees to rotate text. Up to +90 for anti-clockwise ("upwards"), or to -90 for clockwise. #Degrees to rotate text. Up to +90 for anti-clockwise ("upwards"), or to -90 for clockwise.
[ValidateRange(-90, 90)] [ValidateRange(-90, 90)]
[int]$TextRotation , [int]$TextRotation ,
#Set cells to a fixed hieght #Set cells to a fixed hieght
[float]$Height, [float]$Height,
[switch]$PassThru [switch]$PassThru
) )
#if we were passed a package object and a worksheet name , get the worksheet. #if we were passed a package object and a worksheet name , get the worksheet.
if ($ExcelPackage) {$Worksheet = $ExcelPackage.Workbook.worksheets[$Worksheetname] } if ($ExcelPackage) {$Worksheet = $ExcelPackage.Workbook.worksheets[$Worksheetname] }
#In a script block to build a formula, we may want any of corners or the columnname, #In a script block to build a formula, we may want any of corners or the columnname,
#if row and start column aren't specified assume first unused row, and first column #if row and start column aren't specified assume first unused row, and first column
if (-not $StartColumn) {$StartColumn = $Worksheet.Dimension.Start.Column } if (-not $StartColumn) {$StartColumn = $Worksheet.Dimension.Start.Column }
$startRow = $Worksheet.Dimension.Start.Row + 1 $startRow = $Worksheet.Dimension.Start.Row + 1
$endColumn = $Worksheet.Dimension.End.Column $endColumn = $Worksheet.Dimension.End.Column
$endRow = $Worksheet.Dimension.End.Row $endRow = $Worksheet.Dimension.End.Row
if ($Row -lt 2 ) {$Row = $endRow + 1 } if ($Row -lt 2 ) {$Row = $endRow + 1 }
Write-Verbose -Message "Updating Row $Row" Write-Verbose -Message "Updating Row $Row"
#Add a row label #Add a row label
if ($Heading) { if ($Heading) {
$Worksheet.Cells[$Row, $StartColumn].Value = $Heading $Worksheet.Cells[$Row, $StartColumn].Value = $Heading
$StartColumn ++ $StartColumn ++
} }
#Fill in the data #Fill in the data
if ($value) {foreach ($column in ($StartColumn..$EndColumn)) { if ($value) {foreach ($column in ($StartColumn..$EndColumn)) {
#We might want the column name in a script block #We might want the column name in a script block
$ColumnName = [OfficeOpenXml.ExcelCellAddress]::new(1,$column).Address -replace "1","" $ColumnName = [OfficeOpenXml.ExcelCellAddress]::new(1,$column).Address -replace "1",""
if ($Value -is [scriptblock] ) { if ($Value -is [scriptblock] ) {
#re-create the script block otherwise variables from this function are out of scope. #re-create the script block otherwise variables from this function are out of scope.
$cellData = & ([scriptblock]::create( $Value )) $cellData = & ([scriptblock]::create( $Value ))
Write-Verbose -Message $cellData Write-Verbose -Message $cellData
} }
else{$cellData = $Value} else{$cellData = $Value}
if ($cellData -match "^=") { $Worksheet.Cells[$Row, $column].Formula = $cellData } if ($cellData -match "^=") { $Worksheet.Cells[$Row, $column].Formula = $cellData }
else { $Worksheet.Cells[$Row, $Column].Value = $cellData } else { $Worksheet.Cells[$Row, $Column].Value = $cellData }
if ($cellData -is [datetime]) { $Worksheet.Cells[$Row, $Column].Style.Numberformat.Format = 'm/d/yy h:mm' } if ($cellData -is [datetime]) { $Worksheet.Cells[$Row, $Column].Style.Numberformat.Format = 'm/d/yy h:mm' }
}} }}
#region Apply formatting #region Apply formatting
if ($Underline) { if ($Underline) {
$worksheet.row( $Row ).Style.Font.UnderLine = $true $worksheet.row( $Row ).Style.Font.UnderLine = $true
$worksheet.row( $Row ).Style.Font.UnderLineType = $UnderLineType $worksheet.row( $Row ).Style.Font.UnderLineType = $UnderLineType
@@ -130,13 +130,13 @@
if ($VerticalAlignment) { $worksheet.row( $Row ).Style.VerticalAlignment = $VerticalAlignment } if ($VerticalAlignment) { $worksheet.row( $Row ).Style.VerticalAlignment = $VerticalAlignment }
if ($Height) { $worksheet.row( $Row ).Height = $Height } if ($Height) { $worksheet.row( $Row ).Height = $Height }
if ($FontColor) { $worksheet.row( $Row ).Style.Font.Color.SetColor( $FontColor ) } if ($FontColor) { $worksheet.row( $Row ).Style.Font.Color.SetColor( $FontColor ) }
if ($BorderRound) { $worksheet.row( $Row ).Style.Border.BorderAround( $BorderAround ) } if ($BorderAround) { $worksheet.row( $Row ).Style.Border.BorderAround( $BorderAround ) }
if ($BackgroundColor) { if ($BackgroundColor) {
$worksheet.row( $Row ).Style.Fill.PatternType = $BackgroundPattern $worksheet.row( $Row ).Style.Fill.PatternType = $BackgroundPattern
$worksheet.row( $Row ).Style.Fill.BackgroundColor.SetColor($BackgroundColor ) $worksheet.row( $Row ).Style.Fill.BackgroundColor.SetColor($BackgroundColor )
if ($PatternColor) { $worksheet.row( $Row ).Style.Fill.PatternColor.SetColor( $PatternColor ) } if ($PatternColor) { $worksheet.row( $Row ).Style.Fill.PatternColor.SetColor( $PatternColor ) }
} }
#endregion #endregion
#return the new data if -passthru was specified. #return the new data if -passthru was specified.
if ($passThru) {$Worksheet.Row($Row)} if ($passThru) {$Worksheet.Row($Row)}
} }

View File

@@ -72,22 +72,22 @@
[switch]$Hidden [switch]$Hidden
) )
begin { begin {
#Allow Set-Format to take Worksheet and range parameters (like Add Contitional formatting) - convert them to an address #Allow Set-Format to take Worksheet and range parameters (like Add Contitional formatting) - convert them to an address
if ($WorkSheet -and $Range) {$Address = $WorkSheet.Cells[$Range] } if ($WorkSheet -and $Range) {$Address = $WorkSheet.Cells[$Range] }
} }
process { process {
if ($Address -is [Array]) { if ($Address -is [Array]) {
[void]$PSBoundParameters.Remove("Address") [void]$PSBoundParameters.Remove("Address")
$Address | Set-Format @PSBoundParameters $Address | Set-Format @PSBoundParameters
} }
else { else {
if ($ResetFont) { if ($ResetFont) {
$Address.Style.Font.Color.SetColor("Black") $Address.Style.Font.Color.SetColor("Black")
$Address.Style.Font.Bold = $false $Address.Style.Font.Bold = $false
$Address.Style.Font.Italic = $false $Address.Style.Font.Italic = $false
$Address.Style.Font.UnderLine = $false $Address.Style.Font.UnderLine = $false
$Address.Style.Font.Strike = $falsee $Address.Style.Font.Strike = $false
} }
if ($Underline) { if ($Underline) {
$Address.Style.Font.UnderLine = $true $Address.Style.Font.UnderLine = $true
@@ -98,7 +98,7 @@
if ($StrikeThru) {$Address.Style.Font.Strike = $true } if ($StrikeThru) {$Address.Style.Font.Strike = $true }
if ($FontShift) {$Address.Style.Font.VerticalAlign = $FontShift } if ($FontShift) {$Address.Style.Font.VerticalAlign = $FontShift }
if ($FontColor) {$Address.Style.Font.Color.SetColor( $FontColor ) } if ($FontColor) {$Address.Style.Font.Color.SetColor( $FontColor ) }
if ($BorderRound) {$Address.Style.Border.BorderAround( $BorderAround ) } if ($BorderAround) {$Address.Style.Border.BorderAround( $BorderAround ) }
if ($NumberFormat) {$Address.Style.Numberformat.Format = $NumberFormat } if ($NumberFormat) {$Address.Style.Numberformat.Format = $NumberFormat }
if ($TextRotation) {$Address.Style.TextRotation = $TextRotation } if ($TextRotation) {$Address.Style.TextRotation = $TextRotation }
if ($WrapText) {$Address.Style.WrapText = $true } if ($WrapText) {$Address.Style.WrapText = $true }

View File

@@ -1,58 +1,58 @@
Function Add-ConditionalFormatting { Function Add-ConditionalFormatting {
<# <#
.Synopsis .Synopsis
Adds contitional formatting to worksheet Adds contitional formatting to worksheet
.Example .Example
$excel = $avdata | Export-Excel -Path (Join-path $FilePath "\Machines.XLSX" ) -WorksheetName "Server Anti-Virus" -AutoSize -FreezeTopRow -AutoFilter -PassThru $excel = $avdata | Export-Excel -Path (Join-path $FilePath "\Machines.XLSX" ) -WorksheetName "Server Anti-Virus" -AutoSize -FreezeTopRow -AutoFilter -PassThru
Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets[1] -Address "b":b1048576" -ForeGroundColor "RED" -RuleType ContainsText -ConditionValue "2003" Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets[1] -Address "b":b1048576" -ForeGroundColor "RED" -RuleType ContainsText -ConditionValue "2003"
Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets[1] -Address "i2:i1048576" -ForeGroundColor "RED" -RuleType ContainsText -ConditionValue "Disabled" Add-ConditionalFormatting -WorkSheet $excel.Workbook.Worksheets[1] -Address "i2:i1048576" -ForeGroundColor "RED" -RuleType ContainsText -ConditionValue "Disabled"
$excel.Workbook.Worksheets[1].Cells["D1:G1048576"].Style.Numberformat.Format = [cultureinfo]::CurrentCulture.DateTimeFormat.ShortDatePattern $excel.Workbook.Worksheets[1].Cells["D1:G1048576"].Style.Numberformat.Format = [cultureinfo]::CurrentCulture.DateTimeFormat.ShortDatePattern
$excel.Workbook.Worksheets[1].Row(1).style.font.bold = $true $excel.Workbook.Worksheets[1].Row(1).style.font.bold = $true
$excel.Save() ; $excel.Dispose() $excel.Save() ; $excel.Dispose()
Here Export-Excel is called with the -passThru parameter so the Excel Package object is stored in $Excel Here Export-Excel is called with the -passThru parameter so the Excel Package object is stored in $Excel
The desired worksheet is selected and the then columns B and i are conditially formatted (excluding the top row) to show The desired worksheet is selected and the then columns B and i are conditially formatted (excluding the top row) to show
Fixed formats are then applied to dates in columns D..G and the top row is formatted Fixed formats are then applied to dates in columns D..G and the top row is formatted
Finally the workbook is saved and the Excel closed. Finally the workbook is saved and the Excel closed.
#> #>
Param ( Param (
#The worksheet where the format is to be applied #The worksheet where the format is to be applied
[OfficeOpenXml.ExcelWorksheet]$WorkSheet , [OfficeOpenXml.ExcelWorksheet]$WorkSheet ,
#The area of the worksheet where the format is to be applied #The area of the worksheet where the format is to be applied
[OfficeOpenXml.ExcelAddress]$Range , [OfficeOpenXml.ExcelAddress]$Range ,
#One of the standard named rules - Top / Bottom / Less than / Greater than / Contains etc #One of the standard named rules - Top / Bottom / Less than / Greater than / Contains etc
[Parameter(Mandatory=$true,ParameterSetName="NamedRule",Position=3)] [Parameter(Mandatory=$true,ParameterSetName="NamedRule",Position=3)]
[OfficeOpenXml.ConditionalFormatting.eExcelConditionalFormattingRuleType]$RuleType , [OfficeOpenXml.ConditionalFormatting.eExcelConditionalFormattingRuleType]$RuleType ,
#Text colour for matching objects #Text colour for matching objects
[Alias("ForeGroundColour")] [Alias("ForeGroundColour")]
[System.Drawing.Color]$ForeGroundColor, [System.Drawing.Color]$ForeGroundColor,
#colour for databar type charts #colour for databar type charts
[Parameter(Mandatory=$true,ParameterSetName="DataBar")] [Parameter(Mandatory=$true,ParameterSetName="DataBar")]
[Alias("DataBarColour")] [Alias("DataBarColour")]
[System.Drawing.Color]$DataBarColor, [System.Drawing.Color]$DataBarColor,
#One of the three-icon set types (e.g. Traffic Lights) #One of the three-icon set types (e.g. Traffic Lights)
[Parameter(Mandatory=$true,ParameterSetName="ThreeIconSet")] [Parameter(Mandatory=$true,ParameterSetName="ThreeIconSet")]
[OfficeOpenXml.ConditionalFormatting.eExcelconditionalFormatting3IconsSetType]$ThreeIconsSet, [OfficeOpenXml.ConditionalFormatting.eExcelconditionalFormatting3IconsSetType]$ThreeIconsSet,
#A four-icon set name #A four-icon set name
[Parameter(Mandatory=$true,ParameterSetName="FourIconSet")] [Parameter(Mandatory=$true,ParameterSetName="FourIconSet")]
[OfficeOpenXml.ConditionalFormatting.eExcelconditionalFormatting4IconsSetType]$FourIconsSet, [OfficeOpenXml.ConditionalFormatting.eExcelconditionalFormatting4IconsSetType]$FourIconsSet,
#A five-icon set name #A five-icon set name
[Parameter(Mandatory=$true,ParameterSetName="FiveIconSet")] [Parameter(Mandatory=$true,ParameterSetName="FiveIconSet")]
[OfficeOpenXml.ConditionalFormatting.eExcelconditionalFormatting5IconsSetType]$FiveIconsSet, [OfficeOpenXml.ConditionalFormatting.eExcelconditionalFormatting5IconsSetType]$FiveIconsSet,
#A value for the condition (e.g. "2000" if the test is 'lessthan 2000') #A value for the condition (e.g. "2000" if the test is 'lessthan 2000')
[string]$ConditionValue, [string]$ConditionValue,
#A second value for the conditions like between x and Y #A second value for the conditions like between x and Y
[string]$ConditionValue2, [string]$ConditionValue2,
#Background colour for matching items #Background colour for matching items
[System.Drawing.Color]$BackgroundColor, [System.Drawing.Color]$BackgroundColor,
#Background pattern for matching items #Background pattern for matching items
[OfficeOpenXml.Style.ExcelFillStyle]$BackgroundPattern = [OfficeOpenXml.Style.ExcelFillStyle]::Solid, [OfficeOpenXml.Style.ExcelFillStyle]$BackgroundPattern = [OfficeOpenXml.Style.ExcelFillStyle]::Solid,
#Secondary colour when a background pattern requires it #Secondary colour when a background pattern requires it
[System.Drawing.Color]$PatternColor, [System.Drawing.Color]$PatternColor,
#Sets the numeric format for matching items #Sets the numeric format for matching items
$NumberFormat, $NumberFormat,
#Put matching items in bold face #Put matching items in bold face
[switch]$Bold, [switch]$Bold,
#Put matching items in italic #Put matching items in italic
@@ -61,27 +61,27 @@
[switch]$Underline, [switch]$Underline,
#Strikethrough text of matching items #Strikethrough text of matching items
[switch]$StrikeThru [switch]$StrikeThru
) )
If ($ThreeIconsSet) {$rule = $WorkSheet.ConditionalFormatting.AddThreeIconSet($Range , $ThreeIconsSet)} If ($ThreeIconsSet) {$rule = $WorkSheet.ConditionalFormatting.AddThreeIconSet($Range , $ThreeIconsSet)}
elseif ($FourIconsSet) {$rule = $WorkSheet.ConditionalFormatting.AddFourIconSet( $Range , $FourIconsSet) } elseif ($FourIconsSet) {$rule = $WorkSheet.ConditionalFormatting.AddFourIconSet( $Range , $FourIconsSet) }
elseif ($FiveIconsSet) {$rule = $WorkSheet.ConditionalFormatting.AddFiveIconSet( $Range , $IconType) } elseif ($FiveIconsSet) {$rule = $WorkSheet.ConditionalFormatting.AddFiveIconSet( $Range , $IconType) }
elseif ($DataBarColor) {$rule = $WorkSheet.ConditionalFormatting.AddDatabar( $Range , $DataBarColor) } elseif ($DataBarColor) {$rule = $WorkSheet.ConditionalFormatting.AddDatabar( $Range , $DataBarColor) }
else { $rule = ($WorkSheet.ConditionalFormatting)."Add$RuleType"($Range)} else { $rule = ($WorkSheet.ConditionalFormatting)."Add$RuleType"($Range)}
if ($ConditionValue -and $RuleType -match "Top|Botom") {$rule.Rank = $ConditionValue } if ($ConditionValue -and $RuleType -match "Top|Botom") {$rule.Rank = $ConditionValue }
if ($ConditionValue -and $RuleType -match "StdDev") {$rule.StdDev = $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 "Than|Equal|Expression") {$rule.Formula = $ConditionValue }
if ($ConditionValue -and $RuleType -match "Text|With") {$rule.Text = $ConditionValue } if ($ConditionValue -and $RuleType -match "Text|With") {$rule.Text = $ConditionValue }
if ($ConditionValue -and if ($ConditionValue -and
$ConditionValue2 -and $RuleType -match "Between") {$rule.Formula = $ConditionValue $ConditionValue2 -and $RuleType -match "Between") {$rule.Formula = $ConditionValue
$rule.Formula2 = $ConditionValue2} $rule.Formula2 = $ConditionValue2}
if ($NumberFormat) {$rule.Style.NumberFormat.Format = $NumberFormat } if ($NumberFormat) {$rule.Style.NumberFormat.Format = $NumberFormat }
if ($Underline) {$rule.Style.Font.Underline = [OfficeOpenXml.Style.ExcelUnderLineType]::Single } if ($Underline) {$rule.Style.Font.Underline = [OfficeOpenXml.Style.ExcelUnderLineType]::Single }
if ($Bold) {$rule.Style.Font.Bold = $true} if ($Bold) {$rule.Style.Font.Bold = $true}
if ($Italic) {$rule.Style.Font.Italic = $true} if ($Italic) {$rule.Style.Font.Italic = $true}
if ($StrikeThru) {$rule.Style.Font.Strike = $true} if ($StrikeThru) {$rule.Style.Font.Strike = $true}
if ($ForeGroundColor) {$rule.Style.Font.Color.color = $ForeGroundColor } if ($ForeGroundColor) {$rule.Style.Font.Color.color = $ForeGroundColor }
if ($BackgroundColor) {$rule.Style.Fill.BackgroundColor.color = $BackgroundColor } if ($BackgroundColor) {$rule.Style.Fill.BackgroundColor.color = $BackgroundColor }
if ($BackgroundPattern) {$rule.Style.Fill.PatternType = $BackgroundPattern } if ($BackgroundPattern) {$rule.Style.Fill.PatternType = $BackgroundPattern }
@@ -90,14 +90,14 @@
Function Set-Format { Function Set-Format {
<# <#
.SYNOPSIS .SYNOPSIS
Applies Number, font, alignment and colour formatting to a range of Excel Cells Applies Number, font, alignment and colour formatting to a range of Excel Cells
.EXAMPLE .EXAMPLE
$sheet.Column(3) | Set-Format -HorizontalAlignment Right -NumberFormat "#,###" $sheet.Column(3) | Set-Format -HorizontalAlignment Right -NumberFormat "#,###"
Selects column 3 from a sheet object (within a workbook object, which is a child of the ExcelPackage object) and passes it to Set-Format which formats as an integer with comma seperated groups Selects column 3 from a sheet object (within a workbook object, which is a child of the ExcelPackage object) and passes it to Set-Format which formats as an integer with comma seperated groups
.EXAMPLE .EXAMPLE
Set-Format -Address $sheet.Cells["E1:H1048576"] -HorizontalAlignment Right -NumberFormat "#,###" Set-Format -Address $sheet.Cells["E1:H1048576"] -HorizontalAlignment Right -NumberFormat "#,###"
Instead of piping the address in this version specifies a block of cells and applies similar formatting Instead of piping the address in this version specifies a block of cells and applies similar formatting
#> #>
Param ( Param (
@@ -105,62 +105,62 @@ Function Set-Format {
[Parameter(ValueFromPipeline=$true)] [Parameter(ValueFromPipeline=$true)]
[object[]]$Address , [object[]]$Address ,
#Number format to apply to cells e.g. "dd/MM/yyyy HH:mm", "£#,##0.00;[Red]-£#,##0.00", "0.00%" , "##/##" , "0.0E+0" etc #Number format to apply to cells e.g. "dd/MM/yyyy HH:mm", "£#,##0.00;[Red]-£#,##0.00", "0.00%" , "##/##" , "0.0E+0" etc
[Alias("NFormat")] [Alias("NFormat")]
$NumberFormat, $NumberFormat,
#Style of border to draw around the range #Style of border to draw around the range
[OfficeOpenXml.Style.ExcelBorderStyle]$BorderAround, [OfficeOpenXml.Style.ExcelBorderStyle]$BorderAround,
#Colour for the text - if none specified it will be left as it it is #Colour for the text - if none specified it will be left as it it is
[System.Drawing.Color]$FontColor, [System.Drawing.Color]$FontColor,
#Clear Bold, Italic, StrikeThrough and Underline and set colour to black #Clear Bold, Italic, StrikeThrough and Underline and set colour to black
[switch]$ResetFont, [switch]$ResetFont,
#Make text bold #Make text bold
[switch]$Bold, [switch]$Bold,
#Make text italic #Make text italic
[switch]$Italic, [switch]$Italic,
#Underline the text using the underline style in -underline type #Underline the text using the underline style in -underline type
[switch]$Underline, [switch]$Underline,
#Should Underline use single or double, normal or accounting mode : default is single normal #Should Underline use single or double, normal or accounting mode : default is single normal
[OfficeOpenXml.Style.ExcelUnderLineType]$UnderLineType = [OfficeOpenXml.Style.ExcelUnderLineType]::Single, [OfficeOpenXml.Style.ExcelUnderLineType]$UnderLineType = [OfficeOpenXml.Style.ExcelUnderLineType]::Single,
#StrikeThrough text #StrikeThrough text
[switch]$StrikeThru, [switch]$StrikeThru,
#Subscript or superscript #Subscript or superscript
[OfficeOpenXml.Style.ExcelVerticalAlignmentFont]$FontShift, [OfficeOpenXml.Style.ExcelVerticalAlignmentFont]$FontShift,
#Font to use - Excel defaults to Calibri #Font to use - Excel defaults to Calibri
[String]$FontName, [String]$FontName,
#Point size for the text #Point size for the text
[float]$FontSize, [float]$FontSize,
#Change background colour #Change background colour
[System.Drawing.Color]$BackgroundColor, [System.Drawing.Color]$BackgroundColor,
#Background pattern - solid by default #Background pattern - solid by default
[OfficeOpenXml.Style.ExcelFillStyle]$BackgroundPattern =[OfficeOpenXml.Style.ExcelFillStyle]::Solid , [OfficeOpenXml.Style.ExcelFillStyle]$BackgroundPattern =[OfficeOpenXml.Style.ExcelFillStyle]::Solid ,
#Secondary colour for background pattern #Secondary colour for background pattern
[Alias("PatternColour")] [Alias("PatternColour")]
[System.Drawing.Color]$PatternColor, [System.Drawing.Color]$PatternColor,
#Turn on text wrapping #Turn on text wrapping
[switch]$WrapText, [switch]$WrapText,
#Position cell contents to left, right or centre ... #Position cell contents to left, right or centre ...
[OfficeOpenXml.Style.ExcelHorizontalAlignment]$HorizontalAlignment, [OfficeOpenXml.Style.ExcelHorizontalAlignment]$HorizontalAlignment,
#Position cell contents to top bottom or centre #Position cell contents to top bottom or centre
[OfficeOpenXml.Style.ExcelVerticalAlignment]$VerticalAlignment, [OfficeOpenXml.Style.ExcelVerticalAlignment]$VerticalAlignment,
#Degrees to rotate text. Up to +90 for anti-clockwise ("upwards"), or to -90 for clockwise. #Degrees to rotate text. Up to +90 for anti-clockwise ("upwards"), or to -90 for clockwise.
[ValidateRange(-90,90)] [ValidateRange(-90,90)]
[int]$TextRotation , [int]$TextRotation ,
#Autofit cells to width (columns or ranges only) #Autofit cells to width (columns or ranges only)
[switch]$AutoFit, [switch]$AutoFit,
#Set cells to a fixed width (columns or ranges only), ignored if Autofit is specified #Set cells to a fixed width (columns or ranges only), ignored if Autofit is specified
[float]$Width, [float]$Width,
#Set cells to a fixed hieght (rows or ranges only) #Set cells to a fixed hieght (rows or ranges only)
[float]$Height, [float]$Height,
#Hide a row or column (not a range) #Hide a row or column (not a range)
[switch]$Hidden [switch]$Hidden
) )
process { process {
Foreach ($range in $Address) { Foreach ($range in $Address) {
if ($ResetFont) {$Range.Style.Font.Color.SetColor("Black") if ($ResetFont) {$Range.Style.Font.Color.SetColor("Black")
$Range.Style.Font.Bold = $false $Range.Style.Font.Bold = $false
$Range.Style.Font.Italic = $false $Range.Style.Font.Italic = $false
$Range.Style.Font.UnderLine = $false $Range.Style.Font.UnderLine = $false
$Range.Style.Font.Strike = $falsee $Range.Style.Font.Strike = $false
} }
if ($Underline) {$Range.Style.Font.UnderLine = $true if ($Underline) {$Range.Style.Font.UnderLine = $true
$Range.Style.Font.UnderLineType =$UnderLineType $Range.Style.Font.UnderLineType =$UnderLineType
@@ -170,7 +170,7 @@ Function Set-Format {
if ($StrikeThru) {$Range.Style.Font.Strike = $true } if ($StrikeThru) {$Range.Style.Font.Strike = $true }
if ($FontShift) {$Range.Style.Font.VerticalAlign = $FontShift } if ($FontShift) {$Range.Style.Font.VerticalAlign = $FontShift }
if ($FontColor) {$Range.Style.Font.Color.SetColor( $FontColor ) } if ($FontColor) {$Range.Style.Font.Color.SetColor( $FontColor ) }
if ($BorderRound) {$Range.Style.Border.BorderAround( $BorderAround ) } if ($BorderAround) {$Range.Style.Border.BorderAround( $BorderAround ) }
if ($NumberFormat) {$Range.Style.Numberformat.Format= $NumberFormat } if ($NumberFormat) {$Range.Style.Numberformat.Format= $NumberFormat }
if ($TextRotation) {$Range.Style.TextRotation = $TextRotation } if ($TextRotation) {$Range.Style.TextRotation = $TextRotation }
if ($WrapText) {$Range.Style.WrapText = $true } if ($WrapText) {$Range.Style.WrapText = $true }
@@ -179,7 +179,7 @@ Function Set-Format {
if ($BackgroundColor) { if ($BackgroundColor) {
$Range.Style.Fill.PatternType = $BackgroundPattern $Range.Style.Fill.PatternType = $BackgroundPattern
$Range.Style.Fill.BackgroundColor.SetColor($BackgroundColor) $Range.Style.Fill.BackgroundColor.SetColor($BackgroundColor)
if ($PatternColor) { if ($PatternColor) {
$range.Style.Fill.PatternColor.SetColor( $PatternColor) $range.Style.Fill.PatternColor.SetColor( $PatternColor)
} }
@@ -191,13 +191,13 @@ Function Set-Format {
($range.Start.Row)..($range.Start.Row + $range.Rows) | ($range.Start.Row)..($range.Start.Row + $range.Rows) |
ForEach-Object {$ws.Row($_).Height = $Height } ForEach-Object {$ws.Row($_).Height = $Height }
} }
else {Write-Warning -Message ("Can set the height of a row or a range but not a {0} object" -f ($Range.GetType().name)) } else {Write-Warning -Message ("Can set the height of a row or a range but not a {0} object" -f ($Range.GetType().name)) }
} }
if ($AutoFit) { if ($AutoFit) {
if ($Range -is [OfficeOpenXml.ExcelColumn]) {$Range.AutoFit() } if ($Range -is [OfficeOpenXml.ExcelColumn]) {$Range.AutoFit() }
elseif ($Range -is [OfficeOpenXml.ExcelRange] ) {$Range.AutoFitColumns() } elseif ($Range -is [OfficeOpenXml.ExcelRange] ) {$Range.AutoFitColumns() }
else {Write-Warning -Message ("Can autofit a column or a range but not a {0} object" -f ($Range.GetType().name)) } else {Write-Warning -Message ("Can autofit a column or a range but not a {0} object" -f ($Range.GetType().name)) }
} }
elseif ($Width) { elseif ($Width) {
if ($Range -is [OfficeOpenXml.ExcelColumn]) {$Range.Width = $Width} if ($Range -is [OfficeOpenXml.ExcelColumn]) {$Range.Width = $Width}
@@ -205,7 +205,7 @@ Function Set-Format {
($range.Start.Column)..($range.Start.Column+ $range.Columns) | ($range.Start.Column)..($range.Start.Column+ $range.Columns) |
ForEach-Object {$ws.Column($_).Width = $Width} ForEach-Object {$ws.Column($_).Width = $Width}
} }
else {Write-Warning -Message ("Can set the width of a column or a range but not a {0} object" -f ($Range.GetType().name)) } else {Write-Warning -Message ("Can set the width of a column or a range but not a {0} object" -f ($Range.GetType().name)) }
} }
if ($Hidden) { if ($Hidden) {
if ($Range -is [OfficeOpenXml.ExcelRow] -or if ($Range -is [OfficeOpenXml.ExcelRow] -or
@@ -216,12 +216,12 @@ Function Set-Format {
} }
} }
#Argument completer for colours. If we have PS 5 or Tab expansion++ then we'll register it. Otherwise it does nothing. #Argument completer for colours. If we have PS 5 or Tab expansion++ then we'll register it. Otherwise it does nothing.
Function ColorCompletion{ Function ColorCompletion{
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
[System.Drawing.KnownColor].GetFields() | Where-Object {$_.IsStatic -and $_.name -like "$wordToComplete*" } | [System.Drawing.KnownColor].GetFields() | Where-Object {$_.IsStatic -and $_.name -like "$wordToComplete*" } |
Sort-Object name | ForEach-Object {New-CompletionResult $_.name $_.name Sort-Object name | ForEach-Object {New-CompletionResult $_.name $_.name
} }
} }
if (Get-Command -Name register-argumentCompleter -ErrorAction SilentlyContinue) { if (Get-Command -Name register-argumentCompleter -ErrorAction SilentlyContinue) {