24#include "kspread_cell.h"
25#include "kspread_sheet.h"
26#include "kspread_util.h"
27#include "kspread_value.h"
31#include "valueconverter.h"
50void RegisterReferenceFunctions()
52 FunctionRepository* repo = FunctionRepository::self();
55 f =
new Function (
"ADDRESS", func_address);
56 f->setParamCount (2, 5);
58 f =
new Function (
"AREAS", func_areas);
60 f->setNeedsExtra (
true);
62 f =
new Function (
"CHOOSE", func_choose);
63 f->setParamCount (2, -1);
66 f =
new Function (
"COLUMN", func_column);
67 f->setParamCount (0, 1);
69 f =
new Function (
"COLUMNS", func_columns);
72 f->setNeedsExtra (
true);
74 f =
new Function (
"HLOOKUP", func_hlookup);
75 f->setParamCount (3, 4);
78 f =
new Function (
"INDEX", func_index);
82 f =
new Function (
"INDIRECT", func_indirect);
83 f->setParamCount (1, 2);
84 f->setNeedsExtra (
true);
86 f =
new Function (
"LOOKUP", func_lookup);
90 f =
new Function (
"ROW", func_row);
91 f->setParamCount (0, 1);
93 f =
new Function (
"ROWS", func_rows);
96 f->setNeedsExtra (
true);
98 f =
new Function (
"VLOOKUP", func_vlookup);
99 f->setParamCount (3, 4);
100 f->setAcceptArray ();
110 if (args.count() > 2)
111 absNum = calc->conv()->asInteger (args[2]).
asInteger();
112 if (args.count() > 3)
113 r1c1 = !(calc->conv()->asBoolean (args[3]).
asBoolean());
114 if (args.count() == 5)
115 sheetName = calc->conv()->asString (args[4]).
asString();
118 int row = calc->conv()->asInteger (args[0]).
asInteger();
119 int col = calc->conv()->asInteger (args[1]).
asInteger();
121 if ( !sheetName.isEmpty() )
131 if ( absNum == 1 || absNum == 2 )
137 result += TQString::number( row );
144 if ( absNum == 1 || absNum == 3 )
150 result += TQString::number( col );
158 if ( absNum == 1 || absNum == 3 )
167 if ( absNum == 1 || absNum == 2 )
173 result += TQString::number( row );
176 return Value (result);
179bool checkRef( TQString
const & ref )
195 if ((e->ranges[0].col1 != -1) && (e->ranges[0].row1 != -1) &&
196 (e->ranges[0].col2 != -1) && (e->ranges[0].row2 != -1))
201 TQString s = calc->conv()->asString (args[0]).
asString();
202 if ( s[0] !=
'(' || s[s.length() - 1] !=
')' )
209 for (
int i = 1; i < l; ++i )
211 if ( s[i] ==
',' || s[i] ==
')' )
213 if ( !checkRef( ref ) )
231 int cnt = args.count () - 1;
232 int num = calc->conv()->asInteger (args[0]).
asInteger();
233 if ((num <= 0) || (num > cnt))
241 const Value key = args[0];
242 const Value data = args[1];
243 const int row = calc->conv()->asInteger( args[2] ).
asInteger();
244 const int cols = data.
columns();
245 const int rows = data.
rows();
246 if ( row < 1 || row > rows )
248 const bool rangeLookup = ( args.count() > 3 ) ? calc->conv()->asBoolean( args[3] ).
asBoolean() :
true;
253 for (
int col = 0; col < cols; ++col) {
257 return data.
element(col, row - 1);
262 v = data.
element(col, row - 1);
277 unsigned row = calc->conv()->asInteger (args[1]).
asInteger() - 1;
278 unsigned col = calc->conv()->asInteger (args[2]).
asInteger() - 1;
290 Value lookup = args[1];
292 unsigned cols = lookup.
columns();
293 unsigned rows = lookup.
rows();
299 for (
unsigned r = 0; r < rows; ++r)
300 for (
unsigned c = 0; c < cols; ++c)
304 if (calc->
lower (le, num) || calc->
equal (num, le))
315 int col = e ? e->mycol : 0;
316 if (e && args.count())
317 col = e->ranges[0].col1;
326 int row = e ? e->myrow : 0;
327 if (e && args.count())
328 row = e->ranges[0].row1;
337 int col1 = e->ranges[0].col1;
338 int col2 = e->ranges[0].col2;
339 if ((col1 == -1) || (col2 == -1))
341 return Value (col2 - col1 + 1);
347 int row1 = e->ranges[0].row1;
348 int row2 = e->ranges[0].row2;
349 if ((row1 == -1) || (row2 == -1))
351 return Value (row2 - row1 + 1);
356Value func_indirect (valVector args,
ValueCalc *calc, FuncExtra *e)
359 TQString ref = calc->conv()->asString (args[0]).
asString();
360 if (args.count() == 2)
361 r1c1 = !(calc->conv()->asBoolean (args[1]).
asBoolean());
372 Point p (ref, e->sheet->workbook(), e->sheet);
377 Cell * cell = p.cell();
379 return cell->
value();
386 const Value key = args[0];
387 const Value data = args[1];
388 const int col = calc->conv()->asInteger(args[2]).
asInteger();
389 const int cols = data.
columns();
390 const int rows = data.
rows();
391 if (col < 1 || col > cols)
393 const bool rangeLookup = (args.count() > 3) ? calc->conv()->asBoolean(args[3]).
asBoolean() :
true;
398 for (
int row = 0; row < rows; ++row) {
402 return data.
element(col - 1, row);
407 v = data.
element(col - 1, row);
For every cell in the spread sheet there is a Cell object.
const Value value() const
Returns the value that this cell holds.
TQString columnName() const
Returns the column name of the cell.
Represents the position of a single cell in a workbook.
Represents a region within a workbook.
The ValueCalc class is used to perform all sorts of calculations.
bool equal(const Value &a, const Value &b)
numerical comparison
bool naturalLower(const Value &a, const Value &b, bool CS=true)
string or numerical comparison
bool naturalEqual(const Value &a, const Value &b, bool CS=true)
string or numerical comparison
bool lower(const Value &a, const Value &b)
numerical comparison
Value asNumeric(const Value &value) const
convert to a numeric value - not necessarily float
Provides a wrapper for cell value.
unsigned columns() const
If this value is an array, return the number of columns.
Value element(unsigned column, unsigned row) const
Return an element in the array value.
unsigned rows() const
If this value is an array, return the number of rows.
bool isArray() const
Returns true if the type of this value is array.
static const Value & errorREF()
Returns constant reference to #REF! error.
long asInteger() const
Returns the integer value of this value.
TQString asString() const
Returns the string value of this value.
static const Value & errorVALUE()
Returns constant reference to #VALUE! error.
bool asBoolean() const
Returns the boolean value of this value.
static const Value & errorNA()
Returns constant reference to #N/A error.