]> Creatis software - CreaPhase.git/blob - octave_packages/io-1.0.19/spsh_chkrange.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / io-1.0.19 / spsh_chkrange.m
1 ## Copyright (C) 2010,2011,2012 Philip Nienhuis <prnienhuis@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 ## -*- texinfo -*-
17 ## @deftypefn {Function File} [ @var{topleftaddr}, @var{nrows}, @var{ncols}, @var{toprow}, @var{leftcol} ] = spsh_chkrange ( @var{range}, @var{rowsize}, @var{colsize}, @var{intf-type}, @var{filename})
18 ## (Internal function) Get and check various cell and range address parameters for spreadsheet input.
19 ##
20 ## spsh_chkrange should not be invoked directly but rather through oct2xls or oct2ods.
21 ##
22 ## Example:
23 ##
24 ## @example
25 ##   [tl, nrw, ncl, trw, lcl] = spsh_chkrange (crange, nr, nc, xtype, fileptr);
26 ## @end example
27 ##
28 ## @end deftypefn
29
30 ## Author: Philip Nienhuis <prnienhuis@users.sf.net>
31 ## Created: 2010-08-02
32 ## Updates: 
33 ## 2010-08-25 Option for supplying file pointer rather than interface_type & filename
34 ##            (but this can be wasteful if the file ptr is copied)
35 ## 2011-03-29 Bug fix - unrecognized pointer struct & wrong type error msg
36 ## 2011-05-15 Experimental UNO support added (OpenOffice.org & clones)
37 ## 2011-09-18 Updated UNO data row capacity for LibreOffice 3.4+ (now 1,048,576 rows)
38
39 function [ topleft, nrows, ncols, trow, lcol ] = spsh_chkrange (crange, nr, nc, intf, filename=[])
40
41         if (nargin == 4)
42                 # File pointer input assumed
43                 if (isstruct (intf))
44                         xtype = intf.xtype;
45                         filename = intf.filename;
46                 else
47                         error ("Too few or improper arguments supplied.");
48                 endif
49         else
50                 # Interface type & filename supplied
51                 xtype = intf;
52         endif
53
54         # Define max row & column capacity from interface type & file suffix
55         switch xtype
56                 case { 'COM', 'POI' }
57                         if (strmatch (tolower (filename(end-3:end)), '.xls'))
58                                 # BIFF5 & BIFF8
59                                 ROW_CAP = 65536;   COL_CAP = 256;
60                         else
61                                 # OOXML (COM needs Excel 2007+ for this)
62                                 ROW_CAP = 1048576; COL_CAP = 16384;
63                         endif
64                 case { 'JXL', 'OXS' }
65                         # JExcelAPI & OpenXLS can only process BIFF5 & BIFF8
66                         ROW_CAP = 65536;   COL_CAP = 256;
67                 case { 'OTK', 'JOD' }
68                         # ODS
69                         ROW_CAP = 65536;   COL_CAP = 1024;
70                 case { 'UNO' }
71                         # ODS; LibreOffice has a higher row capacity
72                         # FIXME - use UNO calls to check physical row capacity
73       # FIXME - LibreOffice has higher row capacity but its Java classes haven't been updated
74                         ROW_CAP = 1048576;   COL_CAP = 1024;
75                 otherwise
76                         error (sprintf ("Unknown interface type - %s\n", xtype));
77         endswitch
78
79         if (isempty (deblank (crange)))
80                 trow = 1;
81                 lcol = 1;
82                 nrows = nr;
83                 ncols = nc;
84                 topleft = 'A1';
85         elseif (isempty (strfind (deblank (crange), ':')))
86                 # Only top left cell specified
87                 [topleft, dummy1, dummy2, trow, lcol] = parse_sp_range (crange);
88                 nrows = nr;
89                 ncols = nc;
90         else
91                 [topleft, nrows, ncols, trow, lcol] = parse_sp_range (crange);
92         endif
93         if (trow > ROW_CAP || lcol > COL_CAP)
94                 error ("Topleft cell (%s) beyond spreadsheet limits.");
95         endif
96         # Check spreadsheet capacity beyond requested topleft cell
97         nrows = min (nrows, ROW_CAP - trow + 1);
98         ncols = min (ncols, COL_CAP - lcol + 1);
99         # Check array size and requested range
100         nrows = min (nrows, nr);
101         ncols = min (ncols, nc);
102
103 endfunction