Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/FormulaParsing/ExcelUtilities/RangeAddressFactory.cs @ 12074

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

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 6.8 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.Linq;
34using System.Text;
35using OfficeOpenXml.FormulaParsing;
36using OfficeOpenXml.FormulaParsing.Utilities;
37
38namespace OfficeOpenXml.FormulaParsing.ExcelUtilities
39{
40    public class RangeAddressFactory
41    {
42        private readonly ExcelDataProvider _excelDataProvider;
43        private readonly AddressTranslator _addressTranslator;
44        private readonly IndexToAddressTranslator _indexToAddressTranslator;
45
46        public RangeAddressFactory(ExcelDataProvider excelDataProvider)
47            : this(excelDataProvider, new AddressTranslator(excelDataProvider), new IndexToAddressTranslator(excelDataProvider, ExcelReferenceType.RelativeRowAndColumn))
48        {
49           
50           
51        }
52
53        public RangeAddressFactory(ExcelDataProvider excelDataProvider, AddressTranslator addressTranslator, IndexToAddressTranslator indexToAddressTranslator)
54        {
55            Require.That(excelDataProvider).Named("excelDataProvider").IsNotNull();
56            Require.That(addressTranslator).Named("addressTranslator").IsNotNull();
57            Require.That(indexToAddressTranslator).Named("indexToAddressTranslator").IsNotNull();
58            _excelDataProvider = excelDataProvider;
59            _addressTranslator = addressTranslator;
60            _indexToAddressTranslator = indexToAddressTranslator;
61        }
62
63        public RangeAddress Create(int col, int row)
64        {
65            return Create(string.Empty, col, row);
66        }
67
68        public RangeAddress Create(string worksheetName, int col, int row)
69        {
70            return new RangeAddress()
71            {
72                Address = _indexToAddressTranslator.ToAddress(col, row),
73                Worksheet = worksheetName,
74                FromCol = col,
75                ToCol = col,
76                FromRow = row,
77                ToRow = row
78            };
79        }
80
81        /// <summary>
82        ///
83        /// </summary>
84        /// <param name="worksheetName">will be used if no worksheet name is specified in <paramref name="address"/></param>
85        /// <param name="address">address of a range</param>
86        /// <returns></returns>
87        public RangeAddress Create(string worksheetName, string address)
88        {
89            Require.That(address).Named("range").IsNotNullOrEmpty();
90            //var addressInfo = ExcelAddressInfo.Parse(address);
91            var adr = new ExcelAddressBase(address); 
92            var sheet = string.IsNullOrEmpty(adr.WorkSheet) ? worksheetName : adr.WorkSheet;
93            var dim = _excelDataProvider.GetDimensionEnd(adr.WorkSheet);
94            var rangeAddress = new RangeAddress()
95            {
96                Address = adr.Address,
97                Worksheet = sheet,
98                FromRow = adr._fromRow,
99                FromCol = adr._fromCol,
100                ToRow = (dim != null && adr._toRow > dim.Row) ? dim.Row : adr._toRow,
101                ToCol = adr._toCol
102            };
103
104            //if (addressInfo.IsMultipleCells)
105            //{
106            //    HandleMultipleCellAddress(rangeAddress, addressInfo);
107            //}
108            //else
109            //{
110            //    HandleSingleCellAddress(rangeAddress, addressInfo);
111            //}
112            return rangeAddress;
113        }
114
115        public RangeAddress Create(string range)
116        {
117            Require.That(range).Named("range").IsNotNullOrEmpty();
118            //var addressInfo = ExcelAddressInfo.Parse(range);
119            var adr = new ExcelAddressBase(range);
120            var rangeAddress = new RangeAddress()
121            {
122                Address = adr.Address,
123                Worksheet = adr.WorkSheet ?? "",
124                FromRow = adr._fromRow,
125                FromCol = adr._fromCol,
126                ToRow = adr._toRow,
127                ToCol = adr._toCol
128            };
129           
130            //if (addressInfo.IsMultipleCells)
131            //{
132            //    HandleMultipleCellAddress(rangeAddress, addressInfo);
133            //}
134            //else
135            //{
136            //    HandleSingleCellAddress(rangeAddress, addressInfo);
137            //}
138            return rangeAddress;
139        }
140
141        private void HandleSingleCellAddress(RangeAddress rangeAddress, ExcelAddressInfo addressInfo)
142        {
143            int col, row;
144            _addressTranslator.ToColAndRow(addressInfo.StartCell, out col, out row);
145            rangeAddress.FromCol = col;
146            rangeAddress.ToCol = col;
147            rangeAddress.FromRow = row;
148            rangeAddress.ToRow = row;
149        }
150
151        private void HandleMultipleCellAddress(RangeAddress rangeAddress, ExcelAddressInfo addressInfo)
152        {
153            int fromCol, fromRow;
154            _addressTranslator.ToColAndRow(addressInfo.StartCell, out fromCol, out fromRow);
155            int toCol, toRow;
156            _addressTranslator.ToColAndRow(addressInfo.EndCell, out toCol, out toRow, AddressTranslator.RangeCalculationBehaviour.LastPart);
157            rangeAddress.FromCol = fromCol;
158            rangeAddress.ToCol = toCol;
159            rangeAddress.FromRow = fromRow;
160            rangeAddress.ToRow = toRow;
161        }
162    }
163}
Note: See TracBrowser for help on using the repository browser.