-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1305 from thkn-hofa/master
TotalSettings improvements
- Loading branch information
Showing
15 changed files
with
3,994 additions
and
4,793 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Scope='Function', Target='Set*', Justification='Does not change system state')] | ||
param() | ||
|
||
function Set-CellComment { | ||
[CmdletBinding(DefaultParameterSetName = "Range")] | ||
param( | ||
[Parameter(Mandatory = $True, ParameterSetName = "ColumnLetter")] | ||
[Parameter(Mandatory = $True, ParameterSetName = "ColumnNumber")] | ||
[Parameter(Mandatory = $False, ParameterSetName = "Range")] | ||
[OfficeOpenXml.ExcelWorkSheet]$Worksheet, | ||
|
||
[Parameter(Mandatory = $True, ParameterSetName = "Range", ValueFromPipeline = $true,Position=0)] | ||
[Alias("Address")] | ||
$Range, | ||
|
||
[Parameter(Mandatory = $True, ParameterSetName = "ColumnLetter")] | ||
[Parameter(Mandatory = $True, ParameterSetName = "ColumnNumber")] | ||
[Int]$Row, | ||
|
||
[Parameter(Mandatory = $True, ParameterSetName = "ColumnLetter")] | ||
[String]$ColumnLetter, | ||
|
||
[Parameter(Mandatory = $True, ParameterSetName = "ColumnNumber")] | ||
[Int]$ColumnNumber, | ||
|
||
[Parameter(Mandatory = $True)] | ||
[String]$Text | ||
) | ||
|
||
If ($PSCmdlet.ParameterSetName -eq "Range") { | ||
Write-Verbose "Using 'Range' Parameter Set" | ||
if ($Range -is [Array]) { | ||
$null = $PSBoundParameters.Remove("Range") | ||
$Range | Set-CellComment @PSBoundParameters | ||
} | ||
else { | ||
#We should accept, a worksheet and a name of a range or a cell address; a table; the address of a table; a named range; a row, a column or .Cells[ ] | ||
if ($Range -is [OfficeOpenXml.Table.ExcelTable]) {$Range = $Range.Address} | ||
elseif ($Worksheet -and $Range -is [string]) { | ||
# Convert range as string to OfficeOpenXml.ExcelAddress | ||
$Range = [OfficeOpenXml.ExcelAddress]::new($Range) | ||
} | ||
elseif ($Range -is [string]) {Write-Warning -Message "The range parameter you have specified also needs a worksheet parameter." ;return} | ||
#else we assume $Range is a OfficeOpenXml.ExcelAddress | ||
} | ||
} | ||
ElseIf ($PSCmdlet.ParameterSetName -eq "ColumnNumber") { | ||
$Range = [OfficeOpenXml.ExcelAddress]::new($Row, $ColumnNumber, $Row, $ColumnNumber) | ||
} | ||
ElseIf ($PSCmdlet.ParameterSetName -eq "ColumnLetter") { | ||
$Range = [OfficeOpenXml.ExcelAddress]::new(("{0}{1}" -f $ColumnLetter,$Row)) | ||
} | ||
|
||
If ($Range -isnot [Array]) { | ||
Foreach ($c in $Worksheet.Cells[$Range]) { | ||
write-verbose $c.address | ||
Try { | ||
If ($Null -eq $c.comment) { | ||
[Void]$c.AddComment($Text, "ImportExcel") | ||
} | ||
Else { | ||
$c.Comment.Text = $Text | ||
$c.Comment.Author = "ImportExcel" | ||
} | ||
$c.Comment.AutoFit = $True | ||
} | ||
Catch { "Could not add comment to cell {0}: {1}" -f $c.Address, $_.ToString() } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
if (-not (Get-command Import-Excel -ErrorAction SilentlyContinue)) { | ||
Import-Module $PSScriptRoot\..\ImportExcel.psd1 | ||
} | ||
|
||
Describe "Test setting comment on cells in different ways" -Tag SetCellComment { | ||
BeforeAll { | ||
$data = ConvertFrom-Csv @" | ||
OrderId,Category,Sales,Quantity,Discount | ||
1,Cosmetics,744.01,07,0.7 | ||
2,Grocery,349.13,25,0.3 | ||
3,Apparels,535.11,88,0.2 | ||
4,Electronics,524.69,60,0.1 | ||
5,Electronics,439.10,41,0.0 | ||
6,Apparels,56.84,54,0.8 | ||
7,Electronics,326.66,97,0.7 | ||
8,Cosmetics,17.25,74,0.6 | ||
9,Grocery,199.96,39,0.4 | ||
10,Grocery,731.77,20,0.3 | ||
"@ | ||
|
||
$Excel = $data | Export-Excel -PassThru | ||
$ws = $Excel.Workbook.Worksheets | Select-Object -First 1 | ||
} | ||
|
||
AfterAll { | ||
Close-ExcelPackage $Excel | ||
} | ||
|
||
It "Should add comments to multiple cells".PadRight(87) { | ||
Set-CellComment -Range "A1" -Worksheet $ws -Text "This was added with a single cell range" | ||
Set-CellComment -Range "A2:C2" -Worksheet $ws -Text "This was added with a multiple cell range" | ||
Set-CellComment -ColumnLetter A -Row 3 -Worksheet $ws -Text "This was added using a column letter and rownumber" | ||
Set-CellComment -ColumnNumber 1 -Row 4 -Worksheet $ws -Text "This was added using a column number and row number" | ||
|
||
Set-CellComment -Range "B2" -Worksheet $ws -Text "This demonstrates an overwrite of a previously set comment" | ||
|
||
$ws.Cells["A1"].Comment.Text | Should -BeExactly "This was added with a single cell range" | ||
$ws.Cells["A2"].Comment.Text | Should -BeExactly "This was added with a multiple cell range" | ||
$ws.Cells["B2"].Comment.Text | Should -BeExactly "This demonstrates an overwrite of a previously set comment" | ||
$ws.Cells["C2"].Comment.Text | Should -BeExactly "This was added with a multiple cell range" | ||
$ws.Cells["A3"].Comment.Text | Should -BeExactly "This was added using a column letter and rownumber" | ||
$ws.Cells["A4"].Comment.Text | Should -BeExactly "This was added using a column number and row number" | ||
} | ||
} |
Oops, something went wrong.