1 ## Copyright (C) 2009-2011 Philip Nienhuis <pr.nienhuis at users.sf.net>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
16 ## Parse a string representing a range of cells for a spreadsheet
17 ## into nr of rows and nr of columns and also extract top left
18 ## cell address + top row + left column. Some error checks are implemented.
20 ## Author: Philip Nienhuis
21 ## Created: 2009-06-20
22 ## Latest update 2010-01-13
24 function [topleft, nrows, ncols, toprow, lcol] = parse_sp_range (range_org)
26 range = deblank (toupper (range_org));
27 range_error = 0; nrows = 0; ncols = 0;
30 if (index (range, ':') == 0)
36 # Only upperleft cell given, just complete range to 1x1
37 # (needed for some routines)
38 range = [range ":" range];
42 # Split up both sides of the range
43 [topleft, lowerright] = strtok (range, ':');
45 # Get toprow and clean up left column
46 [st, en] = regexp (topleft, '\d+');
47 toprow = str2num (topleft(st:en));
48 leftcol = deblank (topleft(1:st-1));
49 [st, en1] = regexp( leftcol,'\s+');
53 [st, en2] = regexp (leftcol,'\D+');
54 leftcol = leftcol(en1+1:en2);
56 # Get bottom row and clean up right column
57 [st, en] = regexp (lowerright,'\d+');
58 bottomrow = str2num (lowerright(st:en));
59 rightcol = deblank (lowerright(2:st-1));
60 [st, en1] = regexp (rightcol,'\s+');
64 [st, en2] = regexp (rightcol,'\D+');
65 rightcol = rightcol(en1+1:en2);
68 nrows = bottomrow - toprow + 1;
75 [st, en] = regexp (leftcol, '\D+');
76 lcol = (leftcol(st:st) - 'A' + 1);
78 lcol = lcol * 26 + (leftcol(st:st) - 'A' + 1);
81 # Get right column nr.
82 [st, en] = regexp (rightcol, '\D+');
83 rcol = (rightcol(st:st) - 'A' + 1);
85 rcol = rcol * 26 + (rightcol(st:st) - 'A' + 1);
89 ncols = rcol - lcol + 1;
97 error ("Spreadsheet range error! ");
103 %! [a b c d e] = parse_sp_range ('A1:B2');
104 %! assert ([a b c d e], ['A1', 2, 2, 1, 1]);
107 %! [a b c d e] = parse_sp_range ('A1:AB200');
108 %! assert ([a b c d e], ['A1', 200, 28, 1, 1]);
111 %! [a b c d e] = parse_sp_range ('cd230:iY65536');
112 %! assert ([a b c d e], ['CD230', 65307, 178, 230, 82]);
115 %! [a b c d e] = parse_sp_range ('BvV12798 : xFd1054786');
116 %! assert ([b c d e], [1041989, 14439, 12798, 1946]);