Skip to content

Conversation

Copy link

Copilot AI commented Dec 26, 2025

Excel corrupts table-structured reference formulas (e.g., [[#This Row],[ColumnName]]) when fullCalcOnLoad="1" triggers formula recalculation on file open. Excel's parser incorrectly inserts extra @ symbols during this process, breaking complex formulas like:

# Before fix: This formula gets corrupted when opened in Excel
$Cell.Formula = '=IFS([[#This Row],[UserName]]="","", [[#This Row],[Action Add]]=TRUE, CONCAT([[#This Row],[Address]],"-",[[#This Row],[UserName]]), ...)'
# Excel would insert: ,@ CONCAT(...) breaking the formula

Changes

  • Close-ExcelPackage.ps1: Set FullCalcOnLoad = $false before saving
  • Export-Excel.ps1: Set FullCalcOnLoad = $false in direct save path
  • TableFormula.tests.ps1: Added test coverage for formula preservation

Setting fullCalcOnLoad="0" prevents Excel from rewriting formulas on open, avoiding the parser bug while preserving -Calculate flag functionality for users who need explicit calculation.

# After fix: Formula preserved exactly as assigned
Close-ExcelPackage $ExcelFile  # Sets fullCalcOnLoad=false automatically
Original prompt

This section details on the original issue you should resolve

<issue_title>Weird formula assignment</issue_title>
<issue_description>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?

</issue_description>

Comments on the Issue (you are @copilot in this section)

@dfinke Thanks for reporting the issue @RoYoMi. Off the top of my head it looks fine. I'd have to get in there and try some scenarios. I vaguely remember issues with formulas, maybe with the `@` getting added. I don't remember the details or if there was a fix/workaround etc.

You have a good harness here. My first approach would be to start with the simplest formula that works and then stepwise add complexity to see where and when it breaks.

Should be quick to iterate on it and get some ideas.

I'm going to assign copilot to these see if comes up with anything useful.</comment_new>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits December 26, 2025 21:31
Co-authored-by: dfinke <67258+dfinke@users.noreply.github.com>
Co-authored-by: dfinke <67258+dfinke@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix weird formula assignment in Excel table Fix formula corruption by disabling Excel's fullCalcOnLoad Dec 26, 2025
Copilot AI requested a review from dfinke December 26, 2025 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Weird formula assignment

2 participants