Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix more test cases #312

Merged
merged 10 commits into from
Jan 30, 2020
12 changes: 7 additions & 5 deletions main/SS/Formula/WorkbookEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class WorkbookEvaluator
private int _workbookIx;

private IEvaluationListener _evaluationListener;
private Hashtable _sheetIndexesBySheet;
private Dictionary<IEvaluationSheet, int> _sheetIndexesBySheet;
private Dictionary<String, int> _sheetIndexesByName;
private CollaboratingWorkbooksEnvironment _collaboratingWorkbookEnvironment;
private IStabilityClassifier _stabilityClassifier;
Expand All @@ -71,7 +71,7 @@ public WorkbookEvaluator(IEvaluationWorkbook workbook, IEvaluationListener evalu
_workbook = workbook;
_evaluationListener = evaluationListener;
_cache = new EvaluationCache(evaluationListener);
_sheetIndexesBySheet = new Hashtable();
_sheetIndexesBySheet = new Dictionary<IEvaluationSheet, int>();
_sheetIndexesByName = new Dictionary<string, int>();
_collaboratingWorkbookEnvironment = CollaboratingWorkbooksEnvironment.EMPTY;
_workbookIx = 0;
Expand Down Expand Up @@ -208,8 +208,10 @@ public void NotifyDeleteCell(IEvaluationCell cell)

public int GetSheetIndex(IEvaluationSheet sheet)
{
object result = _sheetIndexesBySheet[sheet];
if (result == null)
int result = int.MinValue;
if(_sheetIndexesBySheet.ContainsKey(sheet))
result = _sheetIndexesBySheet[sheet];
if (result == int.MinValue)
{
int sheetIndex = _workbook.GetSheetIndex(sheet);
if (sheetIndex < 0)
Expand All @@ -219,7 +221,7 @@ public int GetSheetIndex(IEvaluationSheet sheet)
result = sheetIndex;
_sheetIndexesBySheet[sheet] = result;
}
return (int)result;
return result;
}
/* package */
internal int GetSheetIndexByExternIndex(int externSheetIndex)
Expand Down
40 changes: 30 additions & 10 deletions main/SS/UserModel/DataFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ static DataFormatter()
*/
private DateTimeFormatInfo dateSymbols;

/**
* A default date format, if no date format was given
*/
private DateFormat defaultDateformat;

/** <em>General</em> FormatBase for whole numbers. */
//private static DecimalFormat generalWholeNumFormat = new DecimalFormat("0");
private FormatBase generalNumberFormat;
Expand Down Expand Up @@ -232,6 +237,15 @@ public DataFormatter(CultureInfo culture, bool localeIsAdapting, bool emulateCSV
// adapt to the current user locale as the locale changes)
this.localeIsAdapting = localeIsAdapting;
this.emulateCSV = emulateCSV;

dateSymbols = culture.DateTimeFormat;
decimalSymbols = culture.NumberFormat;
generalNumberFormat = new ExcelGeneralNumberFormat(culture);

// default format in dot net is not same as in java
defaultDateformat = new SimpleDateFormat(dateSymbols.FullDateTimePattern, dateSymbols);
defaultDateformat.TimeZone = TimeZoneInfo.Local;

formats = new Hashtable();

// init built-in Formats
Expand Down Expand Up @@ -821,12 +835,14 @@ private FormatBase GetDefaultFormat(double cellValue)
private String GetFormattedDateString(ICell cell)
{
FormatBase dateFormat = GetFormat(cell);
DateTime d = cell.DateCellValue;
if (dateFormat != null)
{
return dateFormat.Format(d, currentCulture);
if (dateFormat is ExcelStyleDateFormatter) {
// Hint about the raw excel value
((ExcelStyleDateFormatter)dateFormat).SetDateToBeFormatted(
cell.NumericCellValue
);
}
return d.ToString();
DateTime d = cell.DateCellValue;
return PerformDateFormatting(d, dateFormat);
}

/**
Expand All @@ -849,6 +865,10 @@ private String GetFormattedNumberString(ICell cell)
}
//return numberFormat.Format(d, currentCulture);
String formatted = numberFormat.Format(d);
if (formatted.StartsWith("."))
formatted = "0" + formatted;
if (formatted.StartsWith("-."))
formatted = "-0" + formatted.Substring(1);
//return formatted.ReplaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation
return Regex.Replace(formatted, "E(\\d)", "E+$1");
}
Expand All @@ -863,16 +883,16 @@ public String FormatRawCellContents(double value, int formatIndex, String format
return FormatRawCellContents(value, formatIndex, formatString, false);
}
/**
* Performs Excel-style date formatting, using the
* supplied Date and format
*/
* Performs Excel-style date formatting, using the
* supplied Date and format
*/
private String PerformDateFormatting(DateTime d, FormatBase dateFormat)
{
if (dateFormat != null)
{
return dateFormat.Format(d, currentCulture);
return dateFormat.Format(d);
}
return d.ToString();
return defaultDateformat.Format(d);
}
/**
* Formats the given raw cell value, based on the supplied
Expand Down
19 changes: 16 additions & 3 deletions main/SS/UserModel/ExcelGeneralNumberFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ExcelGeneralNumberFormat : FormatBase
private DecimalFormat integerFormat;
private DecimalFormat decimalFormat;
private DecimalFormat scientificFormat;

private CultureInfo culture;
public ExcelGeneralNumberFormat(CultureInfo culture)
{
decimalSymbols = culture.NumberFormat;// DecimalFormatSymbols.GetInstance(locale);
Expand All @@ -52,14 +52,20 @@ public ExcelGeneralNumberFormat(CultureInfo culture)
//DataFormatter.SetExcelStyleRoundingMode(/*setter*/integerFormat);
decimalFormat = new DecimalFormat("#.##########");//, decimalSymbols);
//DataFormatter.ExcelStyleRoundingMode = (/*setter*/decimalFormat);
this.culture = culture;
}

public override StringBuilder Format(object obj, StringBuilder toAppendTo, int pos)
{
return Format(obj, toAppendTo, culture);
}

public override StringBuilder Format(Object number, StringBuilder toAppendTo, CultureInfo culture)
{
double value;
if (Number.IsNumber(number))
{
value = ((double)number);
value = double.Parse(number.ToString());
if (Double.IsInfinity(value) || Double.IsNaN(value))
{
return integerFormat.Format(number, toAppendTo, culture);
Expand All @@ -86,7 +92,14 @@ public override StringBuilder Format(Object number, StringBuilder toAppendTo, Cu
// character". We know there is a decimal point, so limit to 10 digits.
// https://support.microsoft.com/en-us/kb/65903
//double rounded = new BigDecimal(value).round(TO_10_SF);
double rounded = Math.Round(value, MidpointRounding.ToEven);
// calculate round precision
int digits = 10;
if (Math.Abs(value) > 1)
{
int len = (int)Math.Log10((int)Math.Abs(value)) + 1;
digits -= len;
}
double rounded = Math.Round(value, digits, MidpointRounding.AwayFromZero);
return decimalFormat.Format(rounded, toAppendTo, culture);
}

Expand Down
2 changes: 1 addition & 1 deletion main/SS/Util/DateFormatConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public static string GetDateTimePattern(int style, CultureInfo locale)

}

public class DateFormat
public abstract class DateFormat : FormatBase
{
public const int FULL = 0;
public const int LONG = 1;
Expand Down
58 changes: 46 additions & 12 deletions main/SS/Util/Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public virtual StringBuilder Format(Object obj, StringBuilder sb, int pos)

//public abstract Object Parse(string source);
public abstract Object ParseObject(string source, int pos);

public TimeZoneInfo TimeZone
{
get;
set;
}
}

/**
Expand Down Expand Up @@ -59,7 +63,10 @@ public override String Format(object obj, CultureInfo culture)
sb.Append(result.Substring(5, 4));
return sb.ToString();
}

public override StringBuilder Format(object obj, StringBuilder toAppendTo, int pos)
{
return toAppendTo.Append(Format(obj, CultureInfo.CurrentCulture));
}
public override StringBuilder Format(Object obj, StringBuilder toAppendTo, CultureInfo culture)
{
return toAppendTo.Append(Format((long)obj, culture));
Expand Down Expand Up @@ -96,6 +103,11 @@ public override String Format(object obj, CultureInfo culture)
return sb.ToString();
}

public override StringBuilder Format(object obj, StringBuilder toAppendTo, int pos)
{
return toAppendTo.Append(Format(obj, CultureInfo.CurrentCulture));
}

public override StringBuilder Format(Object obj, StringBuilder toAppendTo, CultureInfo culture)
{
return toAppendTo.Append(Format(obj, culture));
Expand Down Expand Up @@ -151,7 +163,10 @@ public override String Format(object obj, CultureInfo culture)
sb.Append(seg3);
return sb.ToString();
}

public override StringBuilder Format(object obj, StringBuilder toAppendTo, int pos)
{
return toAppendTo.Append(Format(obj, CultureInfo.CurrentCulture));
}
public override StringBuilder Format(Object obj, StringBuilder toAppendTo, CultureInfo culture)
{
return toAppendTo.Append(Format(obj, culture));
Expand Down Expand Up @@ -222,7 +237,7 @@ public override string Format(object obj, CultureInfo culture)

public override StringBuilder Format(object obj, StringBuilder toAppendTo, CultureInfo culture)
{
return toAppendTo.Append(Format((double)obj, culture));
return toAppendTo.Append(Format(obj, culture));
}

public override object ParseObject(string source, int pos)
Expand All @@ -238,14 +253,37 @@ public bool ParseIntegerOnly

}

public class SimpleDateFormat : FormatBase
public class SimpleDateFormat : DateFormat
{
protected string pattern;
private DateTimeFormatInfo formatData;
private CultureInfo culture;
public SimpleDateFormat()
: this("", CultureInfo.CurrentCulture)
{

}

protected string pattern;
public SimpleDateFormat(String pattern, CultureInfo culture)
{
if (pattern == null || culture == null)
{
throw new ArgumentNullException();
}
this.pattern = pattern;
this.formatData = (DateTimeFormatInfo)culture.DateTimeFormat.Clone();
this.culture = culture;
}

public SimpleDateFormat(String pattern, DateTimeFormatInfo formatSymbols)
{
if (pattern == null || formatSymbols == null)
{
throw new ArgumentNullException();
}
this.pattern = pattern;
this.formatData = (DateTimeFormatInfo)formatSymbols.Clone();
this.culture = CultureInfo.CurrentCulture;
}

public SimpleDateFormat(string pattern)
{
Expand Down Expand Up @@ -283,11 +321,7 @@ public DateTime Parse(string source)
return TimeZoneInfo.ConvertTime(dt, TimeZone);
return dt;
}
public TimeZoneInfo TimeZone
{
get;
set;
}

}


Expand Down
2 changes: 2 additions & 0 deletions main/Util/Number.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public static bool IsNumber(object value)
objType != IntPtrType &&
objType != UIntPtrType);
}
if (decimal.TryParse(value.ToString(), out decimal result))
return true;
//if (value is int) return true;
//if (value is uint) return true;
//if (value is long) return true;
Expand Down
11 changes: 10 additions & 1 deletion ooxml/POIXMLDocumentPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,16 @@ protected void Read(POIXMLFactory factory, Dictionary<PackagePart, POIXMLDocumen
//if (uri.getRawFragment() != null)
if (uri.OriginalString.IndexOf('#') >= 0)
{
relName = PackagingUriHelper.CreatePartName(uri.AbsolutePath);
string path = string.Empty;
try
{
path = uri.AbsolutePath;
}
catch (InvalidOperationException)
{
path = uri.OriginalString.Substring(0, uri.OriginalString.IndexOf('#'));
}
relName = PackagingUriHelper.CreatePartName(path);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions ooxml/XSSF/UserModel/XSSFDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public XSSFTextBox CreateTextbox(IClientAnchor anchor)
long shapeId = newShapeId();
CT_TwoCellAnchor ctAnchor = CreateTwoCellAnchor(anchor);
CT_Shape ctShape = ctAnchor.AddNewSp();
ctShape.Set(XSSFSimpleShape.GetPrototype());
ctShape.Set(XSSFSimpleShape.Prototype());
ctShape.nvSpPr.cNvPr.id=(uint)shapeId;
XSSFTextBox shape = new XSSFTextBox(this, ctShape);
shape.anchor = (XSSFClientAnchor)anchor;
Expand Down Expand Up @@ -217,7 +217,7 @@ public XSSFSimpleShape CreateSimpleShape(XSSFClientAnchor anchor)
long shapeId = newShapeId();
CT_TwoCellAnchor ctAnchor = CreateTwoCellAnchor(anchor);
CT_Shape ctShape = ctAnchor.AddNewSp();
ctShape.Set(XSSFSimpleShape.GetPrototype());
ctShape.Set(XSSFSimpleShape.Prototype());
ctShape.nvSpPr.cNvPr.id=(uint)(shapeId);
XSSFSimpleShape shape = new XSSFSimpleShape(this, ctShape);
shape.anchor = anchor;
Expand Down
4 changes: 2 additions & 2 deletions ooxml/XSSF/UserModel/XSSFShapeGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ internal static CT_GroupShape Prototype()
public XSSFTextBox CreateTextbox(XSSFChildAnchor anchor)
{
CT_Shape ctShape = ctGroup.AddNewSp();
ctShape.Set(XSSFSimpleShape.GetPrototype());
ctShape.Set(XSSFSimpleShape.Prototype());

XSSFTextBox shape = new XSSFTextBox(GetDrawing(), ctShape);
shape.parent = this;
Expand All @@ -111,7 +111,7 @@ public XSSFTextBox CreateTextbox(XSSFChildAnchor anchor)
public XSSFSimpleShape CreateSimpleShape(XSSFChildAnchor anchor)
{
CT_Shape ctShape = ctGroup.AddNewSp();
ctShape.Set(XSSFSimpleShape.GetPrototype());
ctShape.Set(XSSFSimpleShape.Prototype());

XSSFSimpleShape shape = new XSSFSimpleShape(GetDrawing(), ctShape);
shape.parent = (this);
Expand Down
9 changes: 7 additions & 2 deletions ooxml/XSSF/UserModel/XSSFSimpleShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,14 @@ protected internal XSSFSimpleShape(XSSFDrawing Drawing, CT_Shape ctShape)
/**
* Prototype with the default structure of a new auto-shape.
*/
protected internal static CT_Shape GetPrototype()
protected internal static CT_Shape Prototype()
{
if (prototype == null)
// in poi, method XmlObject set(XmlObject srcObj) will create a copy of XmlObject
// so this prototype object would be newly set to shape object
// but in .net, the prototype object will be modified and keep its contents, would
// affect next usage. so comment the following code, and create a new prototype object
// for every calling of Prototype().
//if (prototype == null)
{
CT_Shape shape = new CT_Shape();

Expand Down
6 changes: 5 additions & 1 deletion openxml4Net/OPC/PackagingUriHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,11 @@ public static Uri ResolvePartUri(Uri sourcePartUri, Uri targetUri)
path = Path.GetDirectoryName(sourcePartUri.OriginalString).Replace("\\", "/");

string targetPath = targetUri.OriginalString;
if (targetPath.StartsWith("../"))
if (targetPath.StartsWith("#"))
{
path += "/" + Path.GetFileName(sourcePartUri.OriginalString) + targetPath;
}
else if (targetPath.StartsWith("../"))
{
string[] segments = path.Split(new char[] { '/' });

Expand Down
2 changes: 1 addition & 1 deletion testcases/main/HSSF/UserModel/TestHSSFDataFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ public void TestGetFormattedCellValueHSSFCell()
{
ICell cell = (ICell)it.Current;
String fmtval = formatter.FormatCellValue(cell);
log(fmtval);

// should not be equal to "555.555"
Assert.IsTrue(DateUtil.IsCellDateFormatted(cell));
Expand All @@ -261,6 +260,7 @@ public void TestGetFormattedCellValueHSSFCell()
{
jul = jul.Substring(0, 1);
}
log(fmt+"\t\t\t"+fmtval + "\t\t\t" + jul);
// check we found july properly
Assert.IsTrue(fmtval.IndexOf(jul) > -1, "Format came out incorrect - " + fmt);
}
Expand Down
Loading