diff --git a/ClosedXML/ClosedXML/ClosedXML/ExcelHelper.cs b/ClosedXML/ClosedXML/ClosedXML/ExcelHelper.cs index 8338961..cb4a863 100644 --- a/ClosedXML/ClosedXML/ClosedXML/ExcelHelper.cs +++ b/ClosedXML/ClosedXML/ClosedXML/ExcelHelper.cs @@ -84,12 +84,33 @@ public static bool IsValidColumn(string column) { - if (StringExtensions.IsNullOrWhiteSpace(column) || column.Length > 3) + Int32 length = column.Length; + if (StringExtensions.IsNullOrWhiteSpace(column) || length > 3) return false; String theColumn = column.ToUpper(); - return - !column.Where((t, i) => theColumn[i] < 'A' || theColumn[i] > 'Z' || (i == 2 && theColumn[i] > 'D')).Any(); + + + Boolean isValid = theColumn[0] >= 'A' && theColumn[0] <= 'Z'; + if (length == 1) + return isValid; + + if (length == 2) + return isValid && theColumn[1] >= 'A' && theColumn[1] <= 'Z'; + + if (theColumn[0] < 'X') + return theColumn[1] >= 'A' && theColumn[1] <= 'Z' + && theColumn[2] >= 'A' && theColumn[2] <= 'Z'; + + if (theColumn[0] != 'X') return false; + + if (theColumn[1] < 'F') + return theColumn[2] >= 'A' && theColumn[2] <= 'Z'; + + if (theColumn[1] != 'F') return false; + + return theColumn[2] >= 'A' && theColumn[2] <= 'D'; + } public static bool IsValidRow(string rowString) diff --git a/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj b/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj index 8fe2609..9eb6264 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj +++ b/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj @@ -71,6 +71,7 @@ + diff --git a/ClosedXML/ClosedXML/ClosedXML_Tests/ExcelHelperTest.cs b/ClosedXML/ClosedXML/ClosedXML_Tests/ExcelHelperTest.cs new file mode 100644 index 0000000..e49320f --- /dev/null +++ b/ClosedXML/ClosedXML/ClosedXML_Tests/ExcelHelperTest.cs @@ -0,0 +1,89 @@ +using ClosedXML.Excel; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +namespace ClosedXML_Tests +{ + + + /// + ///This is a test class for ExcelHelperTest and is intended + ///to contain all ExcelHelperTest Unit Tests + /// + [TestClass()] + public class ExcelHelperTest + { + + + private TestContext testContextInstance; + + /// + ///Gets or sets the test context which provides + ///information about and functionality for the current test run. + /// + public TestContext TestContext + { + get + { + return testContextInstance; + } + set + { + testContextInstance = value; + } + } + + #region Additional test attributes + // + //You can use the following additional attributes as you write your tests: + // + //Use ClassInitialize to run code before running the first test in the class + //[ClassInitialize()] + //public static void MyClassInitialize(TestContext testContext) + //{ + //} + // + //Use ClassCleanup to run code after all tests in a class have run + //[ClassCleanup()] + //public static void MyClassCleanup() + //{ + //} + // + //Use TestInitialize to run code before running each test + //[TestInitialize()] + //public void MyTestInitialize() + //{ + //} + // + //Use TestCleanup to run code after each test has run + //[TestCleanup()] + //public void MyTestCleanup() + //{ + //} + // + #endregion + + + /// + ///A test for IsValidColumn + /// + [TestMethod()] + public void IsValidColumnTest() + { + Assert.AreEqual(false, ExcelHelper.IsValidColumn("")); + Assert.AreEqual(false, ExcelHelper.IsValidColumn("1")); + Assert.AreEqual(false, ExcelHelper.IsValidColumn("A1")); + Assert.AreEqual(false, ExcelHelper.IsValidColumn("AA1")); + Assert.AreEqual(true, ExcelHelper.IsValidColumn("A")); + Assert.AreEqual(true, ExcelHelper.IsValidColumn("AA")); + Assert.AreEqual(true, ExcelHelper.IsValidColumn("AAA")); + Assert.AreEqual(true, ExcelHelper.IsValidColumn("Z")); + Assert.AreEqual(true, ExcelHelper.IsValidColumn("ZZ")); + Assert.AreEqual(true, ExcelHelper.IsValidColumn("XFD")); + Assert.AreEqual(false, ExcelHelper.IsValidColumn("ZAA")); + Assert.AreEqual(false, ExcelHelper.IsValidColumn("XZA")); + Assert.AreEqual(false, ExcelHelper.IsValidColumn("XFZ")); + } + } +} +