diff --git a/main/HSSF/UserModel/HSSFCell.cs b/main/HSSF/UserModel/HSSFCell.cs index 67c13a5ff..8ccd967ed 100644 --- a/main/HSSF/UserModel/HSSFCell.cs +++ b/main/HSSF/UserModel/HSSFCell.cs @@ -229,7 +229,7 @@ public IRow Row /// Set the cells type (numeric, formula or string) /// /// Type of the cell. - public void SetCellType(CellType cellType) + public ICell SetCellType(CellType cellType) { NotifyFormulaChanging(); if (IsPartOfArrayFormulaGroup) @@ -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; } /// @@ -467,7 +468,7 @@ private String ConvertCellValueToString() /// 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. - public void SetCellValue(double value) + public ICell SetCellValue(double value) { if(double.IsInfinity(value)) { @@ -501,7 +502,8 @@ public void SetCellValue(double value) break; } } - + + return this; } /// @@ -511,9 +513,10 @@ public void SetCellValue(double 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. - public void SetCellValue(DateTime value) + public ICell SetCellValue(DateTime value) { SetCellValue(DateUtil.GetExcelDate(value, this.book.IsDate1904())); + return this; } #if NET6_0_OR_GREATER @@ -524,9 +527,9 @@ public void SetCellValue(DateTime 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. - 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 @@ -539,10 +542,11 @@ 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. - public void SetCellValue(String value) + public ICell SetCellValue(String value) { HSSFRichTextString str = value == null ? null : new HSSFRichTextString(value); SetCellValue(str); + return this; } /** * set a error value for the cell @@ -553,10 +557,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 @@ -566,7 +570,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; @@ -585,6 +589,8 @@ public void SetCellErrorValue(FormulaError error) ((BoolErrRecord)_record).SetValue(error); break; } + + return this; } /// /// Set a string value for the cell. Please note that if you are using @@ -594,7 +600,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. - public void SetCellValue(IRichTextString value) + public ICell SetCellValue(IRichTextString value) { int row = _record.Row; int col = _record.Column; @@ -603,7 +609,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) @@ -618,7 +624,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) @@ -634,6 +640,7 @@ public void SetCellValue(IRichTextString value) stringValue = hvalue; stringValue.SetWorkbookReferences(book.Workbook, ((LabelSSTRecord)_record)); stringValue.UnicodeString = book.Workbook.GetSSTString(index); + return this; } /** @@ -738,7 +745,7 @@ public void RemoveFormula() throw new InvalidOperationException(); } } - public void SetCellFormula(String formula) + public ICell SetCellFormula(String formula) { if (IsPartOfArrayFormulaGroup) { @@ -752,7 +759,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); @@ -769,6 +776,7 @@ public void SetCellFormula(String formula) agg.XFIndex = ((short)0x0f); } agg.SetParsedExpression(ptgs); + return this; } /// @@ -946,7 +954,7 @@ public IRichTextString RichStringCellValue /// 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. - public void SetCellValue(bool value) + public ICell SetCellValue(bool value) { int row = _record.Row; int col = _record.Column; @@ -964,6 +972,7 @@ public void SetCellValue(bool value) ((BoolErrRecord)_record).SetValue(value); break; } + return this; } /// /// Chooses a new bool value for the cell when its type is changing. @@ -1462,9 +1471,9 @@ public CellType GetCachedFormulaResultTypeEnum() throw new NotImplementedException(); } - public void SetBlank() + public ICell SetBlank() { - SetCellType(CellType.Blank); + return SetCellType(CellType.Blank); } public bool IsMergedCell diff --git a/main/SS/UserModel/Cell.cs b/main/SS/UserModel/Cell.cs index f18ba0051..298a385df 100644 --- a/main/SS/UserModel/Cell.cs +++ b/main/SS/UserModel/Cell.cs @@ -89,9 +89,9 @@ CellType CellType /// Set the cells type (numeric, formula or string) /// /// - void SetCellType(CellType cellType); + ICell SetCellType(CellType cellType); - void SetBlank(); + ICell SetBlank(); /// /// Only valid for formula cells /// @@ -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. /// - void SetCellValue(double value); + ICell SetCellValue(double value); /// /// Set a error value for the cell @@ -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. /// - void SetCellErrorValue(byte value); + ICell SetCellErrorValue(byte value); /// /// Converts the supplied date to its equivalent Excel numeric value and Sets that into the cell. @@ -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. /// - void SetCellValue(DateTime value); + ICell SetCellValue(DateTime value); #if NET6_0_OR_GREATER /// @@ -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. /// - void SetCellValue(DateOnly value); + ICell SetCellValue(DateOnly value); #endif /// @@ -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. /// - void SetCellValue(IRichTextString value); + ICell SetCellValue(IRichTextString value); /// /// Set a string value for the cell. @@ -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. /// - void SetCellValue(String value); + ICell SetCellValue(String value); /// /// Copy the cell to the target index. If the target cell exists, a new cell will be inserted before the existing cell. @@ -180,7 +180,7 @@ CellType CellType /// Sets formula for this cell. /// /// the formula to Set, e.g. "SUM(C4:E4)". - void SetCellFormula(String formula); + ICell SetCellFormula(String formula); /// /// Get the value of the cell as a number. @@ -224,7 +224,7 @@ CellType CellType /// Set a bool value for the cell /// /// - void SetCellValue(bool value); + ICell SetCellValue(bool value); /// /// Get the value of the cell as a bool. diff --git a/ooxml/XSSF/Streaming/SXSSFCell.cs b/ooxml/XSSF/Streaming/SXSSFCell.cs index c37010cfa..36f9d22bd 100644 --- a/ooxml/XSSF/Streaming/SXSSFCell.cs +++ b/ooxml/XSSF/Streaming/SXSSFCell.cs @@ -446,7 +446,7 @@ public void SetAsActiveCell() Sheet.ActiveCell = Address; } - public void SetCellErrorValue(byte value) + public ICell SetCellErrorValue(byte value) { //ensure type garuntees that the type is error so the if condition is never true. EnsureType(CellType.Error); @@ -454,26 +454,29 @@ public void SetCellErrorValue(byte value) ((ErrorFormulaValue)_value).PreEvaluatedValue = value; else ((ErrorValue)_value).Value = value; + return this; } - public void SetCellFormula(string formula) + public ICell SetCellFormula(string formula) { if (formula == null) { SetType(CellType.Blank); - return; + return this; } EnsureFormulaType(ComputeTypeFromFormula(formula)); ((FormulaValue)_value).Value = formula; + return this; } - public void SetCellType(CellType cellType) + public ICell SetCellType(CellType cellType) { EnsureType(cellType); + return this; } - public void SetCellValue(string value) + public ICell SetCellValue(string value) { if (value != null) { @@ -500,18 +503,21 @@ public void SetCellValue(string value) { SetCellType(CellType.Blank); } + + return this; } - public void SetCellValue(bool value) + public ICell SetCellValue(bool value) { EnsureTypeOrFormulaType(CellType.Boolean); if (_value.GetType() == CellType.Formula) ((BooleanFormulaValue)_value).PreEvaluatedValue = value; else ((BooleanValue)_value).Value = value; + return this; } - public void SetCellValue(IRichTextString value) + public ICell SetCellValue(IRichTextString value) { XSSFRichTextString xvalue = (XSSFRichTextString)value; @@ -533,21 +539,24 @@ public void SetCellValue(IRichTextString value) { SetCellType(CellType.Blank); } + + return this; } - public void SetCellValue(DateTime? value) + public ICell SetCellValue(DateTime? value) { if (value == null) { SetCellType(CellType.Blank); - return; + return this; } bool date1904 = ((SXSSFWorkbook)Sheet.Workbook).XssfWorkbook.IsDate1904(); SetCellValue(DateUtil.GetExcelDate(value.Value, date1904)); + return this; } - public void SetCellValue(double value) + public ICell SetCellValue(double value) { if (Double.IsInfinity(value)) { @@ -567,6 +576,8 @@ public void SetCellValue(double value) else ((NumericValue)_value).Value = value; } + + return this; } public override string ToString() @@ -893,33 +904,34 @@ private String ConvertCellValueToString(CellType cellType) //END OF COPIED CODE - public void SetCellValue(DateTime value) + public ICell SetCellValue(DateTime value) { - SetCellValue((DateTime?)value); + return SetCellValue((DateTime?)value); } #if NET6_0_OR_GREATER - public void SetCellValue(DateOnly value) + public ICell SetCellValue(DateOnly value) { bool date1904 = ((SXSSFWorkbook)Sheet.Workbook).XssfWorkbook.IsDate1904(); - SetCellValue(DateUtil.GetExcelDate(value, date1904)); + return SetCellValue(DateUtil.GetExcelDate(value, date1904)); } - public void SetCellValue(DateOnly? value) + public ICell SetCellValue(DateOnly? value) { if (!value.HasValue) { SetCellType(CellType.Blank); - return; + return this; } SetCellValue(value.Value); + return this; } #endif - public void SetBlank() + public ICell SetBlank() { - SetCellType(CellType.Blank); + return SetCellType(CellType.Blank); } } } diff --git a/ooxml/XSSF/UserModel/XSSFCell.cs b/ooxml/XSSF/UserModel/XSSFCell.cs index 0b625da9c..54ac540a7 100644 --- a/ooxml/XSSF/UserModel/XSSFCell.cs +++ b/ooxml/XSSF/UserModel/XSSFCell.cs @@ -283,10 +283,11 @@ public bool BooleanCellValue * 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. */ - public void SetCellValue(bool value) + public ICell SetCellValue(bool value) { _cell.t = (ST_CellType.b); _cell.v = (value ? TRUE_AS_STRING : FALSE_AS_STRING); + return this; } /** @@ -342,7 +343,7 @@ public double NumericCellValue * 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. */ - public void SetCellValue(double value) + public ICell SetCellValue(double value) { if (Double.IsInfinity(value)) { @@ -363,6 +364,8 @@ public void SetCellValue(double value) _cell.t = (ST_CellType.n); _cell.v = (value.ToString(CultureInfo.InvariantCulture)); } + + return this; } /** @@ -464,9 +467,9 @@ private static void CheckFormulaCachedValueType(CellType expectedTypeCode, CellT * 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. */ - public void SetCellValue(String str) + public ICell SetCellValue(String str) { - SetCellValue(str == null ? null : new XSSFRichTextString(str)); + return SetCellValue(str == null ? null : new XSSFRichTextString(str)); } /** @@ -477,12 +480,12 @@ public void SetCellValue(String str) * 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. */ - public void SetCellValue(IRichTextString str) + public ICell SetCellValue(IRichTextString str) { if (str == null || str.String == null) { SetCellType(CellType.Blank); - return; + return this; } if (str.Length > SpreadsheetVersion.EXCEL2007.MaxTextLength) @@ -512,6 +515,8 @@ public void SetCellValue(IRichTextString str) } break; } + + return this; } /// @@ -617,13 +622,13 @@ private String ConvertSharedFormula(int si, XSSFEvaluationWorkbook fpb) * @throws InvalidOperationException if the operation is not allowed, for example, * when the cell is a part of a multi-cell array formula */ - public void SetCellFormula(String formula) + public ICell SetCellFormula(String formula) { if (IsPartOfArrayFormulaGroup) { NotifyArrayFormulaChanging(); } - SetFormula(formula, FormulaType.Cell); + return SetFormula(formula, FormulaType.Cell); } internal void SetCellArrayFormula(String formula, CellRangeAddress range) @@ -655,13 +660,13 @@ internal void TryToDeleteArrayFormula(String message) //un-register the single-cell array formula from the parent sheet through public interface Row.Sheet.RemoveArrayFormula(this); } - private void SetFormula(String formula, FormulaType formulaType) + private ICell SetFormula(String formula, FormulaType formulaType) { XSSFWorkbook wb = (XSSFWorkbook)_row.Sheet.Workbook; if (formula == null) { RemoveFormula(); - return; + return this; } if (wb.CellFormulaValidation) @@ -670,10 +675,10 @@ private void SetFormula(String formula, FormulaType formulaType) //validate through the FormulaParser FormulaParser.Parse(formula, fpb, formulaType, wb.GetSheetIndex(this.Sheet), RowIndex); } - CT_CellFormula f = new CT_CellFormula(); - f.Value = formula; - _cell.f= (f); + CT_CellFormula f = new CT_CellFormula { Value = formula }; + _cell.f= f; if (_cell.IsSetV()) _cell.unsetV(); + return this; } /// @@ -889,28 +894,28 @@ public void SetCellValue(DateTime? 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. - public void SetCellValue(DateTime value) + public ICell SetCellValue(DateTime value) { bool date1904 = Sheet.Workbook.IsDate1904(); - SetCellValue(DateUtil.GetExcelDate(value, date1904)); + return SetCellValue(DateUtil.GetExcelDate(value, date1904)); } #if NET6_0_OR_GREATER - public void SetCellValue(DateOnly value) + public ICell SetCellValue(DateOnly value) { bool date1904 = Sheet.Workbook.IsDate1904(); - SetCellValue(DateUtil.GetExcelDate(value, date1904)); + return SetCellValue(DateUtil.GetExcelDate(value, date1904)); } - public void SetCellValue(DateOnly? value) + public ICell SetCellValue(DateOnly? value) { if (value == null) { SetCellType(CellType.Blank); - return; + return this; } - SetCellValue(value.Value); + return SetCellValue(value.Value); } #endif @@ -946,10 +951,10 @@ public byte ErrorCellValue return FormulaError.ForString(code).Code; } } - 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 @@ -958,10 +963,11 @@ public void SetCellErrorValue(byte errorCode) /// For formulas we'll Set the 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. /// - public void SetCellErrorValue(FormulaError error) + public ICell SetCellErrorValue(FormulaError error) { - _cell.t = (ST_CellType.e); - _cell.v = (error.String); + _cell.t = ST_CellType.e; + _cell.v = error.String; + return this; } /// @@ -983,9 +989,9 @@ private void SetBlankInternal() if (_cell.IsSetS()) blank.s=(_cell.s); _cell.Set(blank); } - public void SetBlank() + public ICell SetBlank() { - SetCellType(CellType.Blank); + return SetCellType(CellType.Blank); } /// @@ -1003,7 +1009,7 @@ internal void SetCellNum(int num) /// Set the cells type (numeric, formula or string) /// /// - public void SetCellType(CellType cellType) + public ICell SetCellType(CellType cellType) { CellType prevType = CellType; @@ -1059,6 +1065,8 @@ public void SetCellType(CellType cellType) { _cell.unsetF(); } + + return this; } /// /// Returns a string representation of the cell