diff --git a/Examples/NoFormulaConversion/NoFormula-AllColumns.ps1 b/Examples/NoFormulaConversion/NoFormula-AllColumns.ps1 new file mode 100644 index 00000000..55cce27c --- /dev/null +++ b/Examples/NoFormulaConversion/NoFormula-AllColumns.ps1 @@ -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 diff --git a/Examples/NoFormulaConversion/NoFormula-NamedColumns.ps1 b/Examples/NoFormulaConversion/NoFormula-NamedColumns.ps1 new file mode 100644 index 00000000..db19168c --- /dev/null +++ b/Examples/NoFormulaConversion/NoFormula-NamedColumns.ps1 @@ -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 diff --git a/Public/Export-Excel.ps1 b/Public/Export-Excel.ps1 index 5c08b208..472ca20d 100644 --- a/Public/Export-Excel.ps1 +++ b/Public/Export-Excel.ps1 @@ -80,6 +80,7 @@ [Switch]$DisplayPropertySet, [String[]]$NoNumberConversion, [String[]]$NoHyperLinkConversion, + [String[]]$NoFormulaConversion, [Object[]]$ConditionalFormat, [Object[]]$ConditionalText, [Object[]]$Style, @@ -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 } } diff --git a/__tests__/Export-Excel.Tests.ps1 b/__tests__/Export-Excel.Tests.ps1 index def74455..9d48d91a 100644 --- a/__tests__/Export-Excel.Tests.ps1 +++ b/__tests__/Export-Excel.Tests.ps1 @@ -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 @@ -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 { @@ -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 " { @@ -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" { @@ -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 + } } \ No newline at end of file