Argument completers for worksheet name & number format. Fixed bug with -hidden

This commit is contained in:
jhoneill
2018-07-31 15:07:33 +01:00
parent 61c73461ca
commit 77fb51da8d
11 changed files with 405 additions and 95 deletions

View File

@@ -122,7 +122,7 @@
$rule.Formula2 = $ConditionValue2
}
if ($NumberFormat) {$rule.Style.NumberFormat.Format = $NumberFormat }
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 }

View File

@@ -598,7 +598,8 @@
}
else { $Row = $StartRow }
$ColumnIndex = $StartColumn
$setNumformat = ($numberformat -ne $ws.Cells.Style.Numberformat.Format)
$Numberformat = Expand-NumberFormat -NumberFormat $Numberformat
$setNumformat = ($Numberformat -ne $ws.Cells.Style.Numberformat.Format)
$firstTimeThru = $true
$isDataTypeValueType = $false
@@ -1337,7 +1338,7 @@ function Add-ExcelChart {
if ($XMinorUnit) {$chart.XAxis.MinorUnit = $XMinorUnit}
if ($null -ne $XMinValue) {$chart.XAxis.MinValue = $XMinValue}
if ($null -ne $XMaxValue) {$chart.XAxis.MaxValue = $XMaxValue}
if ($XAxisNumberformat) {$chart.XAxis.Format = $XAxisNumberformat}
if ($XAxisNumberformat) {$chart.XAxis.Format = (Expand-NumberFormat $XAxisNumberformat)}
if ($YAxisTitleText) {
$chart.YAxis.Title.Text = $YAxisTitleText
@@ -1349,7 +1350,7 @@ function Add-ExcelChart {
if ($YMinorUnit) {$chart.YAxis.MinorUnit = $YMinorUnit}
if ($null -ne $YMinValue){$chart.YAxis.MinValue = $YMinValue}
if ($null -ne $YMaxValue){$chart.YAxis.MaxValue = $YMaxValue}
if ($YAxisNumberformat) {$chart.YAxis.Format = $YAxisNumberformat}
if ($YAxisNumberformat) {$chart.YAxis.Format = (Expand-NumberFormat $YAxisNumberformat)}
if ($null -ne $chart.Datalabel) {
$chart.Datalabel.ShowCategory = [boolean]$ShowCategory
$chart.Datalabel.ShowPercent = [boolean]$ShowPercent

View File

@@ -512,3 +512,20 @@ function Export-MultipleExcelSheets {
if($Show) {Invoke-Item $Path}
}
Function WorksheetArgumentCompleter {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$xlPath = $fakeBoundParameter['Path']
if (Test-Path -Path $xlPath) {
$xlpkg = Open-ExcelPackage -Path $xlPath
$WorksheetNames = $xlPkg.Workbook.Worksheets.Name
Close-ExcelPackage -nosave -ExcelPackage $xlpkg
$WorksheetNames.where({$_ -like "*$wordToComplete*"}) | foreach-object {
New-Object -TypeName System.Management.Automation.CompletionResult -ArgumentList "'$_'",
$_ , ([System.Management.Automation.CompletionResultType]::ParameterValue) ,$_
}
}
}
If (Get-Command -ErrorAction SilentlyContinue -name Register-ArgumentCompleter) {
Register-ArgumentCompleter -CommandName 'Import-Excel' -ParameterName 'WorksheetName' -ScriptBlock $Function:WorksheetArgumentCompleter
}

View File

@@ -111,6 +111,18 @@
}
else { $cellData = $Value}
if ($cellData -match "^=") { $Worksheet.Cells[$Row, $Column].Formula = $cellData }
elseif ( [System.Uri]::IsWellFormedUriString($cellData , [System.UriKind]::Absolute)) {
# Save a hyperlink : internal links can be in the form xl://sheet!E419 (use A1 as goto sheet), or xl://RangeName
if ($cellData -match "^xl://internal/") {
$referenceAddress = $cellData -replace "^xl://internal/" , ""
$display = $referenceAddress -replace "!A1$" , ""
$h = New-Object -TypeName OfficeOpenXml.ExcelHyperLink -ArgumentList $referenceAddress , $display
$Worksheet.Cells[$Row, $Column].HyperLink = $h
}
else {$Worksheet.Cells[$Row, $Column].HyperLink = $cellData }
$Worksheet.Cells[$Row, $Column].Style.Font.Color.SetColor([System.Drawing.Color]::Blue)
$Worksheet.Cells[$Row, $Column].Style.Font.UnderLine = $true
}
else { $Worksheet.Cells[$Row, $Column].Value = $cellData }
if ($cellData -is [datetime]) { $Worksheet.Cells[$Row, $Column].Style.Numberformat.Format = 'm/d/yy h:mm' } # This is not a custom format, but a preset recognized as date and localized.
}}

View File

@@ -32,7 +32,7 @@
$Row = 0 ,
#Position in the row to start from
[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 $worksheet, $row, $column [number], $ColumnName [letter(s)], $startRow, $startColumn, $endRow, $endColumn
[parameter(Mandatory=$true)]
$Value,
#Optional Row heading
@@ -110,6 +110,18 @@
}
else{$cellData = $Value}
if ($cellData -match "^=") { $Worksheet.Cells[$Row, $column].Formula = $cellData }
elseif ( [System.Uri]::IsWellFormedUriString($cellData , [System.UriKind]::Absolute)) {
# Save a hyperlink : internal links can be in the form xl://sheet!E419 (use A1 as goto sheet), or xl://RangeName
if ($cellData -match "^xl://internal/") {
$referenceAddress = $cellData -replace "^xl://internal/" , ""
$display = $referenceAddress -replace "!A1$" , ""
$h = New-Object -TypeName OfficeOpenXml.ExcelHyperLink -ArgumentList $referenceAddress , $display
$Worksheet.Cells[$Row, $Column].HyperLink = $h
}
else {$Worksheet.Cells[$Row, $Column].HyperLink = $cellData }
$Worksheet.Cells[$Row, $Column].Style.Font.Color.SetColor([System.Drawing.Color]::Blue)
$Worksheet.Cells[$Row, $Column].Style.Font.UnderLine = $true
}
else { $Worksheet.Cells[$Row, $column].Value = $cellData }
if ($cellData -is [datetime]) { $Worksheet.Cells[$Row, $column].Style.Numberformat.Format = 'm/d/yy h:mm' } # This is not a custom format, but a preset recognized as date and localized.
}}

View File

@@ -120,9 +120,6 @@
if ($PSBoundParameters.ContainsKey('FontColor')){
$Address.Style.Font.Color.SetColor( $FontColor)
}
if ($PSBoundParameters.ContainsKey('NumberFormat')) {
$Address.Style.Numberformat.Format = $NumberFormat
}
if ($PSBoundParameters.ContainsKey('TextRotation')) {
$Address.Style.TextRotation = $TextRotation
}
@@ -136,11 +133,21 @@
$Address.Style.VerticalAlignment = $VerticalAlignment
}
if ($PSBoundParameters.ContainsKey('Value')) {
if ($Value -like '=*') {$Address.Formula = $Value}
else {
$Address.Value = $Value
if ($Value -is [DateTime]) {
$Address.Style.Numberformat.Format = 'm/d/yy h:mm' # This is not a custom format, but a preset recognized as date and localized. It might be overwritten in a moment
}
}
}
if ($PSBoundParameters.ContainsKey('Formula')) {
$Address.Formula = $Formula
}
if ($PSBoundParameters.ContainsKey('NumberFormat')) {
$Address.Style.Numberformat.Format = (Expand-NumberFormat $NumberFormat)
}
if ($PSBoundParameters.ContainsKey('BorderAround')) {
$Address.Style.Border.BorderAround($BorderAround, $BorderColor)
}
@@ -194,7 +201,7 @@
}
else {Write-Warning -Message ("Can set the width of a column or a range but not a {0} object" -f ($Address.GetType().name)) }
}
if ($PSBoundParameters.ContainsKey('$Hidden')) {
if ($PSBoundParameters.ContainsKey('Hidden')) {
if ($Address -is [OfficeOpenXml.ExcelRow] -or
$Address -is [OfficeOpenXml.ExcelColumn] ) {$Address.Hidden = [boolean]$Hidden}
else {Write-Warning -Message ("Can hide a row or a column but not a {0} object" -f ($Address.GetType().name)) }
@@ -203,3 +210,70 @@
}
}
}
Function NumberFormatCompletion {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$numformats = [ordered]@{
"General" = "General" # format ID 0
"Number" = "0.00" # format ID 2
"Percentage" = "0.00%" # format ID 10
"Scientific" = "0.00E+00" # format ID 11
"Fraction" = "# ?/?" # format ID 12
"Short Date" = "Localized" # format ID 14 - will be translated to "mm-dd-yy" which is localized on load by Excel.
"Short Time" = "Localized" # format ID 20 - will be translated to "h:mm" which is localized on load by Excel.
"Long Time" = "Localized" # format ID 21 - will be translated to "h:mm:ss" which is localized on load by Excel.
"Date-Time" = "Localized" # format ID 22 - will be translated to "m/d/yy h:mm" which is localized on load by Excel.
"Currency" = [cultureinfo]::CurrentCulture.NumberFormat.CurrencySymbol + "#,##0.00"
"Text" = "@" # format ID 49
"h:mm AM/PM" = "h:mm AM/PM" # format ID 18
"h:mm:ss AM/PM" = "h:mm:ss AM/PM" # format ID 19
"mm:ss" = "mm:ss" # format ID 45
"[h]:mm:ss" = "Elapsed hours" # format ID 46
"mm:ss.0" = "mm:ss.0" # format ID 47
"d-mmm-yy" = "Localized" # format ID 15 which is localized on load by Excel.
"d-mmm" = "Localized" # format ID 16 which is localized on load by Excel.
"mmm-yy" = "mmm-yy" # format ID 17 which is localized on load by Excel.
"0" = "Whole number" # format ID 1
"0.00" = "Number, 2 decimals" # format ID 2 or "number"
"#,##0" = "Thousand separators" # format ID 3
"#,##0.00" = "Thousand separators and 2 decimals" # format ID 4
"#," = "Whole thousands"
"#.0,," = "Millions, 1 Decimal"
"0%" = "Nearest whole percentage" # format ID 9
"0.00%" = "Percentage with decimals" # format ID 10 or "Percentage"
"00E+00" = "Scientific" # format ID 11 or "Scientific"
"# ?/?" = "One Digit fraction" # format ID 12 or "Fraction"
"# ??/??" = "Two Digit fraction" # format ID 13
"@" = "Text" # format ID 49 or "Text"
}
$numformats.keys.where({$_ -like "$wordToComplete*"} ) | ForEach-Object {
New-Object -TypeName System.Management.Automation.CompletionResult -ArgumentList "'$_'" , $_ ,
([System.Management.Automation.CompletionResultType]::ParameterValue) , $numformats[$_]
}
}
if (Get-Command -ErrorAction SilentlyContinue -name Register-ArgumentCompleter) {
Register-ArgumentCompleter -CommandName Add-ConditionalFormatting -ParameterName NumberFormat -ScriptBlock $Function:NumberFormatCompletion
Register-ArgumentCompleter -CommandName Export-Excel -ParameterName NumberFormat -ScriptBlock $Function:NumberFormatCompletion
Register-ArgumentCompleter -CommandName Set-Format -ParameterName NumberFormat -ScriptBlock $Function:NumberFormatCompletion
Register-ArgumentCompleter -CommandName Set-Column -ParameterName NumberFormat -ScriptBlock $Function:NumberFormatCompletion
Register-ArgumentCompleter -CommandName Set-Row -ParameterName NumberFormat -ScriptBlock $Function:NumberFormatCompletion
Register-ArgumentCompleter -CommandName New-ExcelChartDefinition -ParameterName XAxisNumberformat -ScriptBlock $Function:NumberFormatCompletion
Register-ArgumentCompleter -CommandName New-ExcelChartDefinition -ParameterName YAxisNumberformat -ScriptBlock $Function:NumberFormatCompletion
}
Function Expand-NumberFormat {
param ($NumberFormat)
switch ($NumberFormat) {
"Currency" {return [cultureinfo]::CurrentCulture.NumberFormat.CurrencySymbol + "#,##0.00"}
"Number" {return "0.00" } # format id 2
"Percentage" {return "0.00%" } # format id 10
"Scientific" {return "0.00E+00" } # format id 11
"Fraction" {return "# ?/?" } # format id 12
"Short Date" {return "mm-dd-yy" } # format id 14 localized on load by Excel.
"Short Time" {return "h:mm" } # format id 20 localized on load by Excel.
"Long Time" {return "h:mm:ss" } # format id 21 localized on load by Excel.
"Date-Time" {return "m/d/yy h:mm"} # format id 22 localized on load by Excel.
"Text" {return "@" } # format ID 49
Default {return $NumberFormat}
}
}

View File

@@ -235,7 +235,7 @@ Describe "Merge Worksheet" {
}
}
Context "Wider data set" {
it "Copes with more columns beyond Z in the Output sheet " {
it "Coped with columns beyond Z in the Output sheet " {
{ Merge-Worksheet -Referencefile "$env:temp\server1.xlsx" -Differencefile "$env:temp\Server2.xlsx" -OutputFile "$env:temp\combined2.xlsx" } | Should not throw
}
}

View File

@@ -37,7 +37,7 @@ Describe "Copy-Worksheet" {
$excel = Open-ExcelPackage -Path $path2
$ws = $excel.Workbook.Worksheets["Processes"]
}
it "inserted a worksheet " {
it "Inserted a worksheet " {
$Excel.Workbook.Worksheets.count | Should be 2
$ws | Should not benullorEmpty
$ws.Dimension.Address | should be $ProcRange
@@ -50,7 +50,7 @@ Describe "Copy-Worksheet" {
$excel = Open-ExcelPackage -Path $path2
$ws = $Excel.Workbook.Worksheets[3]
}
it "inserted a worksheet with the expected name, number of rows and number of columns " {
it "Inserted a worksheet with the expected name, number of rows and number of columns " {
$Excel.Workbook.Worksheets.count | Should be 3
$ws | Should not benullorEmpty
$ws.Name | Should be "CopyOfMixedTypes"

View File

@@ -50,7 +50,7 @@ Describe ExportExcel {
}
}
it "Formatted the process StartTime field as 'local short date' " {
it "Formatted the process StartTime field as 'localized Date-Time' " {
$STHeader = $ws.cells["1:1"].where( {$_.Value -eq "StartTime"})[0]
$STCell = $STHeader.Address -replace '1$', '2'
$ws.cells[$stcell].Style.Numberformat.NumFmtID | Should be 22
@@ -605,7 +605,7 @@ Describe ExportExcel {
$rule = Add-ConditionalFormatting -passthru -Address $sheet.cells["C:C"] -RuleType TopPercent -ConditionValue 20 -Bold -StrikeThru
Add-ConditionalFormatting -WorkSheet $sheet -Range "G2:G1048576" -RuleType GreaterThan -ConditionValue "104857600" -ForeGroundColor Red -Bold -Italic -Underline -BackgroundColor Beige -BackgroundPattern LightUp -PatternColor Gray
foreach ($c in 5..9) {Set-Format $sheet.Column($c) -AutoFit }
Add-PivotTable -PivotTableName "PT_Procs" -ExcelPackage $excel -SourceWorkSheet "Processes" -PivotRows Company -PivotData @{'Name' = 'Count'} -IncludePivotChart -ChartType ColumnClustered -NoLegend
Add-PivotTable -PivotTableName "PT_Procs" -ExcelPackage $excel -SourceWorkSheet 1 -PivotRows Company -PivotData @{'Name' = 'Count'} -IncludePivotChart -ChartType ColumnClustered -NoLegend
Close-ExcelPackage $excel
$excel = Open-ExcelPackage $path

View File

@@ -31,12 +31,26 @@ Describe "Join Worksheet" {
$data3 | Export-Excel -Path $path -WorkSheetname Banbury
$ptdef = New-PivotTableDefinition -PivotTableName "Summary" -PivotRows "Store" -PivotColumns "Product" -PivotData @{"Total"="SUM"} -IncludePivotChart -ChartTitle "Sales Breakdown" -ChartType ColumnStacked -ChartColumn 10
Join-Worksheet -Path $path -WorkSheetName "Total" -Clearsheet -FromLabel "Store" -TableName "Summary" -TableStyle Light1 -AutoSize -BoldTopRow -FreezePane 2,1 -Title "Store Sales Summary" -TitleBold -TitleSize 14 -PivotTableDefinition $ptdef
$excel = Open-ExcelPackage -Path $path
$excel = Export-Excel -path $path -WorkSheetname Total -HideSheet * -UnHideSheet "Total","Summary" -PassThru
# Open-ExcelPackage -Path $path
$ws = $excel.Workbook.Worksheets["Total"]
$pt = $excel.Workbook.Worksheets["Summary"].pivottables[0]
$pc = $excel.Workbook.Worksheets["Summary"].Drawings[0]
}
Context "Merge 3 blocks" {
Context "Export-Excel set spreadsheet visibility" {
it "Hid the worksheets " {
$excel.Workbook.Worksheets["Oxford"].Hidden | Should be $true
$excel.Workbook.Worksheets["Banbury"].Hidden | Should be $true
$excel.Workbook.Worksheets["Abingdon"].Hidden | Should be $true
}
it "Un-hid two of the worksheets " {
$excel.Workbook.Worksheets["Total"].Hidden | Should be $false
$excel.Workbook.Worksheets["Summary"].Hidden | Should be $false
}
}
Context "Merging 3 blocks" {
it "Created sheet of the right size with a title and a table " {
$ws.Dimension.Address | Should be "A1:F16"
$ws.Tables[0].Address.Address | Should be "A2:F16"
@@ -78,24 +92,24 @@ Describe "Join Worksheet" {
Join-Worksheet -Path $path -HideSource -WorkSheetName Summary -NoHeader -LabelBlocks -AutoSize -Title "Summary" -TitleBold -TitleSize 22
$excel = Open-ExcelPackage -Path $path
$ws = $excel.Workbook.Worksheets["Summary"]
Context "3 Unlinked blocks" {
Context "Bringing 3 Unlinked blocks onto one page" {
it "Hid the source worksheets " {
$excel.Workbook.Worksheets[1].Hidden.tostring() | should be "Hidden"
$excel.Workbook.Worksheets[2].Hidden.tostring() | should be "Hidden"
$excel.Workbook.Worksheets[1].Hidden.tostring() | Should be "Hidden"
$excel.Workbook.Worksheets[2].Hidden.tostring() | Should be "Hidden"
}
it "Created the Summary sheet with title, and block labels, and copied the correct data " {
$ws.Cells["A1"].Value | should be "Summary"
$ws.Cells["A2"].Value | should be $excel.Workbook.Worksheets[1].name
$ws.Cells["A3"].Value | should be $excel.Workbook.Worksheets[1].Cells["A1"].value
$ws.Cells["A4"].Value | should be $excel.Workbook.Worksheets[1].Cells["A2"].value
$ws.Cells["B4"].Value | should be $excel.Workbook.Worksheets[1].Cells["B2"].value
$ws.Cells["A1"].Value | Should be "Summary"
$ws.Cells["A2"].Value | Should be $excel.Workbook.Worksheets[1].name
$ws.Cells["A3"].Value | Should be $excel.Workbook.Worksheets[1].Cells["A1"].value
$ws.Cells["A4"].Value | Should be $excel.Workbook.Worksheets[1].Cells["A2"].value
$ws.Cells["B4"].Value | Should be $excel.Workbook.Worksheets[1].Cells["B2"].value
$nextRow = $excel.Workbook.Worksheets[1].Dimension.Rows + 3
$ws.Cells["A$NextRow"].Value | should be $excel.Workbook.Worksheets[2].name
$ws.Cells["A$NextRow"].Value | Should be $excel.Workbook.Worksheets[2].name
$nextRow ++
$ws.Cells["A$NextRow"].Value | should be $excel.Workbook.Worksheets[2].Cells["A1"].value
$ws.Cells["A$NextRow"].Value | Should be $excel.Workbook.Worksheets[2].Cells["A1"].value
$nextRow ++
$ws.Cells["A$NextRow"].Value | should be $excel.Workbook.Worksheets[2].Cells["A2"].value
$ws.Cells["B$NextRow"].Value | should be $excel.Workbook.Worksheets[2].Cells["B2"].value
$ws.Cells["A$NextRow"].Value | Should be $excel.Workbook.Worksheets[2].Cells["A2"].value
$ws.Cells["B$NextRow"].Value | Should be $excel.Workbook.Worksheets[2].Cells["B2"].value
}
}

View File

@@ -1,6 +1,5 @@

$path = "$Env:TEMP\test.xlsx"
Remove-Item -Path $path -ErrorAction SilentlyContinue
$data = ConvertFrom-Csv -InputObject @"
ID,Product,Quantity,Price
@@ -11,8 +10,129 @@ ID,Product,Quantity,Price
12011,Crowbar,7,23.48
"@
$DriverData = convertFrom-CSv @"
Name,Wikipage,DateOfBirth
Fernando Alonso,/wiki/Fernando_Alonso,1981-07-29
Jenson Button,/wiki/Jenson_Button,1980-01-19
Kimi Räikkönen,/wiki/Kimi_R%C3%A4ikk%C3%B6nen,1979-10-17
Lewis Hamilton,/wiki/Lewis_Hamilton,1985-01-07
Nico Rosberg,/wiki/Nico_Rosberg,1985-06-27
Sebastian Vettel,/wiki/Sebastian_Vettel,1987-07-03
"@ | ForEach-Object {$_.DateOfBirth = [datetime]$_.DateofBirth; $_ }
Describe "Number format expansion and setting" {
Context "Argmument Completer for NumberFormat" {
it "Returned at least 20 items " {
(NumberFormatCompletion ).count | Should beGreaterThan 20
}
It "Resolved percent to 'percentage' " {
$x = (NumberFormatCompletion -wordToComplete Percent)
$x.count | Should be 1
$x.CompletionText | Should match "^'.*'$"
$x.ToolTip | Should be "0.00%"
$x.ListItemText | Should be "Percentage"
}
}
Context "Expand-NumberFormat function" {
It "Expanded named number formats as expected " {
Expand-NumberFormat 'Number' | Should be "0.00"
Expand-NumberFormat 'Percentage' | Should be "0.00%"
Expand-NumberFormat 'Scientific' | Should be "0.00E+00"
Expand-NumberFormat 'Currency' | Should be ([cultureinfo]::CurrentCulture.NumberFormat.CurrencySymbol + "#,##0.00")
Expand-NumberFormat 'Fraction' | Should be "# ?/?"
Expand-NumberFormat 'Short Date' | Should be "mm-dd-yy"
Expand-NumberFormat 'Short Time' | Should be "h:mm"
Expand-NumberFormat 'Long Time' | Should be "h:mm:ss"
Expand-NumberFormat 'Date-Time' | Should be "m/d/yy h:mm"
Expand-NumberFormat 'Text' | Should be "@"
}
}
Context "Apply-NumberFormat" {
BeforeAll {
Remove-Item -Path $path -ErrorAction SilentlyContinue
$n = [datetime]::Now.ToOADate()
$excel = 1..32 | ForEach-Object {$n} | Export-Excel -Path $path -PassThru
$ws = $excel.Workbook.Worksheets[1]
Set-Format -WorkSheet $ws -Range "A1" -numberFormat 'General'
Set-Format -WorkSheet $ws -Range "A2" -numberFormat 'Number'
Set-Format -WorkSheet $ws -Range "A3" -numberFormat 'Percentage'
Set-Format -WorkSheet $ws -Range "A4" -numberFormat 'Scientific'
Set-Format -WorkSheet $ws -Range "A5" -numberFormat 'Fraction'
Set-Format -WorkSheet $ws -Range "A6" -numberFormat 'Short Date'
Set-Format -WorkSheet $ws -Range "A7" -numberFormat 'Short Time'
Set-Format -WorkSheet $ws -Range "A8" -numberFormat 'Long Time'
Set-Format -WorkSheet $ws -Range "A9" -numberFormat 'Date-Time'
Set-Format -WorkSheet $ws -Range "A10" -numberFormat 'Currency'
Set-Format -WorkSheet $ws -Range "A11" -numberFormat 'Text'
Set-Format -WorkSheet $ws -Range "A12" -numberFormat 'h:mm AM/PM'
Set-Format -WorkSheet $ws -Range "A13" -numberFormat 'h:mm:ss AM/PM'
Set-Format -WorkSheet $ws -Range "A14" -numberFormat 'mm:ss'
Set-Format -WorkSheet $ws -Range "A15" -numberFormat '[h]:mm:ss'
Set-Format -WorkSheet $ws -Range "A16" -numberFormat 'mmss.0'
Set-Format -WorkSheet $ws -Range "A17" -numberFormat 'd-mmm-yy'
Set-Format -WorkSheet $ws -Range "A18" -numberFormat 'd-mmm'
Set-Format -WorkSheet $ws -Range "A19" -numberFormat 'mmm-yy'
Set-Format -WorkSheet $ws -Range "A20" -numberFormat '0'
Set-Format -WorkSheet $ws -Range "A21" -numberFormat '0.00'
Set-Format -Address $ws.Cells[ "A22"] -NumberFormat '#,##0'
Set-Format -Address $ws.Cells[ "A23"] -NumberFormat '#,##0.00'
Set-Format -Address $ws.Cells[ "A24"] -NumberFormat '#,'
Set-Format -Address $ws.Cells[ "A25"] -NumberFormat '#.0,,'
Set-Format -Address $ws.Cells[ "A26"] -NumberFormat '0%'
Set-Format -Address $ws.Cells[ "A27"] -NumberFormat '0.00%'
Set-Format -Address $ws.Cells[ "A28"] -NumberFormat '0.00E+00'
Set-Format -Address $ws.Cells[ "A29"] -NumberFormat '# ?/?'
Set-Format -Address $ws.Cells[ "A30"] -NumberFormat '# ??/??'
Set-Format -Address $ws.Cells[ "A31"] -NumberFormat '@'
Close-ExcelPackage -ExcelPackage $excel
$excel = Open-ExcelPackage -Path $path
$ws = $excel.Workbook.Worksheets[1]
}
It "Set formats which translate to the correct format ID " {
$ws.Cells[10,1].Style.Numberformat.format | # Set as "Currency"
Should match ("^" + ([regex]::Escape([cultureinfo]::CurrentCulture.NumberFormat.CurrencySymbol)))
$ws.Cells[ 1,1].Style.Numberformat.NumFmtID | Should be 0 # Set as General
$ws.Cells[20,1].Style.Numberformat.NumFmtID | Should be 1 # Set as 0
$ws.Cells[ 2,1].Style.Numberformat.NumFmtID | Should be 2 # Set as "Number"
$ws.Cells[21,1].Style.Numberformat.NumFmtID | Should be 2 # Set as 0.00
$ws.Cells[22,1].Style.Numberformat.NumFmtID | Should be 3 # Set as #,##0
$ws.Cells[23,1].Style.Numberformat.NumFmtID | Should be 4 # Set as #,##0.00
$ws.Cells[26,1].Style.Numberformat.NumFmtID | Should be 9 # Set as 0%
$ws.Cells[27,1].Style.Numberformat.NumFmtID | Should be 10 # Set as 0.00%
$ws.Cells[ 3,1].Style.Numberformat.NumFmtID | Should be 10 # Set as "Percentage"
$ws.Cells[28,1].Style.Numberformat.NumFmtID | Should be 11 # Set as 0.00E+00
$ws.Cells[ 4,1].Style.Numberformat.NumFmtID | Should be 11 # Set as "Scientific"
$ws.Cells[ 5,1].Style.Numberformat.NumFmtID | Should be 12 # Set as "Fraction"
$ws.Cells[29,1].Style.Numberformat.NumFmtID | Should be 12 # Set as # ?/?
$ws.Cells[30,1].Style.Numberformat.NumFmtID | Should be 13 # Set as # ??/?
$ws.Cells[ 6,1].Style.Numberformat.NumFmtID | Should be 14 # Set as "Short date"
$ws.Cells[17,1].Style.Numberformat.NumFmtID | Should be 15 # Set as d-mmm-yy
$ws.Cells[18,1].Style.Numberformat.NumFmtID | Should be 16 # Set as d-mmm
$ws.Cells[19,1].Style.Numberformat.NumFmtID | Should be 17 # Set as mmm-yy
$ws.Cells[12,1].Style.Numberformat.NumFmtID | Should be 18 # Set as h:mm AM/PM
$ws.Cells[13,1].Style.Numberformat.NumFmtID | Should be 19 # Set as h:mm:ss AM/PM
$ws.Cells[ 7,1].Style.Numberformat.NumFmtID | Should be 20 # Set as "Short time"
$ws.Cells[ 8,1].Style.Numberformat.NumFmtID | Should be 21 # Set as "Long time"
$ws.Cells[ 9,1].Style.Numberformat.NumFmtID | Should be 22 # Set as "Date-time"
$ws.Cells[14,1].Style.Numberformat.NumFmtID | Should be 45 # Set as mm:ss
$ws.Cells[15,1].Style.Numberformat.NumFmtID | Should be 46 # Set as [h]:mm:ss
$ws.Cells[16,1].Style.Numberformat.NumFmtID | Should be 47 # Set as mmss.0
$ws.Cells[11,1].Style.Numberformat.NumFmtID | Should be 49 # Set as "Text"
$ws.Cells[31,1].Style.Numberformat.NumFmtID | Should be 49 # Set as @
$ws.Cells[24,1].Style.Numberformat.Format | Should be '#,' # Whole thousands
$ws.Cells[25,1].Style.Numberformat.Format | Should be '#.0,,' # Millions
}
}
}
Describe "Set-Column, Set-Row and Set Format" {
BeforeAll {
Remove-Item -Path $path -ErrorAction SilentlyContinue
$excel = $data| Export-Excel -Path $path -AutoNameRange -PassThru
$ws = $excel.Workbook.Worksheets["Sheet1"]
@@ -27,19 +147,26 @@ Describe "Set-Column, Set-Row and Set Format" {
Set-Format -Address $ws.Column(2) -AutoFit
Set-Format -Address $ws.Cells["E:E"] -AutoFit
Set-Format -Address $ws.row(5) -Height 0
$rr = $r.row
Set-Format -WorkSheet $ws -Range "B$rr" -Value "Total"
$BadHideWarnvar = $null
Set-Format -WorkSheet $ws -Range "D$rr" -Formula "=E$rr/C$rr" -Hidden -WarningVariable "BadHideWarnvar" -WarningAction SilentlyContinue
$rr ++
Set-Format -WorkSheet $ws -Range "B$rr" -Value ([datetime]::Now)
Close-ExcelPackage $excel
$excel = Open-ExcelPackage $path
$ws = $excel.Workbook.Worksheets["Sheet1"]
}
Context "Rows and Columns" {
Context "Set-Row and Set-Column" {
it "Set a row and a column to have zero width/height " {
$r | should not beNullorEmpty
# $c | should not beNullorEmpty ## can't see why but this test breaks in appveyor
$ws.Column(1).width | should be 0
$ws.Row(5).height | should be 0
$r | Should not beNullorEmpty
# $c | Should not beNullorEmpty ## can't see why but this test breaks in appveyor
$ws.Column(1).width | Should be 0
$ws.Row(5).height | Should be 0
}
it "Set a column formula, with numberformat, color, bold face and alignment" {
it "Set a column formula, with numberformat, color, bold face and alignment " {
$ws.cells["e2"].Formula | Should be "=Quantity*Price"
$ws.cells["e2"].Style.Font.Color.rgb | Should be "FF0000FF"
$ws.cells["e2"].Style.Font.Bold | Should be $true
@@ -49,6 +176,9 @@ Describe "Set-Column, Set-Row and Set Format" {
}
}
Context "Other formatting" {
it "Trapped an attempt to hide a range instead of a Row/Column " {
$BadHideWarnvar | Should not beNullOrEmpty
}
it "Set a row formula with border font size and underline " {
$ws.cells["b7"].style.Border.Top.Style | Should be "None"
$ws.cells["F7"].style.Border.Top.Style | Should be "None"
@@ -63,7 +193,6 @@ Describe "Set-Column, Set-Row and Set Format" {
$ws.cells["C7"].style.Font.UnderLine | Should be $true
$ws.cells["C6"].style.Font.UnderLine | Should be $false
}
it "Set custom text wrapping, alignment, superscript, border and Fill " {
$ws.cells["e3"].Style.HorizontalAlignment | Should be "Left"
$ws.cells["e3"].Style.Font.VerticalAlign | Should be "Superscript"
@@ -77,11 +206,62 @@ Describe "Set-Column, Set-Row and Set Format" {
$ws.cells["e1"].Style.Font.Bold | Should be $false
$ws.cells["C6"].style.WrapText | Should be $false
$ws.cells["e7"].style.WrapText | Should be $true
$ws.cells["e7"].Style.Fill.BackgroundColor.Rgb| Should be "FFF0F8FF"
$ws.cells["e7"].Style.Fill.BackgroundColor.Rgb | Should be "FFF0F8FF"
$ws.cells["e7"].Style.Fill.PatternColor.Rgb | Should be "FFFF0000"
$ws.cells["e7"].Style.Fill.PatternType | Should be "DarkTrellis"
}
}
Context "Set-Format value setting " {
it "Inserted a formula " {
$ws.Cells["D7"].Formula | Should be "=E7/C7"
}
it "Inserted a value " {
$ws.Cells["B7"].Value | Should be "Total"
}
it "Inserted a date with localized date-time format " {
$ws.Cells["B8"].Style.Numberformat.NumFmtID | Should be 22
}
}
Context "Set-Column Value Setting" {
BeforeAll {
Remove-Item -Path $path -ErrorAction SilentlyContinue
$excel = $DriverData | Export-Excel -PassThru -Path $path -AutoSize -AutoNameRange
$ws = $excel.Workbook.Worksheets[1]
Set-Column -Worksheet $ws -Heading "Link" -AutoSize -Value {"https://en.wikipedia.org" + $worksheet.cells["B$Row"].value }
$c = Set-Column -PassThru -Worksheet $ws -Heading "NextBirthday" -Value {
$bmonth = $worksheet.cells["C$Row"].value.month ; $bDay = $worksheet.cells["C$Row"].value.day
$cMonth = [datetime]::Now.Month ; $cday = [datetime]::Now.day ; $cyear = [datetime]::Now.Year
if (($cmonth -gt $bmonth) -or (($cMonth -eq $bmonth) -and ($cday -ge $bDay))){
[datetime]::new($cyear+1, $bmonth, $bDay)
}
else {[datetime]::new($cyear, $bmonth, $bday) }
}
Set-Column -Worksheet $ws -Heading "Age" -Value "=INT((NOW()-DateOfBirth)/365)"
Set-Format -Address $c,$ws.column(3) -NumberFormat 'Short Date' -AutoSize
Close-ExcelPackage -ExcelPackage $excel
$excel = Open-ExcelPackage $path
$ws = $excel.Workbook.Worksheets["Sheet1"]
}
It "Inserted Hyperlinks " {
$ws.Cells["D2"].Hyperlink | Should not beNullorEmpty
$ws.Cells["D2"].Style.Font.UnderLine | Should be $true
}
It "Inserted Dates " {
$ws.Cells["C2"].Value.GetType().name | should be "DateTime"
$ws.Cells["C2"].Style.Numberformat.NumFmtID | should be 14
$ws.Cells["E2"].Value.GetType().name | should be "DateTime"
$ws.Cells["E2"].Style.Numberformat.NumFmtID | should be 14
}
It "Inserted Formulas " {
$ws.Cells["F2"].Formula | Should not beNullorEmpty
}
}
}