Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions PSExcel/Format-Cell.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Format-Cell {
function Format-Cell {
<#
.SYNOPSIS
Format cells in an Excel worksheet
Expand Down Expand Up @@ -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
Expand All @@ -85,6 +91,12 @@
.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

Expand Down Expand Up @@ -112,6 +124,9 @@
$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

Expand Down Expand Up @@ -181,6 +196,7 @@

[System.Drawing.KnownColor]$Color,
[System.Drawing.KnownColor]$BackgroundColor,
[string[]]$Comment,
[OfficeOpenXml.Style.ExcelFillStyle]$FillStyle,
[boolean]$WrapText,
[String]$NumberFormat,
Expand All @@ -189,6 +205,8 @@
[switch]$Autofit,
[double]$AutofitMinWidth,
[double]$AutofitMaxWidth,
[double]$Height,
[double]$Width,

[OfficeOpenXml.Style.ExcelVerticalAlignment]$VerticalAlignment,
[OfficeOpenXml.Style.ExcelHorizontalAlignment]$HorizontalAlignment,
Expand Down Expand Up @@ -294,6 +312,18 @@
$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 }
Expand All @@ -320,6 +350,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 '*')
{
Expand Down Expand Up @@ -348,4 +400,4 @@
$WorkSheet
}
}
}
}