Skip to content

Commit

Permalink
Merge pull request #1262 from jake-codes-at-5-am/removedatavalidation…
Browse files Browse the repository at this point in the history
…-to-upstream

Add RemoveDataValidation method
  • Loading branch information
tonyqus authored Apr 23, 2024
2 parents 3412b21 + 428a170 commit 976c1f5
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 8 deletions.
29 changes: 29 additions & 0 deletions OpenXmlFormats/Spreadsheet/Sheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7133,6 +7133,8 @@ public static CT_DataValidations Parse(XmlNode node, XmlNamespaceManager namespa

internal void Write(StreamWriter sw, string nodeName)
{
if (this.countField == 0)
return;
sw.Write(string.Format("<{0}", nodeName));
XmlHelper.WriteAttribute(sw, "disablePrompts", this.disablePrompts);
XmlHelper.WriteAttribute(sw, "xWindow", this.xWindow);
Expand Down Expand Up @@ -7387,6 +7389,33 @@ public void Set(CT_DataValidation obj)
promptField = obj.promptField;
sqrefField = obj.sqrefField;
}

public override bool Equals(object obj)
{
CT_DataValidation o = (CT_DataValidation)obj;

if (o is null)
{
return false;
}

return formula1Field == o.formula1Field
&& formula2Field == o.formula2Field
&& typeField == o.typeField
&& errorStyleField == o.errorStyleField
&& imeModeField == o.imeModeField
&& operatorField == o.operatorField
&& allowBlankField == o.allowBlankField
&& showDropDownField == o.showDropDownField
&& showInputMessageField == o.showInputMessageField
&& showErrorMessageField == o.showErrorMessageField
&& errorTitleField == o.errorTitleField
&& errorField == o.errorField
&& promptTitleField == o.promptTitleField
&& promptField == o.promptField
&& sqrefField == o.sqrefField;
}

[XmlElement(Order = 0)]
public string formula1
{
Expand Down
8 changes: 7 additions & 1 deletion main/HSSF/Record/Aggregates/DataValidityTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,11 @@ public void AddDataValidation(DVRecord dvRecord)
_validationList.Add(dvRecord);
_headerRec.DVRecNo = (_validationList.Count);
}

public void RemoveDataValidation(DVRecord dvRecord)
{
_validationList.Remove(dvRecord);
_headerRec.DVRecNo = _validationList.Count;
}
}
}
}
31 changes: 30 additions & 1 deletion main/HSSF/Record/DVRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,36 @@ public int DataType
set { this._option_flags = this.opt_data_type.SetValue(this._option_flags, value); }
}

public override bool Equals(object obj)
{
if (obj == null || !(obj is DVRecord))
{
return false;
}

DVRecord dv = (DVRecord)obj;
return DataType == dv.DataType
&& ErrorStyle == dv.ErrorStyle
&& EmptyCellAllowed == dv.EmptyCellAllowed
&& ShowErrorOnInvalidValue == dv.ShowErrorOnInvalidValue
&& ShowPromptOnCellSelected == dv.ShowPromptOnCellSelected
&& SuppressDropdownArrow == dv.SuppressDropdownArrow
&& ListExplicitFormula == dv.ListExplicitFormula
&& ConditionOperator == dv.ConditionOperator
&& PromptTitle == dv.PromptTitle
&& PromptText == dv.PromptText
&& ErrorTitle == dv.ErrorTitle
&& ErrorText == dv.ErrorText
&& ((Formula1 == null && dv.Formula1 == null)
|| Formula1 != null && dv.Formula1 != null
&& Formula1.ToString() == dv.Formula1.ToString())
&& ((Formula2 == null && dv.Formula2 == null)
|| Formula2 != null && dv.Formula2 != null
&& Formula2.ToString() == dv.Formula2.ToString())
&& (CellRangeAddress == null && dv.CellRangeAddress == null
|| CellRangeAddress != null && dv.CellRangeAddress != null
&& CellRangeAddress.ToString() == dv.CellRangeAddress.ToString());
}

/**
* Get the condition error style
Expand Down Expand Up @@ -426,4 +455,4 @@ public override Object Clone()
return CloneViaReserialise();
}
}
}
}
14 changes: 14 additions & 0 deletions main/HSSF/UserModel/HSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,20 @@ public void AddValidationData(IDataValidation dataValidation)
dvt.AddDataValidation(dvRecord);
}

public void RemoveDataValidation(IDataValidation dataValidation)
{
if (dataValidation == null)
{
throw new ArgumentException("objValidation must not be null");
}

HSSFDataValidation hssfDataValidation = (HSSFDataValidation)dataValidation;
DataValidityTable dvt = _sheet.GetOrCreateDataValidityTable();

DVRecord dvRecord = hssfDataValidation.CreateDVRecord(this);
dvt.RemoveDataValidation(dvRecord);
}

/// <summary>
/// Get the visibility state for a given column.F:\Gloria\�о�\�ļ���ʽ\NPOI\src\NPOI\HSSF\Util\HSSFDataValidation.cs
/// </summary>
Expand Down
8 changes: 7 additions & 1 deletion main/SS/UserModel/Sheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,12 @@ bool Autobreaks
/// <param name="dataValidation">The data validation object settings</param>
void AddValidationData(IDataValidation dataValidation);

/// <summary>
/// Removes a data validation object
/// </summary>
/// <param name="dataValidation">The data validation object settings</param>
void RemoveDataValidation(IDataValidation dataValidation);

/// <summary>
/// Enable filtering for a range of cells
/// </summary>
Expand Down Expand Up @@ -947,4 +953,4 @@ bool Autobreaks

void CopyTo(IWorkbook dest, string name, bool copyStyle, bool keepFormulas);
}
}
}
7 changes: 6 additions & 1 deletion ooxml/XSSF/Streaming/SXSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,16 @@ public void ValidateMergedRegions() {
_sh.ValidateMergedRegions();
}


public void AddValidationData(IDataValidation dataValidation)
{
_sh.AddValidationData(dataValidation);
}

public void RemoveDataValidation(IDataValidation dataValidation)
{
_sh.RemoveDataValidation(dataValidation);
}

/**
* Adjusts the column width to fit the contents.
*
Expand Down
24 changes: 24 additions & 0 deletions ooxml/XSSF/UserModel/XSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3327,7 +3327,31 @@ public void AddValidationData(IDataValidation dataValidation)
CT_DataValidation newval = dataValidations.AddNewDataValidation();
newval.Set(xssfDataValidation.GetCTDataValidation());
dataValidations.count = (uint)currentCount + 1;
}

public void RemoveDataValidation(IDataValidation dataValidation)
{
XSSFDataValidation xssfDataValidation = (XSSFDataValidation)dataValidation;
CT_DataValidations dataValidations = worksheet.dataValidations;

if (dataValidations is null)
{
return;
}

int currentCount = dataValidations.sizeOfDataValidationArray();

for (int i = 0; i < currentCount; i++)
{
CT_DataValidation ctDataValidation = dataValidations.dataValidation[i];

if (ctDataValidation.Equals(xssfDataValidation.GetCTDataValidation()))
{
dataValidations.dataValidation.RemoveAt(i);
dataValidations.count = (uint)currentCount - 1;
return;
}
}
}

public IAutoFilter SetAutoFilter(CellRangeAddress range)
Expand Down
26 changes: 24 additions & 2 deletions testcases/main/HSSF/UserModel/TestDataValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,28 @@ public void TestGetDataValidationsFormula()
Assert.AreEqual(double.NaN, c.Value2);
}

}
[Test]
public void TestRemoveDataValidation()
{

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.CreateSheet() as HSSFSheet;
List<IDataValidation> list = sheet.GetDataValidations();
Assert.AreEqual(0, list.Count);

}
IDataValidationHelper dataValidationHelper = sheet.GetDataValidationHelper();
IDataValidationConstraint constraint = dataValidationHelper.CreateCustomConstraint("A2:A3");
CellRangeAddressList AddressList = new CellRangeAddressList(0, 0, 0, 0);
IDataValidation validation = dataValidationHelper.CreateValidation(constraint, AddressList);
sheet.AddValidationData(validation);

list = sheet.GetDataValidations();
Assert.AreEqual(1, list.Count);

sheet.RemoveDataValidation(validation);

list = sheet.GetDataValidations();
Assert.AreEqual(0, list.Count);
}
}
}
28 changes: 26 additions & 2 deletions testcases/ooxml/XSSF/UserModel/TestXSSFDataValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,32 @@ public void TestCreateMultipleRegionsValidation()
}
}

[Test]
public void TestRemoveDataValidation()
{
XSSFWorkbook wb = new XSSFWorkbook();
try {
XSSFSheet sheet = wb.CreateSheet() as XSSFSheet;
IDataValidationHelper dataValidationHelper = sheet.GetDataValidationHelper();
IDataValidationConstraint constraint = dataValidationHelper.CreateExplicitListConstraint(new string[] { "A" });
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList();
cellRangeAddressList.AddCellRangeAddress(0, 0, 0, 0);
cellRangeAddressList.AddCellRangeAddress(0, 1, 0, 1);
cellRangeAddressList.AddCellRangeAddress(0, 2, 0, 2);
XSSFDataValidation dataValidation = dataValidationHelper.CreateValidation(constraint, cellRangeAddressList) as XSSFDataValidation;
sheet.AddValidationData(dataValidation);

Assert.AreEqual(1, sheet.GetDataValidations().Count);

sheet.RemoveDataValidation(dataValidation);

Assert.AreEqual(0, sheet.GetDataValidations().Count);
}
finally {
wb.Close();
}
}

private XSSFDataValidation CreateValidation(XSSFSheet sheet)
{
//create the cell that will have the validation applied
Expand All @@ -387,5 +413,3 @@ private XSSFDataValidation CreateValidation(XSSFSheet sheet)

}
}


0 comments on commit 976c1f5

Please sign in to comment.