Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/FormulaParsing/Excel/Functions/RefAndLookup/ExcelLookupNavigator.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: 4.3 KB
Line 
1/* Copyright (C) 2011  Jan Källman
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
11 * See the GNU Lesser General Public License for more details.
12 *
13 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
14 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
15 *
16 * All code and executables are provided "as is" with no warranty either express or implied.
17 * The author accepts no liability for any damage or loss of business that this product may cause.
18 *
19 * Code change notes:
20 *
21 * Author             Change            Date
22 *******************************************************************************
23 * Mats Alm                       Added                   2013-12-03
24 *******************************************************************************/
25using System;
26using System.Collections.Generic;
27using System.Linq;
28using System.Text;
29using OfficeOpenXml.FormulaParsing.Utilities;
30using OfficeOpenXml.FormulaParsing.ExcelUtilities;
31
32namespace OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup
33{
34    public class ExcelLookupNavigator : LookupNavigator
35    {
36        private int _currentRow;
37        private int _currentCol;
38        private object _currentValue;
39        private RangeAddress _rangeAddress;
40        private int _index;
41
42        public ExcelLookupNavigator(LookupDirection direction, LookupArguments arguments, ParsingContext parsingContext)
43            : base(direction, arguments, parsingContext)
44        {
45            Initialize();
46        }
47
48        private void Initialize()
49        {
50            _index = 0;
51            var factory = new RangeAddressFactory(ParsingContext.ExcelDataProvider);
52            if (Arguments.RangeInfo == null)
53            {
54                _rangeAddress = factory.Create(ParsingContext.Scopes.Current.Address.Worksheet, Arguments.RangeAddress);
55            }
56            else
57            {
58                _rangeAddress = factory.Create(Arguments.RangeInfo.Address.WorkSheet, Arguments.RangeInfo.Address.Address);
59            }
60            _currentCol = _rangeAddress.FromCol;
61            _currentRow = _rangeAddress.FromRow;
62            SetCurrentValue();
63        }
64
65        private void SetCurrentValue()
66        {
67            _currentValue = ParsingContext.ExcelDataProvider.GetCellValue(_rangeAddress.Worksheet, _currentRow, _currentCol);
68        }
69
70        private bool HasNext()
71        {
72            if (Direction == LookupDirection.Vertical)
73            {
74                return _currentRow < _rangeAddress.ToRow;
75            }
76            else
77            {
78                return _currentCol < _rangeAddress.ToCol;
79            }
80        }
81
82        public override int Index
83        {
84            get { return _index; }
85        }
86
87        public override bool MoveNext()
88        {
89            if (!HasNext()) return false;
90            if (Direction == LookupDirection.Vertical)
91            {
92                _currentRow++;
93            }
94            else
95            {
96                _currentCol++;
97            }
98            _index++;
99            SetCurrentValue();
100            return true;
101        }
102
103        public override object CurrentValue
104        {
105            get { return _currentValue; }
106        }
107
108        public override object GetLookupValue()
109        {
110            var row = _currentRow;
111            var col = _currentCol;
112            if (Direction == LookupDirection.Vertical)
113            {
114                col += Arguments.LookupIndex - 1;
115                row += Arguments.LookupOffset;
116            }
117            else
118            {
119                row += Arguments.LookupIndex - 1;
120                col += Arguments.LookupOffset;
121            }
122            return ParsingContext.ExcelDataProvider.GetCellValue(_rangeAddress.Worksheet, row, col);
123        }
124    }
125}
Note: See TracBrowser for help on using the repository browser.