Skip to content

DeleteDataValidation Not Working On Existing Files #2133

Closed
@Kindred87

Description

@Kindred87

Description

DeleteDataValidation does not work as expected. When calling it with sqrefs collected from a GetDataValidations call on the same sheet:

validations, err := workbook.GetDataValidations(sheetName)
if err != nil {
    return fmt.Errorf("error getting data validations: %w", err)
}

var origSqrefs []string
for _, v := range validations {
    origSqrefs = append(origSqrefs, v.Sqref)
}

if len(origSqrefs) > 0 {
    if err := workbook.DeleteDataValidation(sheetName, origSqrefs...); err != nil {
        return fmt.Errorf("error deleting original data validations: %w", err)
    }
}

The data validations remain in the sheet post-deletion. Even after saving the file.

While debugging, I noticed that the delete function returns early. The object returned by the f.workSheetReader(sheet) call has a DataValidations field that is nil. Which causes the function to return nil before performing any work. This is unexpected since the GetDataValidations() call I made before, on the same sheet, successfully returned data validations:

func (f *File) DeleteDataValidation(sheet string, sqref ...string) error {
    ws, err := f.workSheetReader(sheet)
    if err != nil {
        return err
    }
    ws.mu.Lock()
    defer ws.mu.Unlock()
    if ws.DataValidations == nil {
        return nil  <--------------------------------- RETURNS EARLY HERE
    }
    if sqref == nil {
        ws.DataValidations = nil
        return nil
    }
}

Variables in the DeleteDataValidation call at runtime:

Image

Variables in my code snippet (the one that calls DeleteDataValidation) at runtime:

Image

Steps to reproduce the issue

  1. Open an existing Excel file with a sheet containing data validations.
  2. Call workbook.GetDataValidations on the sheet containing the data validations.
  3. Pass the Sqrefs from the returned data validations to a workbook.DeleteDataValidations call, on the same sheet.
  4. Pause a debugger on line 369 in datavalidation.go
  5. Observe the empty DataValidations member of variable ws

Describe the results you received

No deletion of specified data validations.

Describe the results you expected

Deletion of specified data validations

Go version

1.24.3

Excelize version or commit ID

2.9.1

Environment

Windows 11, Microsoft® Excel® for Microsoft 365 MSO (Version 2502 Build 16.0.18526.20286) 64-bit

Validations

  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • The provided reproduction is a minimal reproducible example of the bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    Features

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions