From: <prn...@us...> - 2011-09-18 22:15:12
|
Revision: 8561 http://octave.svn.sourceforge.net/octave/?rev=8561&view=rev Author: prnienhuis Date: 2011-09-18 22:15:05 +0000 (Sun, 18 Sep 2011) Log Message: ----------- Adapted to LibreOffice 3.4+ features (mostly better ODS 1.2 compliance) Modified Paths: -------------- trunk/octave-forge/main/io/inst/oct2ods.m trunk/octave-forge/main/io/inst/oct2xls.m trunk/octave-forge/main/io/inst/ods2oct.m trunk/octave-forge/main/io/inst/xls2oct.m Modified: trunk/octave-forge/main/io/inst/oct2ods.m =================================================================== --- trunk/octave-forge/main/io/inst/oct2ods.m 2011-09-18 19:42:52 UTC (rev 8560) +++ trunk/octave-forge/main/io/inst/oct2ods.m 2011-09-18 22:15:05 UTC (rev 8561) @@ -1027,52 +1027,70 @@ else sh_names = {sh_names}; endif - # Check sheet pointer - # FIXME sheet capacity check needed. How many can fit in an OOo sprsh.file? - if (isnumeric (wsh)) - if (wsh < 1) - error ("Illegal sheet index: %d", wsh); - elseif (wsh > numel (sh_names)) - # New sheet to be added. First create sheet name but check if it already exists - shname = sprintf ("Sheet%d", numel (sh_names) + 1); - jj = strmatch (wsh, sh_names); - if (~isempty (jj)) - # New sheet name already in file, try to create a unique & reasonable one - ii = 1; filler = ''; maxtry = 5; - while (ii <= maxtry) - shname = sprintf ("Sheet%s%d", [filler "_"], numel (sh_names + 1)); - if (isempty (strmatch (wsh, sh_names))) - ii = 10; - else - ++ii; + + # Clear default 2 last sheets in case of a new spreadsheet file + if (ods.changed > 2) + ii = numel (sh_names); + while (ii > 1) + shnm = sh_names{ii}; + sheets.removeByName (shnm); + --ii; + endwhile + # Give remaining sheet a name + unotmp = java_new ('com.sun.star.uno.Type', 'com.sun.star.sheet.XSpreadsheet'); + sh = sheets.getByName (sh_names{1}).getObject.queryInterface (unotmp); + if (isnumeric (wsh)); wsh = sprintf ("Sheet%d", wsh); endif + unotmp = java_new ('com.sun.star.uno.Type', 'com.sun.star.container.XNamed'); + sh.queryInterface (unotmp).setName (wsh); + else + + # Check sheet pointer + # FIXME sheet capacity check needed. How many can fit in an OOo sprsh.file? + if (isnumeric (wsh)) + if (wsh < 1) + error ("Illegal sheet index: %d", wsh); + elseif (wsh > numel (sh_names)) + # New sheet to be added. First create sheet name but check if it already exists + shname = sprintf ("Sheet%d", numel (sh_names) + 1); + jj = strmatch (wsh, sh_names); + if (~isempty (jj)) + # New sheet name already in file, try to create a unique & reasonable one + ii = 1; filler = ''; maxtry = 5; + while (ii <= maxtry) + shname = sprintf ("Sheet%s%d", [filler "_"], numel (sh_names + 1)); + if (isempty (strmatch (wsh, sh_names))) + ii = 10; + else + ++ii; + endif + endwhile + if (ii > maxtry + 1) + error ("Could not add sheet with a unique name to file %s"); endif - endwhile - if (ii > maxtry + 1) - error ("Could not add sheet with a unique name to file %s"); endif + wsh = shname; + newsh = 1; + else + # turn wsh index into the associated sheet name + wsh = sh_names (wsh); endif - wsh = shname; - newsh = 1; else - # turn wsh index into the associated sheet name - wsh = sh_names (wsh); + # wsh is a sheet name. See if it exists already + if (isempty (strmatch (wsh, sh_names))) + # Not found. New sheet to be added + newsh = 1; + endif endif - else - # wsh is a sheet name. See if it exists already - if (isempty (strmatch (wsh, sh_names))) - # Not found. New sheet to be added - newsh = 1; + if (newsh) + # Add a new sheet. Sheet index MUST be a Java Short object + shptr = java_new ("java.lang.Short", sprintf ("%d", numel (sh_names) + 1)); + sh = sheets.insertNewByName (wsh, shptr); + else + # At this point we have a valid sheet name. Use it to get a sheet handle + unotmp = java_new ('com.sun.star.uno.Type', 'com.sun.star.sheet.XSpreadsheet'); + sh = sheets.getByName (wsh).getObject.queryInterface (unotmp); endif endif - if (newsh) - # Add a new sheet. Sheet index MUST be a Java Short object - shptr = java_new ("java.lang.Short", sprintf ("%d", numel (sh_names) + 1)); - sh = sheets.insertNewByName (wsh, shptr); - sheets = ods.workbook.getSheets (); - endif - # At this point we have a valid sheet name. Use it to get a sheet handle - unotmp = java_new ('com.sun.star.uno.Type', 'com.sun.star.sheet.XSpreadsheet'); - sh = sheets.getByName (wsh).getObject.queryInterface (unotmp); # Check size of data array & range / capacity of worksheet & prepare vars [nr, nc] = size (c_arr); @@ -1090,7 +1108,7 @@ fptr = cellfun (@(x) ischar (x) && strncmp (x, "=", 1), c_arr); typearr(fptr) = ctype(4); # FORMULA endif - +keyboard # Transfer data to sheet for ii=1:nrows for jj=1:ncols Modified: trunk/octave-forge/main/io/inst/oct2xls.m =================================================================== --- trunk/octave-forge/main/io/inst/oct2xls.m 2011-09-18 19:42:52 UTC (rev 8560) +++ trunk/octave-forge/main/io/inst/oct2xls.m 2011-09-18 22:15:05 UTC (rev 8561) @@ -915,52 +915,70 @@ else sh_names = {sh_names}; endif - # Check sheet pointer - # FIXME sheet capacity check needed - if (isnumeric (wsh)) - if (wsh < 1) - error ("Illegal sheet index: %d", wsh); - elseif (wsh > numel (sh_names)) - # New sheet to be added. First create sheet name but check if it already exists - shname = sprintf ("Sheet%d", numel (sh_names) + 1); - jj = strmatch (wsh, sh_names); - if (~isempty (jj)) - # New sheet name already in file, try to create a unique & reasonable one - ii = 1; filler = ''; maxtry = 5; - while (ii <= maxtry) - shname = sprintf ("Sheet%s%d", [filler "_"], numel (sh_names + 1)); - if (isempty (strmatch (wsh, sh_names))) - ii = 10; - else - ++ii; + + # Clear default 2 last sheets in case of a new spreadsheet file + if (ods.changed > 2) + ii = numel (sh_names); + while (ii > 1) + shnm = sh_names{ii}; + sheets.removeByName (shnm); + --ii; + endwhile + # Give remaining sheet a name + unotmp = java_new ('com.sun.star.uno.Type', 'com.sun.star.sheet.XSpreadsheet'); + sh = sheets.getByName (sh_names{1}).getObject.queryInterface (unotmp); + if (isnumeric (wsh)); wsh = sprintf ("Sheet%d", wsh); endif + unotmp = java_new ('com.sun.star.uno.Type', 'com.sun.star.container.XNamed'); + sh.queryInterface (unotmp).setName (wsh); + else + + # Check sheet pointer + # FIXME sheet capacity check needed + if (isnumeric (wsh)) + if (wsh < 1) + error ("Illegal sheet index: %d", wsh); + elseif (wsh > numel (sh_names)) + # New sheet to be added. First create sheet name but check if it already exists + shname = sprintf ("Sheet%d", numel (sh_names) + 1); + jj = strmatch (wsh, sh_names); + if (~isempty (jj)) + # New sheet name already in file, try to create a unique & reasonable one + ii = 1; filler = ''; maxtry = 5; + while (ii <= maxtry) + shname = sprintf ("Sheet%s%d", [filler "_"], numel (sh_names + 1)); + if (isempty (strmatch (wsh, sh_names))) + ii = 10; + else + ++ii; + endif + endwhile + if (ii > maxtry + 1) + error ("Could not add sheet with a unique name to file %s"); endif - endwhile - if (ii > maxtry + 1) - error ("Could not add sheet with a unique name to file %s"); endif + wsh = shname; + newsh = 1; + else + # turn wsh index into the associated sheet name + wsh = sh_names (wsh); endif - wsh = shname; - newsh = 1; else - # turn wsh index into the associated sheet name - wsh = sh_names (wsh); + # wsh is a sheet name. See if it exists already + if (isempty (strmatch (wsh, sh_names))) + # Not found. New sheet to be added + newsh = 1; + endif endif - else - # wsh is a sheet name. See if it exists already - if (isempty (strmatch (wsh, sh_names))) - # Not found. New sheet to be added - newsh = 1; + if (newsh) + # Add a new sheet. Sheet index MUST be a Java Short object + shptr = java_new ("java.lang.Short", sprintf ("%d", numel (sh_names) + 1)); + sh = sheets.insertNewByName (wsh, shptr); + else + # At this point we have a valid sheet name. Use it to get a sheet handle + unotmp = java_new ('com.sun.star.uno.Type', 'com.sun.star.sheet.XSpreadsheet'); + sh = sheets.getByName (wsh).getObject.queryInterface (unotmp); endif endif - if (newsh) - # Add a new sheet. Sheet index MUST be a Java Short object - shptr = java_new ("java.lang.Short", sprintf ("%d", numel (sh_names) + 1)); - sh = sheets.insertNewByName (wsh, shptr); - sheets = xls.workbook.getSheets (); - endif - # At this point we have a valid sheet name. Use it to get a sheet handle - unotmp = java_new ('com.sun.star.uno.Type', 'com.sun.star.sheet.XSpreadsheet'); - sh = sheets.getByName (wsh).getObject.queryInterface (unotmp); # Check size of data array & range / capacity of worksheet & prepare vars [nr, nc] = size (c_arr); Modified: trunk/octave-forge/main/io/inst/ods2oct.m =================================================================== --- trunk/octave-forge/main/io/inst/ods2oct.m 2011-09-18 19:42:52 UTC (rev 8560) +++ trunk/octave-forge/main/io/inst/ods2oct.m 2011-09-18 22:15:05 UTC (rev 8561) @@ -112,6 +112,7 @@ ## " Extended check on spsh_opts (must be a struct) ## 2010-10-27 Moved cropping rawarr from empty outer rows & columns to here ## 2011-05-06 Experimental UNO support +## 2011-09-18 Set rstatus var here ## ## (Latest update of subfunctions below: 2011-09-18) @@ -148,26 +149,28 @@ # Read ods file tru Java & ODF toolkit switch ods.odfvsn case '0.7.5' - [rawarr, ods, rstatus] = ods2jotk2oct (ods, wsh, datrange, spsh_opts); + [rawarr, ods] = ods2jotk2oct (ods, wsh, datrange, spsh_opts); case '0.8.6' - [rawarr, ods, rstatus] = ods3jotk2oct (ods, wsh, datrange, spsh_opts); + [rawarr, ods] = ods3jotk2oct (ods, wsh, datrange, spsh_opts); otherwise error ("Unsupported odfdom version or invalid ods file pointer."); endswitch elseif (strcmp (ods.xtype, 'JOD')) # Read ods file tru Java & jOpenDocument. JOD doesn't know about formulas :-( - [rawarr, ods, rstatus] = ods2jod2oct (ods, wsh, datrange); + [rawarr, ods] = ods2jod2oct (ods, wsh, datrange); elseif (strcmp (ods.xtype, 'UNO')) # Read ods file tru Java & UNO - [rawarr, ods, rstatus] = ods2uno2oct (ods, wsh, datrange, spsh_opts); + [rawarr, ods] = ods2uno2oct (ods, wsh, datrange, spsh_opts); # elseif # ---- < Other interfaces here > else error (sprintf ("ods2oct: unknown OpenOffice.org .ods interface - %s.", ods.xtype)); endif + rstatus = ~isempty (rawarr); + # Optionally strip empty outer rows and columns & keep track of original data location - if (spsh_opts.strip_array && ~isempty (rawarr)) + if (spsh_opts.strip_array && rstatus) emptr = cellfun ('isempty', rawarr); if (all (all (emptr))) rawarr = {}; @@ -225,14 +228,13 @@ ## 2010-03-19 More code cleanup & fixes for bugs introduced 18/3/2010 8-() ## 2010-08-03 Added preliminary support for reading back formulas as text strings ## 2010-10-27 Moved cropping rawarr from empty outer rows & columns to caller +## 2011-09-18 Remove rstatus var (now set in caller) -function [ rawarr, ods, rstatus ] = ods2jotk2oct (ods, wsh, crange, spsh_opts) +function [ rawarr, ods ] = ods2jotk2oct (ods, wsh, crange, spsh_opts) # Parts after user gfterry in # http://www.oooforum.org/forum/viewtopic.phtml?t=69060 - rstatus = 0; - # Get contents and table stuff from the workbook odfcont = ods.workbook; # Use a local copy just to be sure. octave # makes physical copies only when needed (?) @@ -433,11 +435,12 @@ ## Updates: ## 2010-10-27 Moved cropping rawarr from empty outer rows & columns to caller ## 2010-11-13 Added workaround for reading text cells in files made by jOpenDocument 1.2bx +## 2011-09-18 Comment out workaround for jOpenDocument bug (no OfficeValueAttr set) +## because this casts all numeric cells to string type for properly written ODS1.2 +## '' Remove rstatus var (now set in caller) -function [ rawarr, ods, rstatus ] = ods3jotk2oct (ods, wsh, crange, spsh_opts) +function [ rawarr, ods ] = ods3jotk2oct (ods, wsh, crange, spsh_opts) - rstatus = 0; - # Get contents and table stuff from the workbook odfcont = ods.workbook; # Use a local copy just to be sure. octave # makes physical copies only when needed (?) @@ -502,16 +505,16 @@ ocell = row.getCellByIndex (jj-1); if ~isempty (ocell) otype = deblank (tolower (ocell.getValueType ())); - if (spsh_opts.formulas_as_text) + if (spsh_opts.formulas_as_text) if ~isempty (ocell.getFormula ()) otype = 'formula'; endif endif - # Provisions for catching jOpenDocument 1.2b bug where text cells - # haven't been assigned an <office:value-type='string'> attribute - if (~isempty (ocell)) - if (findstr ('<text:', char (ocell.getOdfElement ()))), otype = 'string'; endif - endif +# # Provisions for catching jOpenDocument 1.2b bug where text cells +# # haven't been assigned an <office:value-type='string'> attribute +# if (~isempty (ocell)) +# if (findstr ('<text:', char (ocell.getOdfElement ()))), otype = 'string'; endif +# endif # At last, read the data switch otype case {'float', 'currency', 'percentage'} @@ -604,8 +607,11 @@ ## allows better cell type parsing and is therefore more reliable ## 2010-10-27 Moved cropping rawarr from empty outer rows & columns to here ## 2010-11-13 Added workaround for reading text cells in files made by jOpenDocument 1.2bx +## 2011-09-18 Comment out workaround for jOpenDocument bug (no OfficeValueAttr set) +## because this casts all numeric cells to string type for properly written ODS1.2 +## '' Remove rstatus var (now set in caller) -function [ rawarr, ods, rstatus] = ods2jod2oct (ods, wsh, crange) +function [ rawarr, ods] = ods2jod2oct (ods, wsh, crange) persistent months; months = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}; @@ -674,10 +680,10 @@ try scell = sh.getCellAt (lcol+jj-2, trow+ii-2); sctype = char (scell.getValueType ()); - # Workaround for sheets written by jOpenDocument 1.2bx (no value-type attrb): - if (~isempty (scell)) - if (findstr ('<text:', char (scell))), sctype = STRING; endif - endif +# # Workaround for sheets written by jOpenDocument 1.2bx (no value-type attrb): +# if (~isempty (scell)) +# if (findstr ('<text:', char (scell))), sctype = STRING; endif +# endif switch sctype case { FLOAT, CURRENCY, PERCENTAGE } rawarr{ii, jj} = scell.getValue ().doubleValue (); @@ -755,8 +761,6 @@ # Keep track of data rectangle limits ods.limits = [lcol, rcol; trow, brow]; - - rstatus = ~isempty (rawarr); endfunction @@ -783,8 +787,10 @@ ## Created: 2011-05-05 ## Updates: ## 2011-09-18 Adapted sh_names type to LO 3.4.1 +## '' Remove default 2 last sheets (LibreOffice 3.4.+) +## '' Remove rstatus var (now set in caller) -function [rawarr, ods, rstatus] = ods2uno2oct (ods, wsh, datrange, spsh_opts) +function [rawarr, ods] = ods2uno2oct (ods, wsh, datrange, spsh_opts) sheets = ods.workbook.getSheets (); sh_names = sheets.getElementNames (); @@ -797,7 +803,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 @@ -806,7 +812,7 @@ wsh = ii; endif unotmp = java_new ('com.sun.star.uno.Type', 'com.sun.star.sheet.XSpreadsheet'); - sh = sheets.getByName(sh_names(wsh)).getObject.queryInterface (unotmp); + sh = sheets.getByName(sh_names{wsh}).getObject.queryInterface (unotmp); unotmp = java_new ('com.sun.star.uno.Type', 'com.sun.star.sheet.XCellRangesQuery'); xRQ = sh.queryInterface (unotmp); @@ -874,6 +880,4 @@ # Keep track of data rectangle limits ods.limits = [lcol, rcol; trow, brow]; - rstatus = ~isempty (rawarr); - endfunction Modified: trunk/octave-forge/main/io/inst/xls2oct.m =================================================================== --- trunk/octave-forge/main/io/inst/xls2oct.m 2011-09-18 19:42:52 UTC (rev 8560) +++ trunk/octave-forge/main/io/inst/xls2oct.m 2011-09-18 22:15:05 UTC (rev 8561) @@ -397,7 +397,7 @@ ## 2010-11-14 Fixed sheet # index (was offset by -1) in call to getusedrange() in case #3 of text sheet name arg -function [ rawarr, xls, status ] = xls2jpoi2oct (xls, wsh, cellrange=[], spsh_opts) +function [ rawarr, xls, rstatus ] = xls2jpoi2oct (xls, wsh, cellrange=[], spsh_opts) persistent ctype; if (isempty (ctype)) @@ -410,7 +410,7 @@ ctype(6) = java_get ('org.apache.poi.ss.usermodel.Cell', 'CELL_TYPE_ERROR'); endif - status = 0; jerror = 0; + rstatus = 0; jerror = 0; wb = xls.workbook; # Check if requested worksheet exists in the file & if so, get pointer @@ -517,7 +517,7 @@ if (jerror > 0) warning (sprintf ("xls2oct: %d cached values instead of formula evaluations.\n", jerror)); endif - status = 1; + rstatus = 1; xls.limits = [lcol, rcol; firstrow, lastrow]; endfunction @@ -575,7 +575,7 @@ ## 2011-04-11 (Ron Goldman <ro...@oc...>) Fixed missing months var, wrong arg ## " order in strsplit, wrong isTime condition -function [ rawarr, xls, status ] = xls2jxla2oct (xls, wsh, cellrange=[], spsh_opts) +function [ rawarr, xls, rstatus ] = xls2jxla2oct (xls, wsh, cellrange=[], spsh_opts) persistent ctype; persistent months; if (isempty (ctype)) @@ -595,7 +595,7 @@ months = {'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'}; endif - status = 0; + rstatus = 0; wb = xls.workbook; # Check if requested worksheet exists in the file & if so, get pointer @@ -735,7 +735,7 @@ endfor endfor - status = 1; + rstatus = 1; xls.limits = [lcol, rcol; firstrow, lastrow]; endfunction @@ -772,7 +772,7 @@ ## Created: 2011-03-26 ## Updates: -function [ rawarr, xls, status ] = xls2oxs2oct (xls, wsh, cellrange=[], spsh_opts) +function [ rawarr, xls, rstatus ] = xls2oxs2oct (xls, wsh, cellrange=[], spsh_opts) persistent ctype; if (isempty (ctype)) @@ -786,7 +786,7 @@ ctype( 6) = (java_get ('com.extentech.ExtenXLS.CellHandle', 'TYPE_DOUBLE')); # 5 endif - status = 0; + rstatus = 0; wb = xls.workbook; # Check if requested worksheet exists in the file & if so, get pointer @@ -845,7 +845,7 @@ endfor endfor - status = 1; + rstatus = 1; xls.limits = [lcol, rcol; firstrow, lastrow]; endfunction @@ -887,7 +887,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 @@ -896,7 +896,7 @@ wsh = ii; endif unotmp = java_new ('com.sun.star.uno.Type', 'com.sun.star.sheet.XSpreadsheet'); - sh = sheets.getByName(sh_names(wsh)).getObject.queryInterface (unotmp); + sh = sheets.getByName(sh_names{wsh}).getObject.queryInterface (unotmp); unotmp = java_new ('com.sun.star.uno.Type', 'com.sun.star.sheet.XCellRangesQuery'); xRQ = sh.queryInterface (unotmp); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |