From a4f0155735dbce21c687efe4fe322932dfa6555b Mon Sep 17 00:00:00 2001 From: frenchsomething Date: Tue, 15 Nov 2016 00:19:34 -0500 Subject: [PATCH 1/2] Update Format-Cell.ps1 to add height and width Adding parameters for height and width Will iterate through all selected rows and/or columns as applicable, and set height and width as defined --- PSExcel/Format-Cell.ps1 | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/PSExcel/Format-Cell.ps1 b/PSExcel/Format-Cell.ps1 index 166cac5..342df50 100644 --- a/PSExcel/Format-Cell.ps1 +++ b/PSExcel/Format-Cell.ps1 @@ -1,4 +1,4 @@ -function Format-Cell { +function Format-Cell { <# .SYNOPSIS Format cells in an Excel worksheet @@ -69,6 +69,12 @@ .PARAMETER AutoFitMaxWidth Maximum width to set autofit with + + .PARAMETER Height + If specified, override autofit preferences to apply the defined row height. + + .PARAMETER Width + If specified, override autofit preferences to apply the defined row width. .PARAMETER VerticalAlignment Set the vertical alignment @@ -189,6 +195,8 @@ [switch]$Autofit, [double]$AutofitMinWidth, [double]$AutofitMaxWidth, + [double]$Height, + [double]$Width, [OfficeOpenXml.Style.ExcelVerticalAlignment]$VerticalAlignment, [OfficeOpenXml.Style.ExcelHorizontalAlignment]$HorizontalAlignment, @@ -320,6 +328,28 @@ Write-Error $_ } } + 'Height' { + try + { + for($i=$StartRow;$i -le $EndRow; $i++) + {$WorkSheet.Row($i).Height = $Height } + } + Catch + { + Write-Error $_ + } + } + 'Width' { + try + { + for($i=$StartColumn;$i -le $EndColumn; $i++) + {$WorkSheet.Column($i).Width = $Width } + } + Catch + { + Write-Error $_ + } + } 'Border' { If($Border -eq '*') { @@ -348,4 +378,4 @@ $WorkSheet } } -} \ No newline at end of file +} From 8c30736e8bb9dce90c52d6e23683190d7ed1a5d3 Mon Sep 17 00:00:00 2001 From: Jared V Date: Tue, 15 Nov 2016 16:01:37 -0500 Subject: [PATCH 2/2] Adding Cell comment functionality The PSExcel comment methods only allow adding a comment to one cell at a time, so I had to iterate over all cells in an arbitrary cell range to avoid errors (and failure to apply comment to all cells) if more than one cell is specified. --- PSExcel/Format-Cell.ps1 | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/PSExcel/Format-Cell.ps1 b/PSExcel/Format-Cell.ps1 index 342df50..61470d1 100644 --- a/PSExcel/Format-Cell.ps1 +++ b/PSExcel/Format-Cell.ps1 @@ -70,11 +70,11 @@ function Format-Cell { .PARAMETER AutoFitMaxWidth Maximum width to set autofit with - .PARAMETER Height - If specified, override autofit preferences to apply the defined row height. + .PARAMETER Height + If specified, override autofit preferences to apply the defined row height. - .PARAMETER Width - If specified, override autofit preferences to apply the defined row width. + .PARAMETER Width + If specified, override autofit preferences to apply the defined row width. .PARAMETER VerticalAlignment Set the vertical alignment @@ -91,6 +91,12 @@ function Format-Cell { .PARAMETER BorderColor Color for the border. Defaults to Black + .PARAMETER Comment + Add a comment to the cell(s). You must include the comment and author + + .Parameter AutoFitComment + If specified, the comment box will autofit to the text supplied. Ignored if comment not specified + .PARAMETER Passthru If specified, pass the Worksheet back @@ -118,6 +124,9 @@ function Format-Cell { $WorkSheet | Format-Cell -StartRow 2 -StartColumn 1 -EndColumn 1 -Italic $True -Size 10 # Set the first column, rows 2 through the end to size 10, italic + .EXAMPLE + # Add a comment to the first 3 columns of the second row, and autofit the comment box to the included text. + $WorkSheet | Format-Cell -StartRow 2 -StartColumn 1 -EndColumn 3 -Comment "This is a comment on a cell.`nWe can even do multiple lines!","JohnSmith" -AutoFitComment .EXAMPLE @@ -187,6 +196,7 @@ function Format-Cell { [System.Drawing.KnownColor]$Color, [System.Drawing.KnownColor]$BackgroundColor, + [string[]]$Comment, [OfficeOpenXml.Style.ExcelFillStyle]$FillStyle, [boolean]$WrapText, [String]$NumberFormat, @@ -302,6 +312,18 @@ function Format-Cell { $CellRange.Style.Fill.PatternType = $FillStyle $CellRange.Style.Fill.BackgroundColor.SetColor($BackgroundColorConverted) } + 'Comment' { ForEach ($Row in $StartRow..$EndRow) { + ForEach ($Column in $StartColumn..$EndColumn) { + $CommentCell = ConvertTo-ExcelCoordinate -Row $Row -Column $Column + $NewComment = $WorkSheet.Cells["$CommentCell`:$CommentCell"].AddComment($Comment[0],$Comment[1]) + if($PSBoundParameters.ContainsKey('AutoFitComment')) + { + $NewComment.Autofit = $true + } + + } + } + } 'WrapText' { $CellRange.Style.WrapText = $WrapText } 'VerticalAlignment' { $CellRange.Style.VerticalAlignment = $VerticalAlignment } 'HorizontalAlignment' { $CellRange.Style.HorizontalAlignment = $HorizontalAlignment }