Skip to content

Fix: Handle extLst in data validation for deletion (#2133) #2137

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

Merged
merged 2 commits into from
May 17, 2025

Conversation

DengY11
Copy link
Contributor

@DengY11 DengY11 commented May 16, 2025

Description

This pull request addresses an issue where data validations defined within an <extLst> (Extension List) in the worksheet XML were not being fully parsed. This incomplete parsing led to ws.DataValidations being nil in certain cases, causing the DeleteDataValidation function to return prematurely without attempting to delete the relevant data validation rules.

The key changes implemented in this PR are:

  1. Modified the internal xlsxDataValidation struct by adding an ExtLst field. This allows the excelize library to correctly parse and capture the content of <extLst> elements from the worksheet XML.
  2. Introduced an ExtLstXML string field to the public DataValidation struct. This field exposes the raw XML content of the extension list associated with a data validation rule, making this information accessible to users of the library.
  3. Updated the getDataValidations helper function to ensure that the raw XML content from the parsed dv.ExtLst.Ext (internal struct) is correctly transferred to the dataValidation.ExtLstXML field of the public DataValidation object.

These modifications ensure that all data validation rules, including those defined via XML extensions, are fully loaded and represented. Consequently, DeleteDataValidation can now correctly identify and operate on these data validations.

Related Issue

Fixes #2133

Motivation and Context

This change is required to fix a bug where DeleteDataValidation would not work for Excel files if their data validations were defined using extension list (extLst) elements. The original issue (#2133) describes how the function would return early because the DataValidations field on the worksheet object was nil, due to extLst not being parsed. This PR ensures that such data validations are properly read, allowing them to be managed (specifically, deleted) correctly by the library, thus improving compatibility and correctness when handling modern Excel files.

How Has This Been Tested

The changes have been tested as follows:

  1. Unit and Integration Tests: All existing tests in the excelize project were run using go test -v ./... after applying the changes, and all tests passed successfully. This ensures no existing functionality was broken.
  2. Manual Functional Testing:
    • A minimal XLSX file (extLstDebugFile.xlsx) was manually created, containing a data validation rule on sheet SheetWithExtLstDV cell B1 that was intentionally defined using an <extLst> element (specifically, <ext uri="{CCE6A557-97BC-4b89-ADB6-D9C93CAAB3DF}..."> and <ext uri="{FAC63952-8661-4B54-9155-E8378167997E}...">).
    • A test Go program (main.go in a separate tester project مواد replace directive) was used to:
      • Open this test file using the modified excelize library.
      • Call GetDataValidations("SheetWithExtLstDV") and verify (via logging) that the ExtLstXML field of the returned DataValidation object was correctly populated with the XML content from the extLst.
      • Call DeleteDataValidation("SheetWithExtLstDV", "B1").
      • Call GetDataValidations("SheetWithExtLstDV") again and verify that no data validations were returned, confirming successful deletion in memory.
      • Save the modified file using f.SaveAs().
    • The saved Excel file was then manually opened in Microsoft Excel to confirm that the data validation rule (e.g., dropdown arrow) on cell B1 was indeed removed.
  3. Code Review and Analysis: Ensured that the changes are localized and follow the existing patterns and design of the excelize library.

Testing Environment:

  • Go version: go version go1.23.9 linux/amd64
  • OS: Ubuntu 22.04 LTS

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document. (Make sure you actually have!)
  • I have added tests to cover my changes.
  • All new and existing tests passed.

    This commit resolves issue #2133 by ensuring that data validations
    defined within an <extLst> (Extension List) are correctly parsed
    and accessible. This prevents DeleteDataValidation from prematurely
    returning nil when ws.DataValidations was previously unpopulated
    due to unhandled extLst elements.

    Key changes:
    - Modified internal 'xlsxDataValidation' struct to include an 'ExtLst'
      field, enabling the parsing of <extLst> from worksheet XML.
    - Added 'ExtLstXML' string field to the public 'DataValidation' struct
      to expose the raw XML content of the extension list to users.
    - Updated the 'getDataValidations' helper function to correctly
      transfer the extLst XML content from the internal structure to
      the public DataValidation object.

    These changes ensure that data validations, including those defined
    via extensions, are fully loaded, allowing DeleteDataValidation
    to operate correctly on them.
@DengY11
Copy link
Contributor Author

DengY11 commented May 16, 2025

extLstValidationTest.xlsx

extLstValidationTest_after_delete_attempt.xlsx

The original file and the file deleted extLst validation

@xuri xuri added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label May 16, 2025
Copy link
Member

@xuri xuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your issue. Unit test is required for coverage this changes.

- Support delete data validation by given with multiple cell ranges with reference sequence slice or blank separated reference sequence string
- Update unit tests
Copy link
Member

@xuri xuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fix code review issues based on your branch. Thanks for your contribution.

@xuri xuri added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels May 17, 2025
@xuri xuri moved this to Features in Excelize v2.10.0 May 17, 2025
Copy link

codecov bot commented May 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.23%. Comparing base (72b731a) to head (67e1ae1).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2137   +/-   ##
=======================================
  Coverage   99.23%   99.23%           
=======================================
  Files          32       32           
  Lines       30243    30303   +60     
=======================================
+ Hits        30012    30072   +60     
  Misses        153      153           
  Partials       78       78           
Flag Coverage Δ
unittests 99.23% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@xuri xuri merged commit bb1105b into qax-os:master May 17, 2025
17 checks passed
@github-project-automation github-project-automation bot moved this from Features to Peformance in Excelize v2.10.0 May 17, 2025
@xuri xuri moved this from Peformance to Features in Excelize v2.10.0 May 18, 2025
@xuri xuri moved this to Features in Excelize v2.10.0 Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
Status: Features
Development

Successfully merging this pull request may close these issues.

DeleteDataValidation Not Working On Existing Files
2 participants