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

Formatting weirdness when adding rows to a table #1666

Open
sba923 opened this issue Jan 20, 2025 · 12 comments
Open

Formatting weirdness when adding rows to a table #1666

sba923 opened this issue Jan 20, 2025 · 12 comments

Comments

@sba923
Copy link

sba923 commented Jan 20, 2025

Start with an Excel table that looks like this:

Image

If you try to add two more rows to the table using

$newrows = @(
    [PSCustomObject]@{
        A=10
        B=11
        C=12
    },
    [PSCustomObject]@{
        A=20
        B=21
        C=22
    }
)
$newrows | Export-Excel -Path (Join-Path -Path $PSScriptRoot -ChildPath 'modified2.xlsx') -WorksheetName 'Sheet1' -Table 'Table1' -Append

You'll get a new formatting for the extended table:

Image

Removing the -Table 'Table1' parameter preserves the formatting:

Image

Code, input file and output files in:

AddRowsToTable.zip

@dfinke
Copy link
Owner

dfinke commented Jan 20, 2025

took a quick look.

leaving this hear for later review.

$existingTable = $ws.Tables.Where({ $_.address.address -eq $ws.dimension.address }, 'First', 1)

@arkoudigr
Copy link

arkoudigr commented Jan 21, 2025

Start with an Excel table that looks like this:

Image

If you try to add two more rows to the table using

$newrows = @(
[PSCustomObject]@{
A=10
B=11
C=12
},
[PSCustomObject]@{
A=20
B=21
C=22
}
)
$newrows | Export-Excel -Path (Join-Path -Path $PSScriptRoot -ChildPath 'modified2.xlsx') -WorksheetName 'Sheet1' -Table 'Table1' -Append

You'll get a new formatting for the extended table:

Image

Removing the -Table 'Table1' parameter preserves the formatting:

Image

Code, input file and output files in:

AddRowsToTable.zip

Hello, you need to add -tablestyle medium2 to preserve the same. For some reason the script when you use -table has the pink table style. So your code should look like this:
$newrows | Export-Excel -Path (Join-Path -Path $PSScriptRoot -ChildPath 'modified2.xlsx') -WorksheetName 'Sheet1' -Table 'Table1' -tablestyle Medium2 -Append

@arkoudigr
Copy link

Small update -tablestyle Custom works better, it inherits the style that you already have to the test.xlsx. So optimal code would be:
$newrows | Export-Excel -Path (Join-Path -Path $PSScriptRoot -ChildPath 'modified2.xlsx') -WorksheetName 'Sheet1' -Table 'Table1' -tablestyle Custom -Append

@dfinke
Copy link
Owner

dfinke commented Jan 21, 2025

Thank you @arkoudigr. @sba923 does that work for you?

@sba923
Copy link
Author

sba923 commented Jan 21, 2025

Well, I've tried adding data to two different tables with different formatting, this gets me a corrupted file Excel complains about....

Copy-Item -Path (Join-Path -Path $PSScriptRoot -ChildPath 'test.xlsx') -Destination (Join-Path -Path $PSScriptRoot -ChildPath 'modified.xlsx')


$newrows = @(
    [PSCustomObject]@{
        A=10
        B=11
        C=12
    },
    [PSCustomObject]@{
        A=20
        B=21
        C=22
    }
)

$newrows | Export-Excel -Path (Join-Path -Path $PSScriptRoot -ChildPath 'modified.xlsx') -WorksheetName 'Sheet1' -Table 'Table1' -TableStyle Custom  -Append
$newrows | Export-Excel -Path (Join-Path -Path $PSScriptRoot -ChildPath 'modified.xlsx') -WorksheetName 'Sheet1' -Table 'Table2' -TableStyle Custom  -Append

AddRowsToTable202501212100.zip

@arkoudigr
Copy link

Well, the idea you suggest cannot work as it seems. Your excel file gets corrupted because the script add rows and changes the table structure. What I mean is while changing table1 it destroys table 2. Here is something interesting, when I Import test.xlsx to powershell it returns only the first table
`PS C:**\Downloads\AddRowsToTable202501212100> $test = Import-Excel .\test.xlsx

PS C:**\Downloads\AddRowsToTable202501212100> $test

A B C


1 2 3
4 5 6`

@sba923
Copy link
Author

sba923 commented Jan 23, 2025

I was just trying to test how one would modify an existing table by name while preserving its format, so I created a sheet with two distinct tables.

@dfinke the module should never create a corrupted file, don't you think?

@dfinke
Copy link
Owner

dfinke commented Jan 23, 2025

Sure, can you provide a simple repo of that. It could be that the EPPlus lib may have caused it and fixing that is not likely. Also, no reports on that and if it does, folks find work arounds.

@sba923
Copy link
Author

sba923 commented Jan 23, 2025

Sure, can you provide a simple repo of that. It could be that the EPPlus lib may have caused it and fixing that is not likely. Also, no reports on that and if it does, folks find work arounds.

The repro code in in the zipfile I posted here yesterday. I presume you want me to create a separate issue for that corruption issue, correct?

@dfinke
Copy link
Owner

dfinke commented Jan 23, 2025

not necessarily.

what is the corruption? usually if I see a "corruption" when I open the xlsx and it says it has an issue and it will try to recover.

@sba923
Copy link
Author

sba923 commented Jan 23, 2025

First

Image

if I choose Yes I get:

Image

the XML repair log reads:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <logFileName>error523200_01.xml</logFileName>
    <summary>
        Errors were detected in file
        'https://d.docs.live.net/6c0f5f708f464f30/CloudOnly/ImportExcel/AddRowsToTable/modified.xlsx'</summary>
    <removedFeatures>
        <removedFeature>Removed Feature: AutoFilter from /xl/tables/table1.xml part (Table)</removedFeature>
        <removedFeature>Removed Feature: Table from /xl/tables/table1.xml part (Table)</removedFeature>
        <removedFeature>Removed Feature: AutoFilter from /xl/tables/table2.xml part (Table)</removedFeature>
        <removedFeature>Removed Feature: Table from /xl/tables/table2.xml part (Table)</removedFeature>
    </removedFeatures>
</recoveryLog>

The file (available in the zipfile I posted yesterday) is very badly damaged:

  1. the added data is in the wrong cells
  2. all formatting is lost

Image

@dfinke
Copy link
Owner

dfinke commented Jan 23, 2025

Looked at, will at it some more. Not sure I will be able to fix that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants