File: spsh_chkrange.m

package info (click to toggle)
octave-io 2.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,544 kB
  • ctags: 461
  • sloc: objc: 2,082; cpp: 528; ansic: 172; makefile: 15
file content (105 lines) | stat: -rw-r--r-- 3,746 bytes parent folder | download
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
## Copyright (C) 2010,2011,2012,2013 Philip Nienhuis
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program 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 General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @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})
## (Internal function) Get and check various cell and range address parameters for spreadsheet input.
##
## spsh_chkrange should not be invoked directly but rather through oct2xls or oct2ods.
##
## Example:
##
## @example
##   [tl, nrw, ncl, trw, lcl] = spsh_chkrange (crange, nr, nc, xtype, fileptr);
## @end example
##
## @end deftypefn

## Author: Philip Nienhuis <prnienhuis@users.sf.net>
## Created: 2010-08-02
## Updates: 
## 2010-08-25 Option for supplying file pointer rather than interface_type & filename
##            (but this can be wasteful if the file ptr is copied)
## 2011-03-29 Bug fix - unrecognized pointer struct & wrong type error msg
## 2011-05-15 Experimental UNO support added (OpenOffice.org & clones)
## 2011-09-18 Updated UNO data row capacity for LibreOffice 3.4+ (now 1,048,576 rows)
## 2012-10-23 Style fixes
## 2013-12-06 Updated copyright strings

function [ topleft, nrows, ncols, trow, lcol ] = spsh_chkrange (crange, nr, nc, intf, filename=[])

	if (nargin == 4)
		## File pointer input assumed
		if (isstruct (intf))
			xtype = intf.xtype;
			filename = intf.filename;
		else
			error ("Too few or improper arguments supplied.");
		endif
	else
		## Interface type & filename supplied
		xtype = intf;
	endif

	## Define max row & column capacity from interface type & file suffix
	switch xtype
		case { 'COM', 'POI' }
			if (strmatch (tolower (filename(end-3:end)), '.xls'))
				## BIFF5 & BIFF8
				ROW_CAP = 65536;   COL_CAP = 256;
			else
				## OOXML (COM needs Excel 2007+ for this)
				ROW_CAP = 1048576; COL_CAP = 16384;
			endif
		case { 'JXL', 'OXS' }
			## JExcelAPI & OpenXLS can only process BIFF5 & BIFF8
			ROW_CAP = 65536;   COL_CAP = 256;
		case { 'OTK', 'JOD' }
			## ODS
			ROW_CAP = 65536;   COL_CAP = 1024;
		case { 'UNO' }
			## ODS; LibreOffice has a higher row capacity
			## FIXME - use UNO calls to check physical row capacity
      ## FIXME - LibreOffice has higher row capacity but its Java classes haven't been updated
			ROW_CAP = 1048576;   COL_CAP = 1024;
		otherwise
			error (sprintf ("Unknown interface type - %s\n", xtype));
	endswitch

	if (isempty (deblank (crange)))
		trow = 1;
		lcol = 1;
		nrows = nr;
		ncols = nc;
		topleft = 'A1';
	elseif (isempty (strfind (deblank (crange), ':')))
		## Only top left cell specified
		[topleft, dummy1, dummy2, trow, lcol] = parse_sp_range (crange);
		nrows = nr;
		ncols = nc;
	else
		[topleft, nrows, ncols, trow, lcol] = parse_sp_range (crange);
	endif
	if (trow > ROW_CAP || lcol > COL_CAP)
		error ("Topleft cell (%s) beyond spreadsheet limits.");
	endif
	## Check spreadsheet capacity beyond requested topleft cell
	nrows = min (nrows, ROW_CAP - trow + 1);
	ncols = min (ncols, COL_CAP - lcol + 1);
	## Check array size and requested range
	nrows = min (nrows, nr);
	ncols = min (ncols, nc);

endfunction