Skip to content
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

[FluentAPI] Return ICell type for SetXXX methods of ICell interface #1450

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions main/HSSF/UserModel/HSSFCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public IRow Row
/// Set the cells type (numeric, formula or string)
/// </summary>
/// <param name="cellType">Type of the cell.</param>
public void SetCellType(CellType cellType)
public ICell SetCellType(CellType cellType)
{
NotifyFormulaChanging();
if (IsPartOfArrayFormulaGroup)
Expand All @@ -240,6 +240,7 @@ public void SetCellType(CellType cellType)
int col = _record.Column;
short styleIndex = _record.XFIndex;
SetCellType(cellType, true, row, col, styleIndex);
return this;
}

/// <summary>
Expand Down Expand Up @@ -467,7 +468,7 @@ private String ConvertCellValueToString()
/// <param name="value">the numeric value to Set this cell to. For formulas we'll Set the
/// precalculated value, for numerics we'll Set its value. For other types we
/// will Change the cell to a numeric cell and Set its value.</param>
public void SetCellValue(double value)
public ICell SetCellValue(double value)
{
if(double.IsInfinity(value))
{
Expand Down Expand Up @@ -501,7 +502,8 @@ public void SetCellValue(double value)
break;
}
}


return this;
}

/// <summary>
Expand All @@ -511,9 +513,9 @@ public void SetCellValue(double value)
/// <param name="value">the date value to Set this cell to. For formulas we'll Set the
/// precalculated value, for numerics we'll Set its value. For other types we
/// will Change the cell to a numeric cell and Set its value.</param>
public void SetCellValue(DateTime value)
public ICell SetCellValue(DateTime value)
{
SetCellValue(DateUtil.GetExcelDate(value, this.book.IsDate1904()));
return SetCellValue(DateUtil.GetExcelDate(value, this.book.IsDate1904()));
}

#if NET6_0_OR_GREATER
Expand All @@ -524,9 +526,9 @@ public void SetCellValue(DateTime value)
/// <param name="value">the date value to Set this cell to. For formulas we'll Set the
/// precalculated value, for numerics we'll Set its value. For other types we
/// will Change the cell to a numeric cell and Set its value.</param>
public void SetCellValue(DateOnly value)
public ICell SetCellValue(DateOnly value)
{
SetCellValue(DateUtil.GetExcelDate(value, this.book.IsDate1904()));
return SetCellValue(DateUtil.GetExcelDate(value, this.book.IsDate1904()));
}
#endif

Expand All @@ -539,10 +541,10 @@ public void SetCellValue(DateOnly value)
/// string, for String cells we'll Set its value. For other types we will
/// Change the cell to a string cell and Set its value.
/// If value is null then we will Change the cell to a Blank cell.</param>
public void SetCellValue(String value)
public ICell SetCellValue(String value)
{
HSSFRichTextString str = value == null ? null : new HSSFRichTextString(value);
SetCellValue(str);
return SetCellValue(str);
}
/**
* set a error value for the cell
Expand All @@ -553,10 +555,10 @@ public void SetCellValue(String value)
* cell and set its value.
*/
[Obsolete("deprecated 3.15 beta 2. Use {@link #setCellErrorValue(FormulaError)} instead.")]
public void SetCellErrorValue(byte errorCode)
public ICell SetCellErrorValue(byte errorCode)
{
FormulaError error = FormulaError.ForInt(errorCode);
SetCellErrorValue(error);
return SetCellErrorValue(error);
}
/**
* set a error value for the cell
Expand All @@ -566,7 +568,7 @@ public void SetCellErrorValue(byte errorCode)
* its value. For other types we will change the cell to an error
* cell and set its value.
*/
public void SetCellErrorValue(FormulaError error)
public ICell SetCellErrorValue(FormulaError error)
{
int row = _record.Row;
int col = _record.Column;
Expand All @@ -585,6 +587,8 @@ public void SetCellErrorValue(FormulaError error)
((BoolErrRecord)_record).SetValue(error);
break;
}

return this;
}
/// <summary>
/// Set a string value for the cell. Please note that if you are using
Expand All @@ -594,7 +598,7 @@ public void SetCellErrorValue(FormulaError error)
/// string, for String cells we'll Set its value. For other types we will
/// Change the cell to a string cell and Set its value.
/// If value is null then we will Change the cell to a Blank cell.</param>
public void SetCellValue(IRichTextString value)
public ICell SetCellValue(IRichTextString value)
{
int row = _record.Row;
int col = _record.Column;
Expand All @@ -603,7 +607,7 @@ public void SetCellValue(IRichTextString value)
{
NotifyFormulaChanging();
SetCellType(CellType.Blank, false, row, col, styleIndex);
return;
return this;
}

if (value.Length > NPOI.SS.SpreadsheetVersion.EXCEL97.MaxTextLength)
Expand All @@ -618,7 +622,7 @@ public void SetCellValue(IRichTextString value)
fr.SetCachedStringResult(value.String);
// Update our local cache to the un-formatted version
stringValue = new HSSFRichTextString(value.String);
return;
return this;
}

if (cellType != CellType.String)
Expand All @@ -634,6 +638,7 @@ public void SetCellValue(IRichTextString value)
stringValue = hvalue;
stringValue.SetWorkbookReferences(book.Workbook, ((LabelSSTRecord)_record));
stringValue.UnicodeString = book.Workbook.GetSSTString(index);
return this;
}

/**
Expand Down Expand Up @@ -738,7 +743,7 @@ public void RemoveFormula()
throw new InvalidOperationException();
}
}
public void SetCellFormula(String formula)
public ICell SetCellFormula(String formula)
{
if (IsPartOfArrayFormulaGroup)
{
Expand All @@ -752,7 +757,7 @@ public void SetCellFormula(String formula)
{
NotifyFormulaChanging();
SetCellType(CellType.Blank, false, row, col, styleIndex);
return;
return this;
}
int sheetIndex = book.GetSheetIndex(_sheet);
Ptg[] ptgs = HSSFFormulaParser.Parse(formula, book, FormulaType.Cell, sheetIndex);
Expand All @@ -769,6 +774,7 @@ public void SetCellFormula(String formula)
agg.XFIndex = ((short)0x0f);
}
agg.SetParsedExpression(ptgs);
return this;
}

/// <summary>
Expand Down Expand Up @@ -946,7 +952,7 @@ public IRichTextString RichStringCellValue
/// <param name="value">the bool value to Set this cell to. For formulas we'll Set the
/// precalculated value, for bools we'll Set its value. For other types we
/// will Change the cell to a bool cell and Set its value.</param>
public void SetCellValue(bool value)
public ICell SetCellValue(bool value)
{
int row = _record.Row;
int col = _record.Column;
Expand All @@ -964,6 +970,7 @@ public void SetCellValue(bool value)
((BoolErrRecord)_record).SetValue(value);
break;
}
return this;
}
/// <summary>
/// Chooses a new bool value for the cell when its type is changing.
Expand Down Expand Up @@ -1462,9 +1469,9 @@ public CellType GetCachedFormulaResultTypeEnum()
throw new NotImplementedException();
}

public void SetBlank()
public ICell SetBlank()
{
SetCellType(CellType.Blank);
return SetCellType(CellType.Blank);
}

public bool IsMergedCell
Expand Down
20 changes: 10 additions & 10 deletions main/SS/UserModel/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ CellType CellType
/// Set the cells type (numeric, formula or string)
/// </summary>
/// <param name="cellType"></param>
void SetCellType(CellType cellType);
ICell SetCellType(CellType cellType);

void SetBlank();
ICell SetBlank();
/// <summary>
/// Only valid for formula cells
/// </summary>
Expand All @@ -105,7 +105,7 @@ CellType CellType
/// precalculated value, for numerics we'll set its value. For other types we will change
/// the cell to a numeric cell and set its value.
/// </param>
void SetCellValue(double value);
ICell SetCellValue(double value);

/// <summary>
/// Set a error value for the cell
Expand All @@ -114,7 +114,7 @@ CellType CellType
/// precalculated value , for errors we'll set its value. For other types we will change
/// the cell to an error cell and set its value.
/// </param>
void SetCellErrorValue(byte value);
ICell SetCellErrorValue(byte value);

/// <summary>
/// Converts the supplied date to its equivalent Excel numeric value and Sets that into the cell.
Expand All @@ -123,7 +123,7 @@ CellType CellType
/// precalculated value, for numerics we'll set its value. For other types we will change
/// the cell to a numerics cell and set its value.
/// </param>
void SetCellValue(DateTime value);
ICell SetCellValue(DateTime value);

#if NET6_0_OR_GREATER
/// <summary>
Expand All @@ -133,7 +133,7 @@ CellType CellType
/// precalculated value, for numerics we'll set its value. For other types we will change
/// the cell to a numerics cell and set its value.
/// </param>
void SetCellValue(DateOnly value);
ICell SetCellValue(DateOnly value);
#endif

/// <summary>
Expand All @@ -144,7 +144,7 @@ CellType CellType
/// change the cell to a string cell and set its value.
/// If value is null then we will change the cell to a Blank cell.
/// </param>
void SetCellValue(IRichTextString value);
ICell SetCellValue(IRichTextString value);

/// <summary>
/// Set a string value for the cell.
Expand All @@ -154,7 +154,7 @@ CellType CellType
/// change the cell to a string cell and set its value.
/// If value is null then we will change the cell to a blank cell.
/// </param>
void SetCellValue(String value);
ICell SetCellValue(String value);

/// <summary>
/// Copy the cell to the target index. If the target cell exists, a new cell will be inserted before the existing cell.
Expand All @@ -180,7 +180,7 @@ CellType CellType
/// Sets formula for this cell.
/// </summary>
/// <param name="formula">the formula to Set, e.g. <code>"SUM(C4:E4)"</code>.</param>
void SetCellFormula(String formula);
ICell SetCellFormula(String formula);

/// <summary>
/// Get the value of the cell as a number.
Expand Down Expand Up @@ -224,7 +224,7 @@ CellType CellType
/// Set a bool value for the cell
/// </summary>
/// <param name="value"></param>
void SetCellValue(bool value);
ICell SetCellValue(bool value);

/// <summary>
/// Get the value of the cell as a bool.
Expand Down
Loading
Loading