New bug with MergeNamedStyles

I appear to have found a bug with the MergeNamedStyles function added recently. Please run the following top-level code (adjusting the file paths as needed) with the attached zipped workbooks as a .NET 9 C# console app
Dev.zip (790.5 KB)

:

using Aspose.Cells;

Workbook targetWbk = new(@"C:\Swift\Templates\Dev\InputCombined_Stoch.xlsx");
var inputSheet = targetWbk.Worksheets["Input"];
if (inputSheet != null)
{
    targetWbk.Worksheets.RemoveAt("Input");
}

inputSheet = targetWbk.Worksheets.Insert(0, SheetType.Worksheet, "Input");

var greySheet = targetWbk.Worksheets["Grey"];
if (greySheet != null)
{
    targetWbk.Worksheets.RemoveAt("Grey");
}

greySheet = targetWbk.Worksheets.Insert(1, SheetType.Worksheet, "Grey");

var sheet = targetWbk.Worksheets["Map"];
if (sheet != null)
{
    targetWbk.Worksheets.RemoveAt("Map");
}

DeleteStyles(targetWbk);

Workbook srcWorkbook = new(@"C:\Swift\Templates\Dev\Input_STRUCTURE.xlsx");
targetWbk.MergeNamedStyles(srcWorkbook);

static void DeleteStyles(Workbook wbk)
{
    string[] SWIFT_CUSTOM_STYLES = [
            "S_Date",
            "S_Date_Custom",
            "S_Decimal",
            "S_Decimal_Custom",
            "S_General",
            "S_General_Custom",
            "S_Header",
            "S_Header_Custom",
            "S_IDate",
            "S_IDecimal",
            "S_INumber",
            "S_IPercent",
            "S_IText",
            "S_Number",
            "S_Number_Custom",
            "S_ODate",
            "S_ODecimal",
            "S_ONumber",
            "S_OPercent",
            "S_OText",
            "S_Percent",
            "S_Percent_Custom",
            "S_xDate",
            "S_xDate_Custom",
            "S_xDecimal",
            "S_xDecimal_Custom",
            "S_xGeneral",
            "S_xGeneral_Custom",
            "S_xNumber",
            "S_xNumber_Custom",
            "S_xPercent",
            "S_xPercent_Custom"
        ];

    var builtInStyles = Enum.GetNames<BuiltinStyleType>();
    for (var i = 0; i < wbk.CountOfStylesInPool; i++)
    {
        var style = wbk.GetStyleInPool(i);
        if (!builtInStyles.Contains(style.Name) && !SWIFT_CUSTOM_STYLES.Contains(style.Name))
        {
            foreach (Worksheet sheet in wbk.Worksheets)
            {
                for (var r = 0; r <= sheet.Cells.MaxRow; r++)
                {
                    for (var c = 0; c <= sheet.Cells.MaxColumn; c++)
                    {
                        Cell cell;
                        if ((cell = sheet.Cells.CheckCell(r, c)) != null)
                        {
                            if (cell.IsStyleSet && cell.SharedStyleIndex == i)
                            {
                                cell.SetStyle(wbk.DefaultStyle);
                            }
                        }
                    }
                }
            }
        }
    }

    wbk.RemoveUnusedStyles();
}

@bytefyre

Cause

You are encountering an issue with the MergeNamedStyles function in your code. This may be due to incompatible styles between the source and target workbooks or potential bugs in the library itself.

Solution

To resolve this issue, please follow these steps:

  1. Check for Compatibility: Ensure that the styles in both workbooks are compatible. If there are styles in the source workbook that do not exist in the target workbook, it may cause unexpected behavior.

  2. Update Aspose.Cells: Verify that you are using the latest version of Aspose.Cells, as newer releases often contain bug fixes.

  3. Simplify the Code: Isolate the problem by simplifying your code. Test the MergeNamedStyles function with a minimal example that includes only a few styles to determine if the issue persists.

  4. Error Handling: Implement error handling around the MergeNamedStyles call to capture any exceptions that may provide more insight into the issue.

Code Example

Here’s a simplified version of how you might structure your code to include error handling:

try
{
    Workbook targetWbk = new(@"C:\Swift\Templates\Dev\InputCombined_Stoch.xlsx");
    Workbook srcWorkbook = new(@"C:\Swift\Templates\Dev\Input_STRUCTURE.xlsx");
    
    // Perform cleanup and setup as needed
    DeleteStyles(targetWbk);
    
    // Attempt to merge named styles
    targetWbk.MergeNamedStyles(srcWorkbook);
}
catch (Exception ex)
{
    Console.WriteLine($"An error occurred: {ex.Message}");
}

Next Steps

  1. Run the simplified code to see if the issue persists.
  2. If the problem continues, reach out to Aspose support with specific details about the error message or behavior you are encountering.
  3. Monitor the Aspose.Cells release notes for any updates regarding MergeNamedStyles.

Please let me know if you need further assistance!

The sheets need to be removed from InputCombined_Stoch first so all the styles that should be recognized as unused will be properly removed, so this sample code doesn’t fit my needs. Please use the code I provided. I look forward to a human response.

@bytefyre,

Thanks for the sample files and details.

I reproduced the issue by using your template XLSX files and sample code snippet. I found an exception “Object reference not set to an instance of an object” when merging named styles of the workbooks.

We require thorough evaluation of the issue. We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-58765

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@bytefyre,

This is to inform you that your issue (Ticket ID: “CELLSNET-58765”) has been resolved. The fix/enhancement will be incorporated into the upcoming release of Aspose.Cells (v25.8), which is scheduled for the first half of August 2025. You will be notified when the new version is published.

The issues you have found earlier (filed as CELLSNET-58765) have been fixed in this update. This message was posted using Bugs notification tool by leoluo