From: <prn...@us...> - 2012-06-07 16:36:29
|
Revision: 10585 http://octave.svn.sourceforge.net/octave/?rev=10585&view=rev Author: prnienhuis Date: 2012-06-07 16:36:19 +0000 (Thu, 07 Jun 2012) Log Message: ----------- Tabs replaced by double space Modified Paths: -------------- trunk/octave-forge/main/io/inst/xls2oct.m Modified: trunk/octave-forge/main/io/inst/xls2oct.m =================================================================== --- trunk/octave-forge/main/io/inst/xls2oct.m 2012-06-07 16:35:46 UTC (rev 10584) +++ trunk/octave-forge/main/io/inst/xls2oct.m 2012-06-07 16:36:19 UTC (rev 10585) @@ -133,84 +133,85 @@ ## 2012-02-25 Fixed missing quotes in struct check L.149-153 ## 2012-02-26 Updated texinfo header help text ## 2012-06-06 Implemented "formulas_as_text" option for COM +## 2012-06-07 Replaced all tabs by double space ## ## Latest subfunc update: 2012-06-06 function [ rawarr, xls, rstatus ] = xls2oct (xls, wsh=1, datrange='', spsh_opts=[]) - # Check if xls struct pointer seems valid - if (~isstruct (xls)), error ("File ptr struct expected for arg @ 1"); endif - test1 = ~isfield (xls, "xtype"); - test1 = test1 || ~isfield (xls, "workbook"); - test1 = test1 || isempty (xls.workbook); - test1 = test1 || isempty (xls.app); - test1 = test1 || ~ischar (xls.xtype); - if test1 - error ("Invalid xls file pointer struct"); - endif + # Check if xls struct pointer seems valid + if (~isstruct (xls)), error ("File ptr struct expected for arg @ 1"); endif + test1 = ~isfield (xls, "xtype"); + test1 = test1 || ~isfield (xls, "workbook"); + test1 = test1 || isempty (xls.workbook); + test1 = test1 || isempty (xls.app); + test1 = test1 || ~ischar (xls.xtype); + if test1 + error ("Invalid xls file pointer struct"); + endif - # Check worksheet ptr - if (~(ischar (wsh) || isnumeric (wsh))), error ("Integer (index) or text (wsh name) expected for arg # 2"); endif - # Check range - if (~(isempty (datrange) || ischar (datrange))), error ("Character string (range) expected for arg # 3"); endif + # Check worksheet ptr + if (~(ischar (wsh) || isnumeric (wsh))), error ("Integer (index) or text (wsh name) expected for arg # 2"); endif + # Check range + if (~(isempty (datrange) || ischar (datrange))), error ("Character string (range) expected for arg # 3"); endif - # Check & setup options struct - if (nargin < 4 || isempty (spsh_opts)) - spsh_opts.formulas_as_text = 0; - spsh_opts.strip_array = 1; - # Future options: - elseif (isstruct (spsh_opts)) - if (~isfield (spsh_opts', 'formulas_as_text')), spsh_opts.formulas_as_text = 0; endif - if (~isfield (spsh_opts', 'strip_array')), spsh_opts.strip_array = 1; endif - % Future options: - else - error ("Structure expected for arg # 4"); - endif + # Check & setup options struct + if (nargin < 4 || isempty (spsh_opts)) + spsh_opts.formulas_as_text = 0; + spsh_opts.strip_array = 1; + # Future options: + elseif (isstruct (spsh_opts)) + if (~isfield (spsh_opts', 'formulas_as_text')), spsh_opts.formulas_as_text = 0; endif + if (~isfield (spsh_opts', 'strip_array')), spsh_opts.strip_array = 1; endif + % Future options: + else + error ("Structure expected for arg # 4"); + endif - # Select the proper interfaces - if (strcmp (xls.xtype, 'COM')) - # Call Excel tru COM / ActiveX server - [rawarr, xls, rstatus] = xls2com2oct (xls, wsh, datrange, spsh_opts); - elseif (strcmp (xls.xtype, 'POI')) - # Read xls file tru Java POI - [rawarr, xls, rstatus] = xls2jpoi2oct (xls, wsh, datrange, spsh_opts); - elseif (strcmp (xls.xtype, 'JXL')) - # Read xls file tru JExcelAPI - [rawarr, xls, rstatus] = xls2jxla2oct (xls, wsh, datrange, spsh_opts); - elseif (strcmp (xls.xtype, 'OXS')) - # Read xls file tru OpenXLS - [rawarr, xls, rstatus] = xls2oxs2oct (xls, wsh, datrange, spsh_opts); - elseif (strcmp (xls.xtype, 'UNO')) - # Read xls file tru OpenOffice.org UNO (Java) bridge - [rawarr, xls, rstatus] = xls2uno2oct (xls, wsh, datrange, spsh_opts); -# elseif ---- <Other interfaces here> - # Call to next interface - else - error (sprintf ("xls2oct: unknown Excel .xls interface - %s.", xls.xtype)); - endif + # Select the proper interfaces + if (strcmp (xls.xtype, 'COM')) + # Call Excel tru COM / ActiveX server + [rawarr, xls, rstatus] = xls2com2oct (xls, wsh, datrange, spsh_opts); + elseif (strcmp (xls.xtype, 'POI')) + # Read xls file tru Java POI + [rawarr, xls, rstatus] = xls2jpoi2oct (xls, wsh, datrange, spsh_opts); + elseif (strcmp (xls.xtype, 'JXL')) + # Read xls file tru JExcelAPI + [rawarr, xls, rstatus] = xls2jxla2oct (xls, wsh, datrange, spsh_opts); + elseif (strcmp (xls.xtype, 'OXS')) + # Read xls file tru OpenXLS + [rawarr, xls, rstatus] = xls2oxs2oct (xls, wsh, datrange, spsh_opts); + elseif (strcmp (xls.xtype, 'UNO')) + # Read xls file tru OpenOffice.org UNO (Java) bridge + [rawarr, xls, rstatus] = xls2uno2oct (xls, wsh, datrange, spsh_opts); +# elseif ---- <Other interfaces here> + # Call to next interface + else + error (sprintf ("xls2oct: unknown Excel .xls interface - %s.", xls.xtype)); + endif - # Optionally strip empty outer rows and columns & keep track of original data location - if (spsh_opts.strip_array) - emptr = cellfun ('isempty', rawarr); - if (all (all (emptr))) - rawarr = {}; - xls.limits = []; - else - nrows = size (rawarr, 1); ncols = size (rawarr, 2); - irowt = 1; - while (all (emptr(irowt, :))), irowt++; endwhile - irowb = nrows; - while (all (emptr(irowb, :))), irowb--; endwhile - icoll = 1; - while (all (emptr(:, icoll))), icoll++; endwhile - icolr = ncols; - while (all (emptr(:, icolr))), icolr--; endwhile + # Optionally strip empty outer rows and columns & keep track of original data location + if (spsh_opts.strip_array) + emptr = cellfun ('isempty', rawarr); + if (all (all (emptr))) + rawarr = {}; + xls.limits = []; + else + nrows = size (rawarr, 1); ncols = size (rawarr, 2); + irowt = 1; + while (all (emptr(irowt, :))), irowt++; endwhile + irowb = nrows; + while (all (emptr(irowb, :))), irowb--; endwhile + icoll = 1; + while (all (emptr(:, icoll))), icoll++; endwhile + icolr = ncols; + while (all (emptr(:, icolr))), icolr--; endwhile - # Crop output cell array and update limits - rawarr = rawarr(irowt:irowb, icoll:icolr); - xls.limits = xls.limits + [icoll-1, icolr-ncols; irowt-1, irowb-nrows]; - endif - endif + # Crop output cell array and update limits + rawarr = rawarr(irowt:irowb, icoll:icolr); + xls.limits = xls.limits + [icoll-1, icolr-ncols; irowt-1, irowb-nrows]; + endif + endif endfunction @@ -270,98 +271,98 @@ function [rawarr, xls, rstatus ] = xls2com2oct (xls, wsh, crange, spsh_opts) - rstatus = 0; rawarr = {}; + rstatus = 0; rawarr = {}; - # Basic checks - if (nargin < 2) error ("xls2com2oct needs a minimum of 2 arguments."); endif - if (size (wsh, 2) > 31) - warning ("Worksheet name too long - truncated") - wsh = wsh(1:31); - endif - app = xls.app; - wb = xls.workbook; - # Check to see if ActiveX is still alive - try - wb_cnt = wb.Worksheets.count; - catch - error ("ActiveX invocation in file ptr struct seems non-functional"); - end_try_catch + # Basic checks + if (nargin < 2) error ("xls2com2oct needs a minimum of 2 arguments."); endif + if (size (wsh, 2) > 31) + warning ("Worksheet name too long - truncated") + wsh = wsh(1:31); + endif + app = xls.app; + wb = xls.workbook; + # Check to see if ActiveX is still alive + try + wb_cnt = wb.Worksheets.count; + catch + error ("ActiveX invocation in file ptr struct seems non-functional"); + end_try_catch - # Check & get handle to requested worksheet - wb_cnt = wb.Worksheets.count; - old_sh = 0; - if (isnumeric (wsh)) - if (wsh < 1 || wsh > wb_cnt) - errstr = sprintf ("Worksheet number: %d out of range 1-%d", wsh, wb_cnt); - error (errstr) - rstatus = 1; - return - else - old_sh = wsh; - endif - else - # Find worksheet number corresponding to name in wsh - wb_cnt = wb.Worksheets.count; - for ii =1:wb_cnt - sh_name = wb.Worksheets(ii).name; - if (strcmp (sh_name, wsh)) old_sh = ii; endif - endfor - if (~old_sh) - errstr = sprintf ("Worksheet name \"%s\" not present", wsh); - error (errstr) - else - wsh = old_sh; - endif - endif - sh = wb.Worksheets (wsh); + # Check & get handle to requested worksheet + wb_cnt = wb.Worksheets.count; + old_sh = 0; + if (isnumeric (wsh)) + if (wsh < 1 || wsh > wb_cnt) + errstr = sprintf ("Worksheet number: %d out of range 1-%d", wsh, wb_cnt); + error (errstr) + rstatus = 1; + return + else + old_sh = wsh; + endif + else + # Find worksheet number corresponding to name in wsh + wb_cnt = wb.Worksheets.count; + for ii =1:wb_cnt + sh_name = wb.Worksheets(ii).name; + if (strcmp (sh_name, wsh)) old_sh = ii; endif + endfor + if (~old_sh) + errstr = sprintf ("Worksheet name \"%s\" not present", wsh); + error (errstr) + else + wsh = old_sh; + endif + endif + sh = wb.Worksheets (wsh); - nrows = 0; - if ((nargin == 2) || (isempty (crange))) - allcells = sh.UsedRange; - # Get actually used range indices - [trow, brow, lcol, rcol] = getusedrange (xls, old_sh); - if (trow == 0 && brow == 0) - # Empty sheet - rawarr = {}; - printf ("Worksheet '%s' contains no data\n", sh.Name); - return; - else - nrows = brow - trow + 1; ncols = rcol - lcol + 1; - topleft = calccelladdress (trow, lcol); - lowerright = calccelladdress (brow, rcol); - crange = [topleft ':' lowerright]; - endif - else - # Extract top_left_cell from range - [topleft, nrows, ncols, trow, lcol] = parse_sp_range (crange); - brow = trow + nrows - 1; - rcol = lcol + ncols - 1; - endif; + nrows = 0; + if ((nargin == 2) || (isempty (crange))) + allcells = sh.UsedRange; + # Get actually used range indices + [trow, brow, lcol, rcol] = getusedrange (xls, old_sh); + if (trow == 0 && brow == 0) + # Empty sheet + rawarr = {}; + printf ("Worksheet '%s' contains no data\n", sh.Name); + return; + else + nrows = brow - trow + 1; ncols = rcol - lcol + 1; + topleft = calccelladdress (trow, lcol); + lowerright = calccelladdress (brow, rcol); + crange = [topleft ':' lowerright]; + endif + else + # Extract top_left_cell from range + [topleft, nrows, ncols, trow, lcol] = parse_sp_range (crange); + brow = trow + nrows - 1; + rcol = lcol + ncols - 1; + endif; - if (nrows >= 1) - # Get object from Excel sheet, starting at cell top_left_cell - rr = sh.Range (crange); + if (nrows >= 1) + # Get object from Excel sheet, starting at cell top_left_cell + rr = sh.Range (crange); if (spsh_opts.formulas_as_text) rawarr = rr.Formula; else rawarr = rr.Value; endif - delete (rr); + delete (rr); - # Take care of actual singe cell range - if (isnumeric (rawarr) || ischar (rawarr)) - rawarr = {rawarr}; - endif + # Take care of actual singe cell range + if (isnumeric (rawarr) || ischar (rawarr)) + rawarr = {rawarr}; + endif - # If we get here, all seems to have gone OK - rstatus = 1; - # Keep track of data rectangle limits - xls.limits = [lcol, rcol; trow, brow]; - else - error ("No data read from Excel file"); - rstatus = 0; - endif - + # If we get here, all seems to have gone OK + rstatus = 1; + # Keep track of data rectangle limits + xls.limits = [lcol, rcol; trow, brow]; + else + error ("No data read from Excel file"); + rstatus = 0; + endif + endfunction @@ -420,127 +421,127 @@ function [ rawarr, xls, rstatus ] = xls2jpoi2oct (xls, wsh, cellrange, spsh_opts) - persistent ctype; - if (isempty (ctype)) - # Get enumerated cell types. Beware as they start at 0 not 1 - ctype(1) = java_get ('org.apache.poi.ss.usermodel.Cell', 'CELL_TYPE_NUMERIC'); - ctype(2) = java_get ('org.apache.poi.ss.usermodel.Cell', 'CELL_TYPE_STRING'); - ctype(3) = java_get ('org.apache.poi.ss.usermodel.Cell', 'CELL_TYPE_FORMULA'); - ctype(4) = java_get ('org.apache.poi.ss.usermodel.Cell', 'CELL_TYPE_BLANK'); - ctype(5) = java_get ('org.apache.poi.ss.usermodel.Cell', 'CELL_TYPE_BOOLEAN'); - ctype(6) = java_get ('org.apache.poi.ss.usermodel.Cell', 'CELL_TYPE_ERROR'); - endif - - rstatus = 0; jerror = 0; - wb = xls.workbook; + persistent ctype; + if (isempty (ctype)) + # Get enumerated cell types. Beware as they start at 0 not 1 + ctype(1) = java_get ('org.apache.poi.ss.usermodel.Cell', 'CELL_TYPE_NUMERIC'); + ctype(2) = java_get ('org.apache.poi.ss.usermodel.Cell', 'CELL_TYPE_STRING'); + ctype(3) = java_get ('org.apache.poi.ss.usermodel.Cell', 'CELL_TYPE_FORMULA'); + ctype(4) = java_get ('org.apache.poi.ss.usermodel.Cell', 'CELL_TYPE_BLANK'); + ctype(5) = java_get ('org.apache.poi.ss.usermodel.Cell', 'CELL_TYPE_BOOLEAN'); + ctype(6) = java_get ('org.apache.poi.ss.usermodel.Cell', 'CELL_TYPE_ERROR'); + endif + + rstatus = 0; jerror = 0; + wb = xls.workbook; - # Check if requested worksheet exists in the file & if so, get pointer - nr_of_sheets = wb.getNumberOfSheets (); - if (isnumeric (wsh)) - if (wsh > nr_of_sheets), error (sprintf ("Worksheet # %d bigger than nr. of sheets (%d) in file %s", wsh, nr_of_sheets, xls.filename)); endif - sh = wb.getSheetAt (wsh - 1); # POI sheet count 0-based - printf ("(Reading from worksheet %s)\n", sh.getSheetName ()); - else - sh = wb.getSheet (wsh); - if (isempty (sh)), error (sprintf ("Worksheet %s not found in file %s", wsh, xls.filename)); endif - end + # Check if requested worksheet exists in the file & if so, get pointer + nr_of_sheets = wb.getNumberOfSheets (); + if (isnumeric (wsh)) + if (wsh > nr_of_sheets), error (sprintf ("Worksheet # %d bigger than nr. of sheets (%d) in file %s", wsh, nr_of_sheets, xls.filename)); endif + sh = wb.getSheetAt (wsh - 1); # POI sheet count 0-based + printf ("(Reading from worksheet %s)\n", sh.getSheetName ()); + else + sh = wb.getSheet (wsh); + if (isempty (sh)), error (sprintf ("Worksheet %s not found in file %s", wsh, xls.filename)); endif + end - # Check ranges - firstrow = sh.getFirstRowNum (); # 0-based - lastrow = sh.getLastRowNum (); # 0-based - if (isempty (cellrange)) - if (ischar (wsh)) - # get numeric sheet index - ii = wb.getSheetIndex (sh) + 1; - else - ii = wsh; - endif - [ firstrow, lastrow, lcol, rcol ] = getusedrange (xls, ii); - if (firstrow == 0 && lastrow == 0) - # Empty sheet - rawarr = {}; - printf ("Worksheet '%s' contains no data\n", sh.getSheetName ()); - rstatus = 1; - return; - else - nrows = lastrow - firstrow + 1; - ncols = rcol - lcol + 1; - endif - else - # Translate range to HSSF POI row & column numbers - [topleft, nrows, ncols, firstrow, lcol] = parse_sp_range (cellrange); - lastrow = firstrow + nrows - 1; - rcol = lcol + ncols - 1; - endif + # Check ranges + firstrow = sh.getFirstRowNum (); # 0-based + lastrow = sh.getLastRowNum (); # 0-based + if (isempty (cellrange)) + if (ischar (wsh)) + # get numeric sheet index + ii = wb.getSheetIndex (sh) + 1; + else + ii = wsh; + endif + [ firstrow, lastrow, lcol, rcol ] = getusedrange (xls, ii); + if (firstrow == 0 && lastrow == 0) + # Empty sheet + rawarr = {}; + printf ("Worksheet '%s' contains no data\n", sh.getSheetName ()); + rstatus = 1; + return; + else + nrows = lastrow - firstrow + 1; + ncols = rcol - lcol + 1; + endif + else + # Translate range to HSSF POI row & column numbers + [topleft, nrows, ncols, firstrow, lcol] = parse_sp_range (cellrange); + lastrow = firstrow + nrows - 1; + rcol = lcol + ncols - 1; + endif - # Create formula evaluator (needed to infer proper cell type into rawarr) - frm_eval = wb.getCreationHelper().createFormulaEvaluator (); - - # Read contents into rawarr - rawarr = cell (nrows, ncols); # create placeholder - for ii = firstrow:lastrow - irow = sh.getRow (ii-1); - if ~isempty (irow) - scol = (irow.getFirstCellNum).intValue (); - ecol = (irow.getLastCellNum).intValue () - 1; - for jj = lcol:rcol - scell = irow.getCell (jj-1); - if ~isempty (scell) - # Explore cell contents - type_of_cell = scell.getCellType (); - if (type_of_cell == ctype(3)) # Formula - if ~(spsh_opts.formulas_as_text) - try # Because not al Excel formulas have been implemented in POI - cvalue = frm_eval.evaluate (scell); - type_of_cell = cvalue.getCellType(); - # Separate switch because form.eval. yields different type - switch type_of_cell - case ctype (1) # Numeric - rawarr {ii+1-firstrow, jj+1-lcol} = scell.getNumberValue (); - case ctype(2) # String - rawarr {ii+1-firstrow, jj+1-lcol} = char (scell.getStringValue ()); - case ctype (5) # Boolean - rawarr {ii+1-firstrow, jj+1-lcol} = scell.BooleanValue (); - otherwise - # Nothing to do here - endswitch - # Set cell type to blank to skip switch below - type_of_cell = ctype(4); - catch - # In case of formula errors we take the cached results - type_of_cell = scell.getCachedFormulaResultType (); - ++jerror; # We only need one warning even for multiple errors - end_try_catch - endif - endif - # Preparations done, get data values into data array - switch type_of_cell - case ctype(1) # 0 Numeric - rawarr {ii+1-firstrow, jj+1-lcol} = scell.getNumericCellValue (); - case ctype(2) # 1 String - rawarr {ii+1-firstrow, jj+1-lcol} = char (scell.getRichStringCellValue ()); - case ctype(3) - if (spsh_opts.formulas_as_text) - tmp = char (scell.getCellFormula ()); - rawarr {ii+1-firstrow, jj+1-lcol} = ['=' tmp]; - endif - case ctype(4) # 3 Blank - # Blank; ignore until further notice - case ctype(5) # 4 Boolean - rawarr {ii+1-firstrow, jj+1-lcol} = scell.getBooleanCellValue (); - otherwise # 5 Error - # Ignore - endswitch - endif - endfor - endif - endfor + # Create formula evaluator (needed to infer proper cell type into rawarr) + frm_eval = wb.getCreationHelper().createFormulaEvaluator (); + + # Read contents into rawarr + rawarr = cell (nrows, ncols); # create placeholder + for ii = firstrow:lastrow + irow = sh.getRow (ii-1); + if ~isempty (irow) + scol = (irow.getFirstCellNum).intValue (); + ecol = (irow.getLastCellNum).intValue () - 1; + for jj = lcol:rcol + scell = irow.getCell (jj-1); + if ~isempty (scell) + # Explore cell contents + type_of_cell = scell.getCellType (); + if (type_of_cell == ctype(3)) # Formula + if ~(spsh_opts.formulas_as_text) + try # Because not al Excel formulas have been implemented in POI + cvalue = frm_eval.evaluate (scell); + type_of_cell = cvalue.getCellType(); + # Separate switch because form.eval. yields different type + switch type_of_cell + case ctype (1) # Numeric + rawarr {ii+1-firstrow, jj+1-lcol} = scell.getNumberValue (); + case ctype(2) # String + rawarr {ii+1-firstrow, jj+1-lcol} = char (scell.getStringValue ()); + case ctype (5) # Boolean + rawarr {ii+1-firstrow, jj+1-lcol} = scell.BooleanValue (); + otherwise + # Nothing to do here + endswitch + # Set cell type to blank to skip switch below + type_of_cell = ctype(4); + catch + # In case of formula errors we take the cached results + type_of_cell = scell.getCachedFormulaResultType (); + ++jerror; # We only need one warning even for multiple errors + end_try_catch + endif + endif + # Preparations done, get data values into data array + switch type_of_cell + case ctype(1) # 0 Numeric + rawarr {ii+1-firstrow, jj+1-lcol} = scell.getNumericCellValue (); + case ctype(2) # 1 String + rawarr {ii+1-firstrow, jj+1-lcol} = char (scell.getRichStringCellValue ()); + case ctype(3) + if (spsh_opts.formulas_as_text) + tmp = char (scell.getCellFormula ()); + rawarr {ii+1-firstrow, jj+1-lcol} = ['=' tmp]; + endif + case ctype(4) # 3 Blank + # Blank; ignore until further notice + case ctype(5) # 4 Boolean + rawarr {ii+1-firstrow, jj+1-lcol} = scell.getBooleanCellValue (); + otherwise # 5 Error + # Ignore + endswitch + endif + endfor + endif + endfor - if (jerror > 0) warning (sprintf ("xls2oct: %d cached values instead of formula evaluations.\n", jerror)); endif - - rstatus = 1; - xls.limits = [lcol, rcol; firstrow, lastrow]; - + if (jerror > 0) warning (sprintf ("xls2oct: %d cached values instead of formula evaluations.\n", jerror)); endif + + rstatus = 1; + xls.limits = [lcol, rcol; firstrow, lastrow]; + endfunction @@ -599,167 +600,167 @@ function [ rawarr, xls, rstatus ] = xls2jxla2oct (xls, wsh, cellrange, spsh_opts) - persistent ctype; persistent months; - if (isempty (ctype)) - ctype = cell (11, 1); - # Get enumerated cell types. Beware as they start at 0 not 1 - ctype( 1) = (java_get ('jxl.CellType', 'BOOLEAN')).toString (); - ctype( 2) = (java_get ('jxl.CellType', 'BOOLEAN_FORMULA')).toString (); - ctype( 3) = (java_get ('jxl.CellType', 'DATE')).toString (); - ctype( 4) = (java_get ('jxl.CellType', 'DATE_FORMULA')).toString (); - ctype( 5) = (java_get ('jxl.CellType', 'EMPTY')).toString (); - ctype( 6) = (java_get ('jxl.CellType', 'ERROR')).toString (); - ctype( 7) = (java_get ('jxl.CellType', 'FORMULA_ERROR')).toString (); - ctype( 8) = (java_get ('jxl.CellType', 'NUMBER')).toString (); - ctype( 9) = (java_get ('jxl.CellType', 'LABEL')).toString (); - ctype(10) = (java_get ('jxl.CellType', 'NUMBER_FORMULA')).toString (); - ctype(11) = (java_get ('jxl.CellType', 'STRING_FORMULA')).toString (); - months = {'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'}; - endif - - rstatus = 0; - wb = xls.workbook; - - # Check if requested worksheet exists in the file & if so, get pointer - nr_of_sheets = wb.getNumberOfSheets (); - shnames = char (wb.getSheetNames ()); - if (isnumeric (wsh)) - if (wsh > nr_of_sheets), error (sprintf ("Worksheet # %d bigger than nr. of sheets (%d) in file %s", wsh, nr_of_sheets, xls.filename)); endif - sh = wb.getSheet (wsh - 1); # JXL sheet count 0-based - printf ("(Reading from worksheet %s)\n", shnames {wsh}); - else - sh = wb.getSheet (wsh); - if (isempty (sh)), error (sprintf ("Worksheet %s not found in file %s", wsh, xls.filename)); endif - end + persistent ctype; persistent months; + if (isempty (ctype)) + ctype = cell (11, 1); + # Get enumerated cell types. Beware as they start at 0 not 1 + ctype( 1) = (java_get ('jxl.CellType', 'BOOLEAN')).toString (); + ctype( 2) = (java_get ('jxl.CellType', 'BOOLEAN_FORMULA')).toString (); + ctype( 3) = (java_get ('jxl.CellType', 'DATE')).toString (); + ctype( 4) = (java_get ('jxl.CellType', 'DATE_FORMULA')).toString (); + ctype( 5) = (java_get ('jxl.CellType', 'EMPTY')).toString (); + ctype( 6) = (java_get ('jxl.CellType', 'ERROR')).toString (); + ctype( 7) = (java_get ('jxl.CellType', 'FORMULA_ERROR')).toString (); + ctype( 8) = (java_get ('jxl.CellType', 'NUMBER')).toString (); + ctype( 9) = (java_get ('jxl.CellType', 'LABEL')).toString (); + ctype(10) = (java_get ('jxl.CellType', 'NUMBER_FORMULA')).toString (); + ctype(11) = (java_get ('jxl.CellType', 'STRING_FORMULA')).toString (); + months = {'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'}; + endif + + rstatus = 0; + wb = xls.workbook; + + # Check if requested worksheet exists in the file & if so, get pointer + nr_of_sheets = wb.getNumberOfSheets (); + shnames = char (wb.getSheetNames ()); + if (isnumeric (wsh)) + if (wsh > nr_of_sheets), error (sprintf ("Worksheet # %d bigger than nr. of sheets (%d) in file %s", wsh, nr_of_sheets, xls.filename)); endif + sh = wb.getSheet (wsh - 1); # JXL sheet count 0-based + printf ("(Reading from worksheet %s)\n", shnames {wsh}); + else + sh = wb.getSheet (wsh); + if (isempty (sh)), error (sprintf ("Worksheet %s not found in file %s", wsh, xls.filename)); endif + end - if (isempty (cellrange)) - # Get numeric sheet pointer (1-based) - ii = 1; - while (ii <= nr_of_sheets) - if (strcmp (wsh, shnames{ii}) == 1) - wsh = ii; - ii = nr_of_sheets + 1; - else - ++ii; - endif - endwhile - # Get data rectangle row & column numbers (1-based) - [firstrow, lastrow, lcol, rcol] = getusedrange (xls, wsh); - if (firstrow == 0 && lastrow == 0) - # Empty sheet - rawarr = {}; - printf ("Worksheet '%s' contains no data\n", shnames {wsh}); - rstatus = 1; - return; - else - nrows = lastrow - firstrow + 1; - ncols = rcol - lcol + 1; - endif - else - # Translate range to row & column numbers (1-based) - [dummy, nrows, ncols, firstrow, lcol] = parse_sp_range (cellrange); - # Check for too large requested range against actually present range - lastrow = min (firstrow + nrows - 1, sh.getRows ()); - nrows = min (nrows, sh.getRows () - firstrow + 1); - ncols = min (ncols, sh.getColumns () - lcol + 1); - rcol = lcol + ncols - 1; - endif + if (isempty (cellrange)) + # Get numeric sheet pointer (1-based) + ii = 1; + while (ii <= nr_of_sheets) + if (strcmp (wsh, shnames{ii}) == 1) + wsh = ii; + ii = nr_of_sheets + 1; + else + ++ii; + endif + endwhile + # Get data rectangle row & column numbers (1-based) + [firstrow, lastrow, lcol, rcol] = getusedrange (xls, wsh); + if (firstrow == 0 && lastrow == 0) + # Empty sheet + rawarr = {}; + printf ("Worksheet '%s' contains no data\n", shnames {wsh}); + rstatus = 1; + return; + else + nrows = lastrow - firstrow + 1; + ncols = rcol - lcol + 1; + endif + else + # Translate range to row & column numbers (1-based) + [dummy, nrows, ncols, firstrow, lcol] = parse_sp_range (cellrange); + # Check for too large requested range against actually present range + lastrow = min (firstrow + nrows - 1, sh.getRows ()); + nrows = min (nrows, sh.getRows () - firstrow + 1); + ncols = min (ncols, sh.getColumns () - lcol + 1); + rcol = lcol + ncols - 1; + endif - # Read contents into rawarr - rawarr = cell (nrows, ncols); # create placeholder - for jj = lcol : rcol - for ii = firstrow:lastrow - scell = sh.getCell (jj-1, ii-1); - switch char (scell.getType ()) - case ctype {1} # Boolean - rawarr {ii+1-firstrow, jj+1-lcol} = scell.getValue (); - case ctype {2} # Boolean formula - if (spsh_opts.formulas_as_text) - tmp = scell.getFormula (); - rawarr {ii+1-firstrow, jj+1-lcol} = ["=" tmp]; - else - rawarr {ii+1-firstrow, jj+1-lcol} = scell.getValue (); - endif - case ctype {3} # Date - try - % Older JXL.JAR, returns float - rawarr {ii+1-firstrow, jj+1-lcol} = scell.getValue (); - catch - % Newer JXL.JAR, returns date string w. epoch = 1-1-1900 :-( - tmp = strsplit (char (scell.getDate ()), ' '); - yy = str2num (tmp{6}); - mo = find (ismember (months, upper (tmp{2})) == 1); - dd = str2num (tmp{3}); - hh = str2num (tmp{4}(1:2)); - mi = str2num (tmp{4}(4:5)); - ss = str2num (tmp{4}(7:8)); - if (scell.isTime ()) - yy = mo = dd = 0; - endif - rawarr {ii+1-firstrow, jj+1-lcol} = datenum (yy, mo, dd, hh, mi, ss); - end_try_catch - case ctype {4} # Date formula - if (spsh_opts.formulas_as_text) - tmp = scell.getFormula (); - rawarr {ii+1-firstrow, jj+1-lcol} = ["=" tmp]; - else - unwind_protect - % Older JXL.JAR, returns float - tmp = scell.getValue (); - % if we get here, we got a float (old JXL). - % Check if it is time - if (~scell.isTime ()) - % Reset rawarr <> so it can be processed below as date string - rawarr {ii+1-firstrow, jj+1-lcol} = []; - else - rawarr {ii+1-firstrow, jj+1-lcol} = tmp; - end - unwind_protect_cleanup - if (isempty (rawarr {ii+1-firstrow, jj+1-lcol})) - % Newer JXL.JAR, returns date string w. epoch = 1-1-1900 :-( - tmp = strsplit (char (scell.getDate ()), ' '); - yy = str2num (tmp{6}); - mo = find (ismember (months, upper (tmp{2})) == 1); - dd = str2num (tmp{3}); - hh = str2num (tmp{4}(1:2)); - mi = str2num (tmp{4}(4:5)); - ss = str2num (tmp{4}(7:8)); - if (scell.isTime ()) - yy = 0; mo = 0; dd = 0; - end - rawarr {ii+1-firstrow, jj+1-lcol} = datenum (yy, mo, dd, hh, mi, ss); - endif - end_unwind_protect - endif - case { ctype {5}, ctype {6}, ctype {7} } - # Empty, Error or Formula error. Nothing to do here - case ctype {8} # Number - rawarr {ii+1-firstrow, jj+1-lcol} = scell.getValue (); - case ctype {9} # String - rawarr {ii+1-firstrow, jj+1-lcol} = scell.getString (); - case ctype {10} # Numerical formula - if (spsh_opts.formulas_as_text) - tmp = scell.getFormula (); - rawarr {ii+1-firstrow, jj+1-lcol} = ["=" tmp]; - else - rawarr {ii+1-firstrow, jj+1-lcol} = scell.getValue (); - endif - case ctype {11} # String formula - if (spsh_opts.formulas_as_text) - tmp = scell.getFormula (); - rawarr {ii+1-firstrow, jj+1-lcol} = ["=" tmp]; - else - rawarr {ii+1-firstrow, jj+1-lcol} = scell.getString (); - endif - otherwise - # Do nothing - endswitch - endfor - endfor + # Read contents into rawarr + rawarr = cell (nrows, ncols); # create placeholder + for jj = lcol : rcol + for ii = firstrow:lastrow + scell = sh.getCell (jj-1, ii-1); + switch char (scell.getType ()) + case ctype {1} # Boolean + rawarr {ii+1-firstrow, jj+1-lcol} = scell.getValue (); + case ctype {2} # Boolean formula + if (spsh_opts.formulas_as_text) + tmp = scell.getFormula (); + rawarr {ii+1-firstrow, jj+1-lcol} = ["=" tmp]; + else + rawarr {ii+1-firstrow, jj+1-lcol} = scell.getValue (); + endif + case ctype {3} # Date + try + % Older JXL.JAR, returns float + rawarr {ii+1-firstrow, jj+1-lcol} = scell.getValue (); + catch + % Newer JXL.JAR, returns date string w. epoch = 1-1-1900 :-( + tmp = strsplit (char (scell.getDate ()), ' '); + yy = str2num (tmp{6}); + mo = find (ismember (months, upper (tmp{2})) == 1); + dd = str2num (tmp{3}); + hh = str2num (tmp{4}(1:2)); + mi = str2num (tmp{4}(4:5)); + ss = str2num (tmp{4}(7:8)); + if (scell.isTime ()) + yy = mo = dd = 0; + endif + rawarr {ii+1-firstrow, jj+1-lcol} = datenum (yy, mo, dd, hh, mi, ss); + end_try_catch + case ctype {4} # Date formula + if (spsh_opts.formulas_as_text) + tmp = scell.getFormula (); + rawarr {ii+1-firstrow, jj+1-lcol} = ["=" tmp]; + else + unwind_protect + % Older JXL.JAR, returns float + tmp = scell.getValue (); + % if we get here, we got a float (old JXL). + % Check if it is time + if (~scell.isTime ()) + % Reset rawarr <> so it can be processed below as date string + rawarr {ii+1-firstrow, jj+1-lcol} = []; + else + rawarr {ii+1-firstrow, jj+1-lcol} = tmp; + end + unwind_protect_cleanup + if (isempty (rawarr {ii+1-firstrow, jj+1-lcol})) + % Newer JXL.JAR, returns date string w. epoch = 1-1-1900 :-( + tmp = strsplit (char (scell.getDate ()), ' '); + yy = str2num (tmp{6}); + mo = find (ismember (months, upper (tmp{2})) == 1); + dd = str2num (tmp{3}); + hh = str2num (tmp{4}(1:2)); + mi = str2num (tmp{4}(4:5)); + ss = str2num (tmp{4}(7:8)); + if (scell.isTime ()) + yy = 0; mo = 0; dd = 0; + end + rawarr {ii+1-firstrow, jj+1-lcol} = datenum (yy, mo, dd, hh, mi, ss); + endif + end_unwind_protect + endif + case { ctype {5}, ctype {6}, ctype {7} } + # Empty, Error or Formula error. Nothing to do here + case ctype {8} # Number + rawarr {ii+1-firstrow, jj+1-lcol} = scell.getValue (); + case ctype {9} # String + rawarr {ii+1-firstrow, jj+1-lcol} = scell.getString (); + case ctype {10} # Numerical formula + if (spsh_opts.formulas_as_text) + tmp = scell.getFormula (); + rawarr {ii+1-firstrow, jj+1-lcol} = ["=" tmp]; + else + rawarr {ii+1-firstrow, jj+1-lcol} = scell.getValue (); + endif + case ctype {11} # String formula + if (spsh_opts.formulas_as_text) + tmp = scell.getFormula (); + rawarr {ii+1-firstrow, jj+1-lcol} = ["=" tmp]; + else + rawarr {ii+1-firstrow, jj+1-lcol} = scell.getString (); + endif + otherwise + # Do nothing + endswitch + endfor + endfor - rstatus = 1; - xls.limits = [lcol, rcol; firstrow, lastrow]; - + rstatus = 1; + xls.limits = [lcol, rcol; firstrow, lastrow]; + endfunction @@ -796,84 +797,84 @@ function [ rawarr, xls, rstatus ] = xls2oxs2oct (xls, wsh, cellrange, spsh_opts) - persistent ctype; - if (isempty (ctype)) - ctype = zeros (6, 1); - # Get enumerated cell types. Beware as they start at 0 not 1 - ctype( 1) = (java_get ('com.extentech.ExtenXLS.CellHandle', 'TYPE_STRING')); # 0 - ctype( 2) = (java_get ('com.extentech.ExtenXLS.CellHandle', 'TYPE_FP')); # 1 - ctype( 3) = (java_get ('com.extentech.ExtenXLS.CellHandle', 'TYPE_INT')); # 2 - ctype( 4) = (java_get ('com.extentech.ExtenXLS.CellHandle', 'TYPE_FORMULA')); # 3 - ctype( 5) = (java_get ('com.extentech.ExtenXLS.CellHandle', 'TYPE_BOOLEAN')); # 4 - ctype( 6) = (java_get ('com.extentech.ExtenXLS.CellHandle', 'TYPE_DOUBLE')); # 5 - endif - - rstatus = 0; - wb = xls.workbook; - - # Check if requested worksheet exists in the file & if so, get pointer - nr_of_sheets = wb.getNumWorkSheets (); - if (isnumeric (wsh)) - if (wsh > nr_of_sheets), error (sprintf ("Worksheet # %d bigger than nr. of sheets (%d) in file %s", wsh, nr_of_sheets, xls.filename)); endif - sh = wb.getWorkSheet (wsh - 1); # OXS sheet count 0-based - printf ("(Reading from worksheet %s)\n", sh.getSheetName ()); - else - try - sh = wb.getWorkSheet (wsh); - catch - error (sprintf ("Worksheet %s not found in file %s", wsh, xls.filename)); - end_try_catch - end + persistent ctype; + if (isempty (ctype)) + ctype = zeros (6, 1); + # Get enumerated cell types. Beware as they start at 0 not 1 + ctype( 1) = (java_get ('com.extentech.ExtenXLS.CellHandle', 'TYPE_STRING')); # 0 + ctype( 2) = (java_get ('com.extentech.ExtenXLS.CellHandle', 'TYPE_FP')); # 1 + ctype( 3) = (java_get ('com.extentech.ExtenXLS.CellHandle', 'TYPE_INT')); # 2 + ctype( 4) = (java_get ('com.extentech.ExtenXLS.CellHandle', 'TYPE_FORMULA')); # 3 + ctype( 5) = (java_get ('com.extentech.ExtenXLS.CellHandle', 'TYPE_BOOLEAN')); # 4 + ctype( 6) = (java_get ('com.extentech.ExtenXLS.CellHandle', 'TYPE_DOUBLE')); # 5 + endif + + rstatus = 0; + wb = xls.workbook; + + # Check if requested worksheet exists in the file & if so, get pointer + nr_of_sheets = wb.getNumWorkSheets (); + if (isnumeric (wsh)) + if (wsh > nr_of_sheets), error (sprintf ("Worksheet # %d bigger than nr. of sheets (%d) in file %s", wsh, nr_of_sheets, xls.filename)); endif + sh = wb.getWorkSheet (wsh - 1); # OXS sheet count 0-based + printf ("(Reading from worksheet %s)\n", sh.getSheetName ()); + else + try + sh = wb.getWorkSheet (wsh); + catch + error (sprintf ("Worksheet %s not found in file %s", wsh, xls.filename)); + end_try_catch + end - if (isempty (cellrange)) - # Get numeric sheet pointer (0-based) - wsh = sh.getTabIndex (); - # Get data rectangle row & column numbers (1-based) - [firstrow, lastrow, lcol, rcol] = getusedrange (xls, wsh+1); - if (firstrow == 0 && lastrow == 0) - # Empty sheet - rawarr = {}; - printf ("Worksheet '%s' contains no data\n", shnames {wsh}); - rstatus = 1; - return; - else - nrows = lastrow - firstrow + 1; - ncols = rcol - lcol + 1; - endif - else - # Translate range to row & column numbers (1-based) - [dummy, nrows, ncols, firstrow, lcol] = parse_sp_range (cellrange); - # Check for too large requested range against actually present range - lastrow = min (firstrow + nrows - 1, sh.getLastRow + 1 ()); - nrows = min (nrows, sh.getLastRow () - firstrow + 1); - ncols = min (ncols, sh.getLastCol () - lcol + 1); - rcol = lcol + ncols - 1; - endif + if (isempty (cellrange)) + # Get numeric sheet pointer (0-based) + wsh = sh.getTabIndex (); + # Get data rectangle row & column numbers (1-based) + [firstrow, lastrow, lcol, rcol] = getusedrange (xls, wsh+1); + if (firstrow == 0 && lastrow == 0) + # Empty sheet + rawarr = {}; + printf ("Worksheet '%s' contains no data\n", shnames {wsh}); + rstatus = 1; + return; + else + nrows = lastrow - firstrow + 1; + ncols = rcol - lcol + 1; + endif + else + # Translate range to row & column numbers (1-based) + [dummy, nrows, ncols, firstrow, lcol] = parse_sp_range (cellrange); + # Check for too large requested range against actually present range + lastrow = min (firstrow + nrows - 1, sh.getLastRow + 1 ()); + nrows = min (nrows, sh.getLastRow () - firstrow + 1); + ncols = min (ncols, sh.getLastCol () - lcol + 1); + rcol = lcol + ncols - 1; + endif - # Read contents into rawarr - rawarr = cell (nrows, ncols); # create placeholder - for jj = lcol:rcol - for ii = firstrow:lastrow - try - scell = sh.getCell (ii-1, jj-1); - sctype = scell.getCellType (); - rawarr {ii+1-firstrow, jj+1-lcol} = scell.getVal (); - if (sctype == ctype(2) || sctype == ctype(3) || sctype == ctype(6)) - rawarr {ii+1-firstrow, jj+1-lcol} = scell.getDoubleVal (); - endif - catch - # Empty or non-existing cell - end_try_catch - endfor - endfor + # Read contents into rawarr + rawarr = cell (nrows, ncols); # create placeholder + for jj = lcol:rcol + for ii = firstrow:lastrow + try + scell = sh.getCell (ii-1, jj-1); + sctype = scell.getCellType (); + rawarr {ii+1-firstrow, jj+1-lcol} = scell.getVal (); + if (sctype == ctype(2) || sctype == ctype(3) || sctype == ctype(6)) + rawarr {ii+1-firstrow, jj+1-lcol} = scell.getDoubleVal (); + endif + catch + # Empty or non-existing cell + end_try_catch + endfor + endfor - rstatus = 1; - xls.limits = [lcol, rcol; firstrow, lastrow]; - + rstatus = 1; + xls.limits = [lcol, rcol; firstrow, lastrow]; + endfunction -## Copyright (C) 2011 Philip Nienhuis <prn...@us...> +## Copyright (C) 2011,2012 Philip Nienhuis <prn...@us...> ## ## 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 @@ -909,7 +910,7 @@ # Check sheet pointer if (isnumeric (wsh)) - if (wsh < 1 || wsh > numel (sh_names)) + if (wsh < 1 || wsh > numel (sh_names)) error ("Sheet index %d out of range 1-%d", wsh, numel (sh_names)); endif else @@ -944,8 +945,8 @@ # out-of-range errors [ trow, brow, lcol, rcol ] = getusedrange (xls, wsh); if (isempty (datrange)) - nrows = brow - trow + 1; # Number of rows to be read - ncols = rcol - lcol + 1; # Number of columns to be read + nrows = brow - trow + 1; # Number of rows to be read + ncols = rcol - lcol + 1; # Number of columns to be read else [dummy, nrows, ncols, srow, scol] = parse_sp_range (datrange); # Truncate range silently if needed @@ -953,8 +954,8 @@ rcol = min (scol + ncols - 1, rcol); trow = max (trow, srow); lcol = max (lcol, scol); - nrows = min (brow - trow + 1, nrows); # Number of rows to be read - ncols = min (rcol - lcol + 1, ncols); # Number of columns to be read + nrows = min (brow - trow + 1, nrows); # Number of rows to be read + ncols = min (rcol - lcol + 1, ncols); # Number of columns to be read endif # Create storage for data at Octave side rawarr = cell (nrows, ncols); @@ -965,12 +966,12 @@ XCell = sh.getCellByPosition (jj, ii); cType = XCell.getType().getValue (); switch cType - case 1 # Value + case 1 # Value rawarr{ii-trow+2, jj-lcol+2} = XCell.getValue (); - case 2 # String + case 2 # String unotmp = java_new ('com.sun.star.uno.Type', 'com.sun.star.text.XText'); rawarr{ii-trow+2, jj-lcol+2} = XCell.queryInterface (unotmp).getString (); - case 3 # Formula + case 3 # Formula if (spsh_opts.formulas_as_text) rawarr{ii-trow+2, jj-lcol+2} = XCell.getFormula (); else This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |