Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/nissl-lab/npoi
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyqus committed May 4, 2024
2 parents 7a4daeb + 09966aa commit 1ff4c6e
Show file tree
Hide file tree
Showing 39 changed files with 860 additions and 521 deletions.
25 changes: 17 additions & 8 deletions OpenXmlFormats/Vml/OfficeDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2385,10 +2385,10 @@ public class CT_Entry
private int? oldField = null;

[XmlAttribute]
public int @new
public int? @new
{
get { return (int)this.newField; }
set { this.newField = value; }
get { return newField; }
set { newField = value; }
}
[XmlIgnore]
public bool newSpecified
Expand All @@ -2397,10 +2397,10 @@ public bool newSpecified
}

[XmlAttribute]
public int old
public int? old
{
get { return (int)this.oldField; }
set { this.oldField = value; }
get { return oldField; }
set { oldField = value; }
}
[XmlIgnore]
public bool oldSpecified
Expand All @@ -2424,8 +2424,17 @@ public static CT_Entry Parse(XmlNode node, XmlNamespaceManager namespaceManager)
internal void Write(StreamWriter sw, string nodeName)
{
sw.Write(string.Format("<o:{0}", nodeName));
XmlHelper.WriteAttribute(sw, "new", this.@new);
XmlHelper.WriteAttribute(sw, "old", this.old);

if(@new.HasValue)
{
XmlHelper.WriteAttribute(sw, "new", @new.Value);
}

if(old.HasValue)
{
XmlHelper.WriteAttribute(sw, "old", old.Value);
}

sw.Write(">");
sw.Write(string.Format("</o:{0}>", nodeName));
}
Expand Down
4 changes: 4 additions & 0 deletions main/HSSF/Extractor/OldExcelExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace NPOI.HSSF.Extractor
*/
public class OldExcelExtractor
{
private const int FILE_PASS_RECORD_SID = 0x2f;
private RecordInputStream ris;

// sometimes we hold the stream here and thus need to ensure it is closed at some point
Expand Down Expand Up @@ -247,6 +248,9 @@ public String Text

switch (sid)
{
case FILE_PASS_RECORD_SID:
throw new EncryptedDocumentException("Encryption not supported for Old Excel files");

// Biff 5+ only, no sheet names in older formats
case OldSheetRecord.sid:
OldSheetRecord shr = new OldSheetRecord(ris);
Expand Down
5 changes: 1 addition & 4 deletions main/HSSF/Record/EscherAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,10 +1298,7 @@ public void RemoveTailRecord(NoteRecord note)

internal void AddTailRecord(NoteRecord note)
{
if (tailRec.ContainsKey(note.ShapeId))
tailRec.Add(note.ShapeId, note);
else
tailRec[note.ShapeId] = note;
tailRec[note.ShapeId] = note;
}
/**
* @return unmodifiable copy of tail records. We need to access them when building shapes.
Expand Down
2 changes: 1 addition & 1 deletion main/NPOI.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<PackageReference Include="Enums.NET" Version="4.0.1" />
<PackageReference Include="ExtendedNumerics.BigDecimal" Version="2023.1000.0.230" />
<PackageReference Include="ExtendedNumerics.BigDecimal" Version="2025.1000.2.122" />
<PackageReference Include="MathNet.Numerics.Signed" Version="5.0.0" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />
<PackageReference Include="BouncyCastle.Cryptography" Version="2.3.0" />
Expand Down
133 changes: 49 additions & 84 deletions main/SS/Formula/Eval/OperandResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public class OperandResolver
{
// Based on regular expression defined in JavaDoc at {@link java.lang.Double#valueOf}
// modified to remove support for NaN, Infinity, Hexadecimal support and floating type suffixes
private const String Digits = "\\d+";
private const String Exp = "[eE][+-]?" + Digits;
private const String fpRegex =
private const string Digits = "\\d+";
private const string Exp = "[eE][+-]?" + Digits;
private const string fpRegex =
("[\\x00-\\x20]*" +
"[+-]?(" +
"(((" + Digits + "(\\.)?(" + Digits + "?)(" + Exp + ")?)|" +
Expand All @@ -58,21 +58,21 @@ private OperandResolver()
public static ValueEval GetSingleValue(ValueEval arg, int srcCellRow, int srcCellCol)
{
ValueEval result;
if (arg is RefEval)
if (arg is RefEval rev)
{
result = ChooseSingleElementFromRef((RefEval)arg);
result = ChooseSingleElementFromRef(rev);
}
else if (arg is AreaEval)
else if (arg is AreaEval aev)
{
result = ChooseSingleElementFromArea((AreaEval)arg, srcCellRow, srcCellCol);
result = ChooseSingleElementFromArea(aev, srcCellRow, srcCellCol);
}
else
{
result = arg;
}
if (result is ErrorEval)
if (result is ErrorEval eva)
{
throw new EvaluationException((ErrorEval)result);
throw new EvaluationException(eva);
}
return result;
}
Expand Down Expand Up @@ -126,9 +126,9 @@ public static ValueEval ChooseSingleElementFromArea(AreaEval ae,
{
ValueEval result = ChooseSingleElementFromAreaInternal(ae, srcCellRow, srcCellCol);

if (result is ErrorEval)
if (result is ErrorEval eva)
{
throw new EvaluationException((ErrorEval)result);
throw new EvaluationException(eva);

}
return result;
Expand Down Expand Up @@ -236,14 +236,14 @@ public static double CoerceValueToDouble(ValueEval ev)
{
return 0.0;
}
if (ev is NumericValueEval)
if (ev is NumericValueEval nve)
{
// this also handles bools
return ((NumericValueEval)ev).NumberValue;
return nve.NumberValue;
}
if (ev is StringEval)
if (ev is StringEval sev)
{
double dd = ParseDouble(((StringEval)ev).StringValue);
double dd = ParseDouble(sev.StringValue);
if (double.IsNaN(dd))
{
throw EvaluationException.InvalidValue();
Expand All @@ -269,64 +269,29 @@ public static double CoerceValueToDouble(ValueEval ev)
* @param text
* @return <c>null</c> if the specified text cannot be Parsed as a number
*/
public static double ParseDouble(String pText)
public static double ParseDouble(string pText)
{
//if (Regex.Match(fpRegex, pText).Success)
try
{
double ret = double.Parse(pText, CultureInfo.CurrentCulture);
if (double.IsInfinity(ret))
return double.NaN;
return ret;
}
catch
{
return Double.NaN;
}
//else
try
{
//return Double.NaN;
double ret = double.Parse(pText, CultureInfo.CurrentCulture);
if (double.IsInfinity(ret))
return double.NaN;
return ret;
}
catch
{
return double.NaN;
}
//String text = pText.Trim();
//if (text.Length < 1)
//{
// return double.NaN;
//}
//bool isPositive = true;
//if (text[0] == '-')
//{
// isPositive = false;
// text = text.Substring(1).Trim();
//}

//if (text.Length == 0 || !Char.IsDigit(text[0]))
//{
// // avoid using Exception to tell when string is not a number
// return double.NaN;
//}
//// TODO - support notation like '1E3' (==1000)

//double val;
//try
//{
// val = double.Parse(text);
//}
//catch
//{
// return double.NaN;
//}
//return isPositive ? +val : -val;
}

/**
* @param ve must be a <c>NumberEval</c>, <c>StringEval</c>, <c>BoolEval</c>, or <c>BlankEval</c>
* @return the Converted string value. never <c>null</c>
*/
public static String CoerceValueToString(ValueEval ve)
public static string CoerceValueToString(ValueEval ve)
{
if (ve is StringValueEval)
if (ve is StringValueEval sve)
{
StringValueEval sve = (StringValueEval)ve;
return sve.StringValue;
}

Expand All @@ -336,30 +301,31 @@ public static String CoerceValueToString(ValueEval ve)
}
throw new ArgumentException("Unexpected eval class (" + ve.GetType().Name + ")");
}

/**
* @return <c>null</c> to represent blank values
* @throws EvaluationException if ve is an ErrorEval, or if a string value cannot be converted
*/
public static Boolean? CoerceValueToBoolean(ValueEval ve, bool stringsAreBlanks)
* @return <c>null</c> to represent blank values
* @throws EvaluationException if ve is an ErrorEval, or if a string value cannot be converted
*/
public static bool? CoerceValueToBoolean(ValueEval ve, bool stringsAreBlanks)
{

if (ve == null || ve == BlankEval.instance)
{
// TODO - remove 've == null' condition once AreaEval is fixed
return null;
}
if (ve is BoolEval)
if (ve is BoolEval be)
{
return ((BoolEval)ve).BooleanValue;
return be.BooleanValue;
}

if (ve is StringEval)
if (ve is StringEval se)
{
if (stringsAreBlanks)
{
return null;
}
String str = ((StringEval)ve).StringValue;
string str = se.StringValue;
if (str.Equals("true", StringComparison.OrdinalIgnoreCase))
{
return true;
Expand All @@ -372,32 +338,31 @@ public static String CoerceValueToString(ValueEval ve)
throw new EvaluationException(ErrorEval.VALUE_INVALID);
}

if (ve is NumericValueEval)
if(ve is NumericValueEval ne)
{
NumericValueEval ne = (NumericValueEval)ve;
double d = ne.NumberValue;
if (Double.IsNaN(d))
if(double.IsNaN(d))
{
throw new EvaluationException(ErrorEval.VALUE_INVALID);
}
return d != 0;
}
if (ve is ErrorEval)
if (ve is ErrorEval ee)
{
throw new EvaluationException((ErrorEval)ve);
throw new EvaluationException(ee);
}
throw new InvalidOperationException("Unexpected eval (" + ve.GetType().Name + ")");
}
/**
* Retrieves a single value from an area evaluation utilizing the 2D indices of the cell
* within its own area reference to index the value in the area evaluation.
*
* @param ae area reference after evaluation
* @param cell the source cell of the formula that contains its 2D indices
* @return a <tt>NumberEval</tt>, <tt>StringEval</tt>, <tt>BoolEval</tt> or <tt>BlankEval</tt>. or <tt>ErrorEval<tt>
* Never <code>null</code>.
*/

/**
* Retrieves a single value from an area evaluation utilizing the 2D indices of the cell
* within its own area reference to index the value in the area evaluation.
*
* @param ae area reference after evaluation
* @param cell the source cell of the formula that contains its 2D indices
* @return a <tt>NumberEval</tt>, <tt>StringEval</tt>, <tt>BoolEval</tt> or <tt>BlankEval</tt>. or <tt>ErrorEval<tt>
* Never <code>null</code>.
*/
public static ValueEval GetElementFromArray(AreaEval ae, IEvaluationCell cell)
{
CellRangeAddress range = cell.ArrayFormulaRange;
Expand Down
14 changes: 5 additions & 9 deletions main/SS/Formula/Eval/StringEval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,23 @@ public class StringEval : StringValueEval
{
public static readonly StringEval EMPTY_INSTANCE = new StringEval("");

private String value;
private readonly string value;

public StringEval(Ptg ptg):this(((StringPtg)ptg).Value)
{

}

public StringEval(String value)
public StringEval(string value)
{
if (value == null)
{
throw new ArgumentException("value must not be null");
}
this.value = value;
this.value = value ?? throw new ArgumentException("value must not be null");
}

public String StringValue
public string StringValue
{
get { return value; }
}
public override String ToString()
public override string ToString()
{
StringBuilder sb = new StringBuilder(64);
sb.Append(GetType().Name).Append(" [");
Expand Down
Loading

0 comments on commit 1ff4c6e

Please sign in to comment.