]> Creatis software - CreaPhase.git/blob - octave_packages/io-1.0.19/parse_sp_range.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / io-1.0.19 / parse_sp_range.m
1 ## Copyright (C) 2009-2011 Philip Nienhuis <pr.nienhuis at users.sf.net>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
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.
19
20 ## Author: Philip Nienhuis
21 ## Created: 2009-06-20
22 ## Latest update 2010-01-13
23
24 function [topleft, nrows, ncols, toprow, lcol] = parse_sp_range (range_org)
25
26         range = deblank (toupper (range_org));
27         range_error = 0; nrows = 0; ncols = 0;
28
29         # Basic checks
30         if (index (range, ':') == 0) 
31                 if (isempty (range))
32                         range_error = 0;
33                         leftcol = 'A';
34                         rightcol = 'A';
35                 else
36                         # Only upperleft cell given, just complete range to 1x1
37                         # (needed for some routines)
38                         range = [range ":" range];
39                 endif
40         endif
41
42         # Split up both sides of the range
43         [topleft, lowerright] = strtok (range, ':');
44
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+');
50         if (isempty (en1)) 
51                 en1=0; 
52         endif
53         [st, en2] = regexp (leftcol,'\D+');
54         leftcol = leftcol(en1+1:en2);
55
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+');
61         if (isempty (en1)) 
62                 en1=0; 
63         endif
64         [st, en2] = regexp (rightcol,'\D+');
65         rightcol = rightcol(en1+1:en2);
66
67         # Check nr. of rows
68         nrows = bottomrow - toprow + 1; 
69         if (nrows < 1) 
70                 range_error = 1; 
71         endif
72
73         if (range_error == 0) 
74                 # Get left column nr.
75                 [st, en] = regexp (leftcol, '\D+');
76                 lcol = (leftcol(st:st) - 'A' + 1);
77                 while (++st <= en)
78                         lcol = lcol * 26 + (leftcol(st:st) - 'A' + 1);
79                 endwhile
80
81                 # Get right column nr.
82                 [st, en] = regexp (rightcol, '\D+');
83                 rcol = (rightcol(st:st) - 'A' + 1);
84                 while (++st <= en)
85                         rcol = rcol * 26 + (rightcol(st:st) - 'A' + 1);
86                 endwhile
87
88                 # Check
89                 ncols = rcol - lcol + 1;
90                 if (ncols < 1) 
91                         range_error = 1; 
92                 endif
93         endif
94
95         if (range_error > 0) 
96                 ncols = 0; nrows = 0;
97                 error ("Spreadsheet range error! ");
98         endif
99   
100 endfunction
101
102 %!test
103 %! [a b c d e] = parse_sp_range ('A1:B2');
104 %! assert ([a b c d e], ['A1', 2, 2, 1, 1]);
105
106 %!test
107 %! [a b c d e] = parse_sp_range ('A1:AB200');
108 %! assert ([a b c d e], ['A1', 200, 28, 1, 1]);
109
110 %!test
111 %! [a b c d e] = parse_sp_range ('cd230:iY65536');
112 %! assert ([a b c d e], ['CD230', 65307, 178, 230, 82]);
113
114 %!test
115 %! [a b c d e] = parse_sp_range ('BvV12798 : xFd1054786');
116 %! assert ([b c d e], [1041989, 14439, 12798, 1946]);
117