diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs index 8d4d96d..1426566 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs @@ -115,15 +115,6 @@ Boolean TryGetValue(out T value); - //Boolean TryGetDouble(out Double value); - - //Boolean TryGetBoolean(out Boolean value); - - //Boolean TryGetDateTime(out DateTime value); - - //Boolean TryGetTimeSpan(out TimeSpan value); - - Boolean TryGetHyperlink(out XLHyperlink value); Boolean HasHyperlink { get; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs index 9b20f00..c3c4521 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs @@ -236,7 +236,7 @@ _cellValue = dtTest.ToOADate().ToString(); } else if ( - value is sbyte + value is sbyte || value is byte || value is short || value is ushort @@ -268,44 +268,11 @@ public T GetValue() { - var val = Value; + T retVal; + if(TryGetValue(out retVal)) + return retVal; - if (val is TimeSpan) - { - if (typeof (T) == typeof (String)) - return (T)Convert.ChangeType(val.ToString(), typeof (T)); - - return (T)val; - } - - if (val is IXLRichText) - return (T)RichText; - - if (typeof (T) == typeof (String)) - { - var valToUse = val.ToString(); - if (!utfPattern.Match(valToUse).Success) - return (T)Convert.ChangeType(val, typeof (T)); - - var sb = new StringBuilder(); - var lastIndex = 0; - foreach (Match match in utfPattern.Matches(valToUse)) - { - var matchString = match.Value; - var matchIndex = match.Index; - sb.Append(valToUse.Substring(lastIndex, matchIndex - lastIndex)); - - sb.Append((char)int.Parse(match.Groups[1].Value, NumberStyles.AllowHexSpecifier)); - - lastIndex = matchIndex + matchString.Length; - } - if (lastIndex < valToUse.Length) - sb.Append(valToUse.Substring(lastIndex)); - - return (T)Convert.ChangeType(sb.ToString(), typeof (T)); - } - - return (T)Convert.ChangeType(val, typeof (T)); + throw new Exception("Cannot convert cell value to " + typeof(T)); } public string GetString() @@ -1201,71 +1168,160 @@ { var currValue = Value; - if (typeof(T) == typeof(TimeSpan)) + if (currValue == null) + { + value = default(T); + return true; + } + + bool b; + if (TryGetTimeSpanValue(out value, currValue, out b)) return b; + + if (TryGetRichStringValue(out value)) return true; + + if (TryGetStringValue(out value, currValue)) return true; + + var strValue = currValue.ToString(); + if (typeof(T) == typeof(bool)) return TryGetBasicValue(out value, strValue, bool.TryParse); + if (typeof(T) == typeof(sbyte)) return TryGetBasicValue(out value, strValue, sbyte.TryParse); + if (typeof(T) == typeof(byte)) return TryGetBasicValue(out value, strValue, byte.TryParse); + if (typeof(T) == typeof(short)) return TryGetBasicValue(out value, strValue, short.TryParse); + if (typeof(T) == typeof(ushort)) return TryGetBasicValue(out value, strValue, ushort.TryParse); + if (typeof(T) == typeof(int)) return TryGetBasicValue(out value, strValue, int.TryParse); + if (typeof(T) == typeof(uint)) return TryGetBasicValue(out value, strValue, uint.TryParse); + if (typeof(T) == typeof(long)) return TryGetBasicValue(out value, strValue, long.TryParse); + if (typeof(T) == typeof(ulong)) return TryGetBasicValue(out value, strValue, ulong.TryParse); + if (typeof(T) == typeof(float)) return TryGetBasicValue(out value, strValue, float.TryParse); + if (typeof(T) == typeof(double)) return TryGetBasicValue(out value, strValue, double.TryParse); + if (typeof(T) == typeof(decimal)) return TryGetBasicValue(out value, strValue, decimal.TryParse); + + if (typeof(T) == typeof(XLHyperlink)) + { + XLHyperlink tmp = GetHyperlink(); + if (tmp != null) + { + value = (T)Convert.ChangeType(tmp, typeof(T)); + return true; + } + + value = default(T); + return false; + } + + try + { + value = (T)Convert.ChangeType(currValue, typeof(T)); + return true; + } + catch + { + value = default(T); + return false; + } + } + + private static bool TryGetTimeSpanValue(out T value, object currValue, out bool b) + { + if (typeof (T) == typeof (TimeSpan)) { TimeSpan tmp; Boolean retVal = true; if (currValue is TimeSpan) { - tmp = (TimeSpan)currValue; + tmp = (TimeSpan) currValue; } else if (!TimeSpan.TryParse(currValue.ToString(), out tmp)) { retVal = false; } - value = (T)Convert.ChangeType(tmp, typeof(T)); - return retVal; + value = (T) Convert.ChangeType(tmp, typeof (T)); + { + b = retVal; + return true; + } } + value = default(T); + b = false; + return false; + } - - if (typeof(T) == typeof(IXLRichText)) + private bool TryGetRichStringValue(out T value) + { + if (typeof (T) == typeof (IXLRichText)) { value = (T) RichText; return true; } - - //if (typeof(T) == typeof(String)) - //{ - // var valToUse = currValue.ToString(); - // if (!utfPattern.Match(valToUse).Success) - // return (T)Convert.ChangeType(currValue, typeof(T)); - - // var sb = new StringBuilder(); - // var lastIndex = 0; - // foreach (Match match in utfPattern.Matches(valToUse)) - // { - // var matchString = match.Value; - // var matchIndex = match.Index; - // sb.Append(valToUse.Substring(lastIndex, matchIndex - lastIndex)); - - // sb.Append((char)int.Parse(match.Groups[1].Value, NumberStyles.AllowHexSpecifier)); - - // lastIndex = matchIndex + matchString.Length; - // } - // if (lastIndex < valToUse.Length) - // sb.Append(valToUse.Substring(lastIndex)); - - // return (T)Convert.ChangeType(sb.ToString(), typeof(T)); - //} - - value = (T)Convert.ChangeType(currValue, typeof(T)); - return true; + value = default(T); + return false; } - //public Boolean TryGetDouble(out Double value); - - //public Boolean TryGetBoolean(out Boolean value); - - //public Boolean TryGetDateTime(out DateTime value); - - //public Boolean TryGetTimeSpan(out TimeSpan value); - - public Boolean TryGetHyperlink(out XLHyperlink value) + private static bool TryGetStringValue(out T value, object currValue) { - value = GetHyperlink(); - return value != null; + if (typeof (T) == typeof (String)) + { + var valToUse = currValue.ToString(); + if (!utfPattern.Match(valToUse).Success) + { + value = (T) Convert.ChangeType(valToUse, typeof (T)); + return true; + } + + var sb = new StringBuilder(); + var lastIndex = 0; + foreach (Match match in utfPattern.Matches(valToUse)) + { + var matchString = match.Value; + var matchIndex = match.Index; + sb.Append(valToUse.Substring(lastIndex, matchIndex - lastIndex)); + + sb.Append((char) int.Parse(match.Groups[1].Value, NumberStyles.AllowHexSpecifier)); + + lastIndex = matchIndex + matchString.Length; + } + if (lastIndex < valToUse.Length) + sb.Append(valToUse.Substring(lastIndex)); + + value = (T) Convert.ChangeType(sb.ToString(), typeof (T)); + return true; + } + value = default(T); + return false; + } + + private static Boolean TryGetBooleanValue(out T value, object currValue) + { + if (typeof (T) == typeof (Boolean)) + { + Boolean tmp; + if (Boolean.TryParse(currValue.ToString(), out tmp)) + { + value = (T) Convert.ChangeType(tmp, typeof (T)); + { + return true; + } + } + } + value = default(T); + return false; + } + + delegate Boolean Func(String input, out T output); + + private static Boolean TryGetBasicValue(out T value, String currValue, Func func ) + { + U tmp; + if (func(currValue, out tmp)) + { + value = (T)Convert.ChangeType(tmp, typeof(T)); + { + return true; + } + } + value = default(T); + return false; } #endregion diff --git a/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/Cells/XLCellTest.cs b/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/Cells/XLCellTest.cs index 0cf7043..ec056f6 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/Cells/XLCellTest.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Tests/Excel/Cells/XLCellTest.cs @@ -169,5 +169,90 @@ Assert.AreEqual(cell.RichText, outValue); Assert.AreEqual("Anything", outValue.ToString()); } + + [TestMethod] + public void TryGetValue_Boolean_True() + { + var ws = new XLWorkbook().Worksheets.Add("Sheet1"); + Boolean outValue; + var cell = ws.Cell("A1").SetValue(true); + var success = cell.TryGetValue(out outValue); + Assert.IsTrue(success); + Assert.IsTrue(outValue); + } + + [TestMethod] + public void TryGetValue_Boolean_False() + { + var ws = new XLWorkbook().Worksheets.Add("Sheet1"); + Boolean outValue; + var cell = ws.Cell("A1").SetValue(false); + var success = cell.TryGetValue(out outValue); + Assert.IsTrue(success); + Assert.IsFalse(outValue); + } + + [TestMethod] + public void TryGetValue_Boolean_Bad() + { + var ws = new XLWorkbook().Worksheets.Add("Sheet1"); + Boolean outValue; + var cell = ws.Cell("A1").SetValue("ABC"); + var success = cell.TryGetValue(out outValue); + Assert.IsFalse(success); + } + + [TestMethod] + public void TryGetValue_Boolean_Good() + { + var ws = new XLWorkbook().Worksheets.Add("Sheet1"); + Boolean outValue; + var cell = ws.Cell("A1").SetValue("True"); + var success = cell.TryGetValue(out outValue); + Assert.IsTrue(success); + Assert.IsTrue(outValue); + } + + [TestMethod] + public void TryGetValue_sbyte_Good() + { + var ws = new XLWorkbook().Worksheets.Add("Sheet1"); + sbyte outValue; + var cell = ws.Cell("A1").SetValue(5); + var success = cell.TryGetValue(out outValue); + Assert.IsTrue(success); + Assert.AreEqual((sbyte)5, outValue); + } + + [TestMethod] + public void TryGetValue_sbyte_Bad() + { + var ws = new XLWorkbook().Worksheets.Add("Sheet1"); + sbyte outValue; + var cell = ws.Cell("A1").SetValue(255); + var success = cell.TryGetValue(out outValue); + Assert.IsFalse(success); + } + + [TestMethod] + public void TryGetValue_sbyte_Good2() + { + var ws = new XLWorkbook().Worksheets.Add("Sheet1"); + sbyte outValue; + var cell = ws.Cell("A1").SetValue("5"); + var success = cell.TryGetValue(out outValue); + Assert.IsTrue(success); + Assert.AreEqual((sbyte)5, outValue); + } + + [TestMethod] + public void TryGetValue_sbyte_Bad2() + { + var ws = new XLWorkbook().Worksheets.Add("Sheet1"); + sbyte outValue; + var cell = ws.Cell("A1").SetValue("255"); + var success = cell.TryGetValue(out outValue); + Assert.IsFalse(success); + } } } \ No newline at end of file