Skip to content

Weird formula assignment #1728

@RoYoMi

Description

@RoYoMi

Happy Holidays everyone :)

I am populating a table cell with a formula, but the formula stored in the file magically gets a new character which then breaks the formula.

Using ImportExcel v7.8.10
Using PowersShell v7.4.12
Excel version from Microsoft 365 enterprise

The worksheet was defined as a table with fields for Action Add, UserName, Address, Name, and NewName

Here is the MVP pattern I'm using:

$BlankRecordForFile = [PsCustomObject]@{
    'Action Add'    = ''
    UserName        = ''
    Address         = ''
    Name            = ''
    NewName         = ''
    }

$File = [system.io.fileinfo]'c:\myfolder\myfile.xlsx'

$ExportOptions = @{
    Path            = $File.FullName
    WorksheetName   = 'Data'
    TableName       = 'DataTable'
    TableStyle      = 'Light1'
    AutoSize        = $True
    AutoFilter      = $True
    BoldTopRow      = $True
    FreezeTopRow    = $True
    StartRow        = 1
    }
$ExcelFile  = $BlankRecordForFile | Export-Excel @ExportOptions -PassThru

$Worksheet  = $ExcelFile.Workbook.Worksheets[ 'Data' ]

$Worksheet.InsertRow( 2, 1 )

# Note:                                no '@' character between these arrows --->  <---
$Formula = '=IFS( [[#This Row],[UserName]]="","", [[#This Row],[Action Add]]=TRUE, CONCAT([[#This Row],[Address]],"-",[[#This Row],[UserName]]), CONCAT([[#This Row],[Address]],"-",[[#This Row],[UserName]]) <> [[#This Row],[Name]], CONCAT([[#This Row],[Address]],"-",[[#This Row],[UserName]]), TRUE, "")'

$Cell = $Worksheet.Cells[ 'e2' ]
$Cell.Formula = $Formula
Write-Host "Assigned formula '$( $Cell.Formula )'"

Close-ExcelPackage $ExcelFile

$Cell.Formula shows the exact value I provided.

When viewing the formula in Excel we can see the resulting formula is changed to this:

=@IFS( [@UserName]="","", [@[Action Add]]=TRUE, CONCAT([@Address],"-",[@UserName]),@ CONCAT([@Address],"-",[@UserName]) <> [@Name], CONCAT([@Address],"-",[@UserName]), TRUE, "")
  • the initial @ injected after the equals sign, this is a new thing Excel is doing. It's strange but is apparently normal
  • the [#This Row], are replaced with @ to make the table references work. This is a desired change.
  • a @ symbol injected at character 84 directly before CONCAT. This extra character breaks the formula. This symbol was not present in my original formula. In Excel if I remove this character the formula starts to work as expected.

I'm looking for advise. Is this a real bug; or am I just doing it wrong; or is there a better way to do this?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions