0

Say I have a custom data validation setup like this for a cell in Excel:

enter image description here

I then set the value of the cell in c# using Gembox Spreadsheet. At this point, is there a way to verify (from c#) if the validation linked to this cell was successful or not?

What has been tried: I did manage to find the DataValidation object linked to the cell via:

private DataValidation FindDatataValidationForCell(ExcelCell requiredCell)
{
    foreach (DataValidation dv in requiredCell.Worksheet.DataValidations)
    {
        foreach (CellRange range in dv.CellRanges)
        {
            foreach (ExcelCell foundCell in range)
            {
                if (foundCell == requiredCell)
                    return dv;
            }
        }
    }

    return null;
}

But in the case of a custom validation, not sure where to go from here. A workaround might be to write the formula read from the DataValidation object into a new (temporary) cell, and read the result, like this:

public bool IsValid(ExcelCell cell)
{
    DataValidation dv = FindDatataValidationForCell(cell);
    if (dv != null)
    {
        if (dv.Type == DataValidationType.Custom)
        {
            string str = dv.Formula1 as string;
            if (str != null && str.StartsWith("="))
            {
                // dodgy: use a cell which is known to be unused somewhere on the worksheet.                        
                var dummyCell = cell.Worksheet.Cells[100, 0];

                dummyCell.Formula = str;
                dummyCell.Calculate();                        
                bool res = dummyCell.BoolValue;
                dummyCell.Formula = null;   // no longer required. Reset. 

                return res; 
            }
        }
    }
    return true;
}

This does seem to work, but hoping there is a better way. Or failing that, maybe a better way to work out a temporary dummy cell location.

Moe Sisko
  • 11,665
  • 8
  • 50
  • 80
  • 1
    Currently, there is no such API but we are thinking of adding something similar to what we have for conditional formatting (the `ConditionalFormattingRule.Matches(ExcelCell)` method). I'll let you know when it's available (probably sometime at the end of this week). – Mario Z Oct 31 '22 at 12:19

1 Answers1

1

Try using this latest bugfix version:
https://www.gemboxsoftware.com/spreadsheet/nightlybuilds/GBS49v1171.zip

Or this latest NuGet package:
Install-Package GemBox.Spreadsheet -Version 49.0.1171-hotfix

Now there is a DataValidation.Validate(ExcelCell) method that you can use.

Mario Z
  • 4,328
  • 2
  • 24
  • 38
  • works well with the custom validation. When Data Validation is set as a List, it seems that this method performs a case sensitive check ? e.g. if the list allows "Hello", but you set the cell's value to "hello", then the new method returns false. However if you typed in "hello" in MS Excel, it seems to accept it. So Excel seems to perform a case insensitive test. Is it possible/feasible to change the method to be consistent with Excel ? – Moe Sisko Nov 15 '22 at 01:18
  • Clarification: "However if you typed in "hello" in MS Excel, it seems to accept it". I mean: "However if you typed in "hello" into a cell in the Excel application, Excel itself seems to accept it (validation is passed)", – Moe Sisko Nov 15 '22 at 06:49
  • @MoeSisko are you sure MS Excel accepted it? I'm not able to reproduce that. Here is the data validation I tried: "https://drive.google.com/file/d/12fI7qHMM7JR7gVd5l4sQNpexGd6unBkJ/view?usp=sharing", and here is what happens when I write a lower case "hello": "https://drive.google.com/file/d/17zuOTcWZonphPlwsFNw7dW3m8BWZDfwV/view?usp=sharing" – Mario Z Nov 15 '22 at 07:59
  • ah, sorry: to reproduce the problem, set list validation's "source" to a 1 column (or 1 row) range, such as "=D1:D3" . Set cell D1 to "Hello", D2 to "abc", D3 to "xyz". – Moe Sisko Nov 15 '22 at 11:29
  • @MoeSisko I've updated the answer with the current latest version (49.0.1171). Please try again with it. – Mario Z Nov 16 '22 at 13:15