Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/FormulaParsing/ExcelUtilities/AddressTranslator.cs

Last change on this file was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 5.0 KB
Line 
1/*******************************************************************************
2 * You may amend and distribute as you like, but don't remove this header!
3 *
4 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
5 * See http://www.codeplex.com/EPPlus for details.
6 *
7 * Copyright (C) 2011  Jan Källman
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
17 * See the GNU Lesser General Public License for more details.
18 *
19 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
20 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
21 *
22 * All code and executables are provided "as is" with no warranty either express or implied.
23 * The author accepts no liability for any damage or loss of business that this product may cause.
24 *
25 * Code change notes:
26 *
27 * Author             Change            Date
28 * ******************************************************************************
29 * Mats Alm                       Added                   2013-03-01 (Prior file history on https://github.com/swmal/ExcelFormulaParser)
30 *******************************************************************************/
31using System;
32using System.Collections.Generic;
33using System.Globalization;
34using System.Linq;
35using System.Text;
36using System.Text.RegularExpressions;
37using OfficeOpenXml.FormulaParsing;
38using OfficeOpenXml.FormulaParsing.Utilities;
39
40namespace OfficeOpenXml.FormulaParsing.ExcelUtilities
41{
42    /// <summary>
43    /// Handles translations from Spreadsheet addresses to 0-based numeric index.
44    /// </summary>
45    public class AddressTranslator
46    {
47        public enum RangeCalculationBehaviour
48        {
49            FirstPart,
50            LastPart
51        }
52
53        private readonly ExcelDataProvider _excelDataProvider;
54
55        public AddressTranslator(ExcelDataProvider excelDataProvider)
56        {
57            Require.That(excelDataProvider).Named("excelDataProvider").IsNotNull();
58            _excelDataProvider = excelDataProvider;
59        }
60
61        /// <summary>
62        /// Translates an address in format "A1" to col- and rowindex.
63        ///
64        /// If the supplied address is a range, the address of the first part will be calculated.
65        /// </summary>
66        /// <param name="address"></param>
67        /// <param name="col"></param>
68        /// <param name="row"></param>
69        public virtual void ToColAndRow(string address, out int col, out int row)
70        {
71            ToColAndRow(address, out col, out row, RangeCalculationBehaviour.FirstPart);
72        }
73
74        /// <summary>
75        /// Translates an address in format "A1" to col- and rowindex.
76        /// </summary>
77        /// <param name="address"></param>
78        /// <param name="col"></param>
79        /// <param name="row"></param>
80        /// <param name="behaviour"></param>
81        public virtual void ToColAndRow(string address, out int col, out int row, RangeCalculationBehaviour behaviour)
82        {
83            address = address.ToUpper(CultureInfo.InvariantCulture);
84            var alphaPart = GetAlphaPart(address);
85            col = 0;
86            var nLettersInAlphabet = 26;
87            for (int x = 0; x < alphaPart.Length; x++)
88            {
89                var pos = alphaPart.Length - x - 1;
90                var currentNumericValue = GetNumericAlphaValue(alphaPart[x]);
91                col += (nLettersInAlphabet * pos * currentNumericValue);
92                if (pos == 0)
93                {
94                    col += currentNumericValue;
95                }
96            }
97            //col--;
98            //row = GetIntPart(address) - 1 ?? GetRowIndexByBehaviour(behaviour);
99            row = GetIntPart(address) ?? GetRowIndexByBehaviour(behaviour);
100
101        }
102
103        private int GetRowIndexByBehaviour(RangeCalculationBehaviour behaviour)
104        {
105            if (behaviour == RangeCalculationBehaviour.FirstPart)
106            {
107                return 1;
108            }
109            return _excelDataProvider.ExcelMaxRows;
110        }
111
112        private int GetNumericAlphaValue(char c)
113        {
114            return (int)c - 64;
115        }
116
117        private string GetAlphaPart(string address)
118        {
119            return Regex.Match(address, "[A-Z]+").Value;
120        }
121
122        private int? GetIntPart(string address)
123        {
124            if (Regex.IsMatch(address, "[0-9]+"))
125            {
126                return int.Parse(Regex.Match(address, "[0-9]+").Value);
127            }
128            return null;
129        }
130    }
131}
Note: See TracBrowser for help on using the repository browser.