File indexing completed on 2024-12-08 12:55:59

0001 /*
0002  *  Copyright (c) 2010 Sebastian Sauer <sebsauer@kdab.com>
0003  *
0004  *  This library is free software; you can redistribute it and/or modify
0005  *  it under the terms of the GNU Lesser General Public License as published
0006  *  by the Free Software Foundation; either version 2.1 of the License, or
0007  *  (at your option) any later version.
0008  *
0009  *  This library is distributed in the hope that it will be useful,
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *  GNU Lesser General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU Lesser General Public License
0015  *  along with this program; if not, write to the Free Software
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017  */
0018 
0019 #ifndef XLSUTILS_H
0020 #define XLSUTILS_H
0021 
0022 #include <QDebug>
0023 #include <math.h>
0024 
0025 // translate the range-character to a number
0026 int rangeCharToInt(char c)
0027 {
0028     return (c >= 'A' && c <= 'Z') ? (c - 'A' + 1) : -1;
0029 }
0030 
0031 // translates the range-string into a number
0032 int rangeStringToInt(const QString &string)
0033 {
0034     int result = 0;
0035     const int size = string.size();
0036     for ( int i = 0; i < size; i++ )
0037         result += rangeCharToInt( string[i].toLatin1() ) * pow( 10.0, ( size - i - 1 ) );
0038     return result;
0039 }
0040 
0041 // splits a given cellrange like Sheet1.D2:Sheet1.F2, Sheet1.D2:F2, D2:F2 or D2 into its parts
0042 QPair<QString,QRect> splitCellRange(QString range)
0043 {
0044     range.remove( '$' ); // remove "fixed" character
0045     // remove []
0046     if(range.startsWith(QLatin1Char('[')) && range.endsWith(QLatin1Char(']'))) {
0047         range.remove(0, 1).chop(1);
0048     }
0049     QPair<QString,QRect> result;
0050     const bool isPoint = !range.contains( ':' );
0051     QRegExp regEx = isPoint ? QRegExp( "(.*)(\\.|\\!)([A-Z]+)([0-9]+)" ) : QRegExp ( "(.*)(\\.|\\!)([A-Z]+)([0-9]+)\\:(|.*\\.)([A-Z]+)([0-9]+)" );
0052     if ( regEx.indexIn( range ) >= 0 ) {
0053         const QString sheetName = regEx.cap( 1 );
0054         QPoint topLeft( rangeStringToInt( regEx.cap(3) ), regEx.cap(4).toInt() );
0055         if ( isPoint ) {
0056             result = QPair<QString,QRect>(sheetName, QRect(topLeft,QSize(1,1)));
0057         } else {
0058             QPoint bottomRight( rangeStringToInt( regEx.cap(6) ), regEx.cap(7).toInt() );
0059             result = QPair<QString,QRect>(sheetName, QRect(topLeft,bottomRight));
0060         }
0061     }
0062     return result;
0063 }
0064 
0065 #endif