1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
/* Swinder - Portable library for spreadsheet
Copyright (C) 2003 Ariya Hidayat <ariya@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/
#include "cell.h"
#include "ustring.h"
#include "format.h"
#include "value.h"
#include <iostream>
namespace Swinder
{
class CellPrivate
{
public:
Sheet* sheet;
unsigned column;
unsigned row;
UString formula;
Value value;
Format* format;
int formatIndex;
unsigned columnSpan;
unsigned rowSpan;
static UString columnNames[256];
CellPrivate(Sheet* s, unsigned column, unsigned row);
~CellPrivate() { delete format; }
};
}
using namespace Swinder;
UString CellPrivate::columnNames[256];
CellPrivate::CellPrivate(Sheet* s, unsigned c, unsigned r):
sheet(s), column(c), row(r), format(0), formatIndex(-1), columnSpan(0), rowSpan(0)
{
}
Cell::Cell( Sheet* sheet, unsigned column, unsigned row )
{
d = new CellPrivate(sheet, column, row);
}
Cell::~Cell()
{
delete d;
}
Sheet* Cell::sheet()
{
return d->sheet;
}
unsigned Cell::column() const
{
return d->column;
}
unsigned Cell::row() const
{
return d->row;
}
UString Cell::name() const
{
return name( column(), row() );
}
UString Cell::name( unsigned column, unsigned row )
{
// column=0, row=0 is "A1"
return columnLabel( column ) + UString::number( row + 1 );
}
UString Cell::columnLabel() const
{
return columnLabel( column() );
}
UString Cell::columnLabel( unsigned column )
{
UString label;
// Excel has only up to 256 columns, so we cache those
if(column < 256 )
{
label = CellPrivate::columnNames[column];
// cache is not ready, so construct it first
if(label.isEmpty())
{
for(unsigned c = 0; c < 26; c++)
CellPrivate::columnNames[c] = UString(UChar((char)'A'+c));
for(unsigned d = 0; d < 256-26; d++)
{
char buf[3] = { (char)('A'+(d / 26)), (char)('A'+(d % 26)), 0};
CellPrivate::columnNames[d+26] = UString(buf);
}
label = CellPrivate::columnNames[column];
}
}
else
{
// otherwise, find with slower method
// how many characters for the column name?
unsigned digits = 1;
unsigned offset = 0;
for( unsigned limit = 26; column-offset >= limit; limit *= 26, digits++ )
offset += limit;
// extreme case e.g. column 4294967295 is "AATYHWUR" (8 characters)
if(digits < 9)
{
char ref[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// from the last character
char* colp = ref + 9 - 1;
for( unsigned c = column - offset; digits; --digits, c/=26, colp-- )
*colp = char('A' + (c%26));
label = UString((const char*)(colp+1));
}
}
return label;
}
const Value& Cell::value() const
{
return d->value;
}
void Cell::setValue( const Value& value )
{
d->value = value;
}
const UString& Cell::formula() const
{
return d->formula;
}
void Cell::setFormula( const UString& formula )
{
d->formula = formula;
}
int Cell::formatIndex() const
{
return d->formatIndex;
}
void Cell::setFormatIndex( int index )
{
d->formatIndex = index;
}
Format Cell::format() const
{
if(!d->format)
d->format = new Format();
return Format(*d->format);
}
void Cell::setFormat( const Format& format )
{
if(!d->format)
d->format = new Format();
(*d->format) = format;
}
unsigned Cell::columnSpan() const
{
return d->columnSpan;
}
void Cell::setColumnSpan( unsigned span )
{
if( span < 1 ) return;
d->columnSpan = span;
}
unsigned Cell::rowSpan() const
{
return d->rowSpan;
}
void Cell::setRowSpan( unsigned span )
{
if( span < 1 ) return;
d->rowSpan = span;
}
|