Skip to content

Commit

Permalink
Merge branch 'master' into getcellrange
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyqus authored Dec 12, 2024
2 parents 6b57bc7 + bcdb67f commit a10a86c
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 139 deletions.
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
4 changes: 4 additions & 0 deletions main/SS/UserModel/DataFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,10 @@ Excel displays the month instead of minutes."
}
else
{
if (Char.IsWhiteSpace(c))
{
ms.Clear();
}
sb.Append(c);
}
}
Expand Down
10 changes: 9 additions & 1 deletion main/SS/UserModel/DateUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ public class DateUtil
private static Regex date_ptrn1 = new Regex("^\\[\\$\\-.*?\\]", RegexOptions.Compiled);
private static Regex date_ptrn2 = new Regex("^\\[[a-zA-Z]+\\]", RegexOptions.Compiled);
private static Regex date_ptrn3a = new Regex("[yYmMdDhHsS]", RegexOptions.Compiled);
private static Regex date_ptrn3b = new Regex("^[\\[\\]yYmMdDhHsS\\-T/,. :\"\\\\]+0*[ampAMP/]*$", RegexOptions.Compiled);
// add "\u5e74 \u6708 \u65e5"£¨ÄêÔÂÈÕ£© for Chinese/Japanese date format:2017Äê2ÔÂ7ÈÕ
private static Regex date_ptrn3b = new Regex("^[\\[\\]yYmMdDhHsS\\-T/\u5e74\u6708\u65e5,. :\"\\\\]+0*[ampAMP/]*$", RegexOptions.Compiled);

// elapsed time patterns: [h],[m] and [s]
//private static Regex date_ptrn4 = new Regex("^\\[([hH]+|[mM]+|[sS]+)\\]", RegexOptions.Compiled);
private static Regex date_ptrn4 = new Regex("^\\[([hH]+|[mM]+|[sS]+)\\]$", RegexOptions.Compiled);

// for format which start with "[DBNum1]" or "[DBNum2]" or "[DBNum3]" could be a Chinese date
private static Regex date_ptrn5 = new Regex("^\\[DBNum(1|2|3)\\]", RegexOptions.Compiled);

/// <summary>
/// Given a Calendar, return the number of days since 1899/12/31.
Expand Down Expand Up @@ -672,6 +676,10 @@ public static bool IsADateFormat(int formatIndex, String formatString)
return true;
}

// If it starts with [DBNum1] or [DBNum2] or [DBNum3]
// then it could be a Chinese date
fs = date_ptrn5.Replace(fs, "");

// If it starts with [$-...], then could be a date, but
// who knows what that starting bit Is all about
//fs = Regex.Replace(fs, "^\\[\\$\\-.*?\\]", "");
Expand Down
Loading

0 comments on commit a10a86c

Please sign in to comment.