Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NoFormulaConversion to Export-Excel #1650

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions Examples/NoFormulaConversion/NoFormula-AllColumns.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
try {Import-Module $PSScriptRoot\..\..\ImportExcel.psd1} catch {throw ; return}

$xlSourcefile = "$env:TEMP\NoFormulaExample.xlsx"

#Remove existing file
Remove-Item $xlSourcefile -ErrorAction Ignore

#These formulas should not calculate and remain as text
[PSCustOmobject][Ordered]@{
Formula1 = "=COUNT(1,1,1)"
Formula2 = "=SUM(2,3)"
Formula3 = "=1=1"
} | Export-Excel $xlSourcefile -NoFormulaConversion * -Calculate -Show
12 changes: 12 additions & 0 deletions Examples/NoFormulaConversion/NoFormula-NamedColumns.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
try {Import-Module $PSScriptRoot\..\..\ImportExcel.psd1} catch {throw ; return}

$xlSourcefile = "$env:TEMP\NoFormulaExample.xlsx"

#Remove existing file
Remove-Item $xlSourcefile -ErrorAction Ignore

#Simulate a matching comparison to get a SideIndicator of '=='
$comparison = Compare-Object -ReferenceObject @(1) -DifferenceObject @(1) -IncludeEqual

#Add '-NoFormulaConversion SideIndicator' to allow '==' to be used in the SideIndicator column without converting to a formula
$comparison | Export-Excel $xlSourcefile -NoFormulaConversion SideIndicator -Show
6 changes: 5 additions & 1 deletion Public/Export-Excel.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
[Switch]$DisplayPropertySet,
[String[]]$NoNumberConversion,
[String[]]$NoHyperLinkConversion,
[String[]]$NoFormulaConversion,
[Object[]]$ConditionalFormat,
[Object[]]$ConditionalText,
[Object[]]$Style,
Expand Down Expand Up @@ -330,7 +331,10 @@
#Other objects or null.
if ($null -ne $v) { $ws.Cells[$row, $ColumnIndex].Value = $v.toString() }
}
elseif ($v[0] -eq '=') {
elseif ( $v[0] -eq '=' -and
$NoFormulaConversion -ne '*' -and
$NoFormulaConversion -notcontains $Name
) {
$ws.Cells[$row, $ColumnIndex].Formula = ($v -replace '^=', '')
if ($setNumformat) { $ws.Cells[$row, $ColumnIndex].Style.Numberformat.Format = $Numberformat }
}
Expand Down
35 changes: 32 additions & 3 deletions __tests__/Export-Excel.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Describe ExportExcel -Tag "ExportExcel" {
else { $OtherCurrencySymbol = "£" }
$path = "TestDrive:\test.xlsx"
$warnVar = $null
#Test correct export of different data types and number formats; test hyperlinks, test -NoNumberConversion and -NoHyperLinkConversion test object is converted to a string with no warnings, test calcuation of formula
#Test correct export of different data types and number formats; test hyperlinks, test -NoNumberConversion, -NoHyperLinkConversion and -NoFormulaConversion test object is converted to a string with no warnings, test calcuation of formula
Remove-item -Path $path -ErrorAction SilentlyContinue
[PSCustOmobject][Ordered]@{
Date = Get-Date
Expand Down Expand Up @@ -236,7 +236,8 @@ Describe ExportExcel -Tag "ExportExcel" {
Link6 = "xl://internal/sheet1!C5"
Process = (Get-Process -Id $PID)
TimeSpan = [datetime]::Now.Subtract([datetime]::Today)
} | Export-Excel -NoNumberConversion IPAddress, StrLeadZero, StrAltPhone2 -NoHyperLinkConversion Link6 -Path $path -Calculate -WarningVariable $warnVar
NotAFormula = "=SUM(2,3)"
} | Export-Excel -NoNumberConversion IPAddress, StrLeadZero, StrAltPhone2 -NoHyperLinkConversion Link6 -NoFormulaConversion NotAFormula -Path $path -Calculate -WarningVariable $warnVar
}

BeforeEach {
Expand All @@ -254,7 +255,7 @@ Describe ExportExcel -Tag "ExportExcel" {
}
it "Created the worksheet with the expected name, number of rows and number of columns " {
$ws.Name | Should -Be "sheet1"
$ws.Dimension.Columns | Should -Be 28
$ws.Dimension.Columns | Should -Be 29
$ws.Dimension.Rows | Should -Be 2
}
it "Set a date in Cell A2 " {
Expand Down Expand Up @@ -326,6 +327,11 @@ Describe ExportExcel -Tag "ExportExcel" {
$ws.cells[2, 28].Value.ToOADate() | Should -BeLessThan 1
$ws.cells[2, 28].Style.Numberformat.Format | Should -Be '[h]:mm:ss'
}
it "Creates no formula in cell AC2 NotAFormula (NoFormulaConversion) " {
$ws.Cells[2, 29].Value | Should -Be '=SUM(2,3)'
$ws.Cells[2, 29].Value.GetType().name | Should -Be 'String'
$ws.Cells[2, 29].Formula | Should -Be ''
}
}

Context "# # Setting cells for different data types with -noHeader" {
Expand Down Expand Up @@ -1353,4 +1359,27 @@ Describe ExportExcel -Tag "ExportExcel" {

Remove-Item $path
}

It "Should have no formula created using wild card" -Tag Formula {
$path = "TestDrive:\testFormula.xlsx"

[PSCustOmobject][Ordered]@{
Formula1 = "=COUNT(1,1,1)"
Formula2 = "=SUM(2,3)"
Formula3 = "=1=1"
} | Export-Excel -NoFormulaConversion * -Path $path -Calculate

$excel = Open-ExcelPackage $Path
$ws = $excel.Sheet1

$ws.Dimension.Rows | Should -Be 2
$ws.Dimension.Columns | Should -Be 3

$ws.Cells["A2"].Value | Should -BeExactly "=COUNT(1,1,1)"
$ws.Cells["B2"].Value | Should -BeExactly "=SUM(2,3)"
$ws.Cells["C2"].Value | Should -BeExactly "=1=1"

Close-ExcelPackage $excel
Remove-Item $path
}
}