File indexing completed on 2025-01-19 10:49:14

0001 /*
0002  *  SPDX-FileCopyrightText: 2010 Sebastian Sauer <sebsauer@kdab.com>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #ifndef XLSUTILS_H
0008 #define XLSUTILS_H
0009 
0010 #include <QDebug>
0011 #include <math.h>
0012 
0013 // translate the range-character to a number
0014 inline int rangeCharToInt(char c)
0015 {
0016     return (c >= 'A' && c <= 'Z') ? (c - 'A' + 1) : -1;
0017 }
0018 
0019 // translates the range-string into a number
0020 inline int rangeStringToInt(const QString &string)
0021 {
0022     int result = 0;
0023     const int size = string.size();
0024     for ( int i = 0; i < size; i++ )
0025         result += rangeCharToInt( string[i].toLatin1() ) * pow( 10.0, ( size - i - 1 ) );
0026     return result;
0027 }
0028 
0029 // splits a given cellrange like Sheet1.D2:Sheet1.F2, Sheet1.D2:F2, D2:F2 or D2 into its parts
0030 inline QPair<QString,QRect> splitCellRange(QString range)
0031 {
0032     range.remove( '$' ); // remove "fixed" character
0033     // remove []
0034     if(range.startsWith(QLatin1Char('[')) && range.endsWith(QLatin1Char(']'))) {
0035         range.remove(0, 1).chop(1);
0036     }
0037     QPair<QString,QRect> result;
0038     const bool isPoint = !range.contains( ':' );
0039     QRegExp regEx = isPoint ? QRegExp( "(.*)(\\.|\\!)([A-Z]+)([0-9]+)" ) : QRegExp ( "(.*)(\\.|\\!)([A-Z]+)([0-9]+)\\:(|.*\\.)([A-Z]+)([0-9]+)" );
0040     if ( regEx.indexIn( range ) >= 0 ) {
0041         const QString sheetName = regEx.cap( 1 );
0042         QPoint topLeft( rangeStringToInt( regEx.cap(3) ), regEx.cap(4).toInt() );
0043         if ( isPoint ) {
0044             result = QPair<QString,QRect>(sheetName, QRect(topLeft,QSize(1,1)));
0045         } else {
0046             QPoint bottomRight( rangeStringToInt( regEx.cap(6) ), regEx.cap(7).toInt() );
0047             result = QPair<QString,QRect>(sheetName, QRect(topLeft,bottomRight));
0048         }
0049     }
0050     return result;
0051 }
0052 
0053 #endif