From: <prn...@us...> - 2009-12-13 20:51:34
|
Revision: 6643 http://octave.svn.sourceforge.net/octave/?rev=6643&view=rev Author: prnienhuis Date: 2009-12-13 20:51:27 +0000 (Sun, 13 Dec 2009) Log Message: ----------- First throw at ODS spreadsheet (OpenOffice.org) support. Only reading works, writing too unreliable yet. Added Paths: ----------- trunk/octave-forge/main/io/inst/calccelladdress.m trunk/octave-forge/main/io/inst/ods2oct.m trunk/octave-forge/main/io/inst/odsclose.m trunk/octave-forge/main/io/inst/odsopen.m trunk/octave-forge/main/io/inst/odsread.m Added: trunk/octave-forge/main/io/inst/calccelladdress.m =================================================================== --- trunk/octave-forge/main/io/inst/calccelladdress.m (rev 0) +++ trunk/octave-forge/main/io/inst/calccelladdress.m 2009-12-13 20:51:27 UTC (rev 6643) @@ -0,0 +1,39 @@ +## Copyright (C) 2009 Philip +## +## 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 2 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 Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## calccelladdress - compute spreadsheet style cell address from +## row & column index. +## +## Max column index currently set to 1024 (ODS) + +## Author: Philip Nienhuis <prnienhuis at users.sf.net> +## Created: 2009-12-12 + +function [ celladdress ] = calccelladdress (trow, lcol, row, column) + + colnr = lcol + column - 1; + if (colnr> 1024) error ("Column nr >1024"); endif + str = char (rem ((colnr-1), 26) + 'A'); + if (colnr > 26 && colnr < 703) + tmp = char (floor ((colnr - 27) / 26) + 'A'); + str = [tmp str]; + elseif (colnr > 702) + tmp = char (floor ((colnr - 703) / 26) + 'A'); + str = ['A' tmp str]; + endif + celladdress = sprintf ("%s%d", str, trow + row - 1); + +endfunction \ No newline at end of file Added: trunk/octave-forge/main/io/inst/ods2oct.m =================================================================== --- trunk/octave-forge/main/io/inst/ods2oct.m (rev 0) +++ trunk/octave-forge/main/io/inst/ods2oct.m 2009-12-13 20:51:27 UTC (rev 6643) @@ -0,0 +1,62 @@ +## Copyright (C) 2009 Philip Nienhuis <pr.nienhuis at users.sf.net> +## +## 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 2 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 Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## ods2oct - get data out of an ODS spreadsheet into octave. +## Watch out, no error checks, and spreadsheet formula error results +## are conveyed as 0 (zero). +## +## Author: Philip Nienhuis +## Created: 2009-12-13 + +function [ rawarr, ods ] = ods2oct (ods, wsh, range) + + sh = ods.workbook.getSheet (wsh); + + [dummy, nrows, ncols, toprow, lcol] = parse_sp_range (range); + + # Placeholder for data + rawarr = cell (nrows, ncols); + for ii=1:nrows + for jj = 1:ncols + celladdress = calccelladdress (toprow, lcol, ii, jj); + try + val = sh.getCellAt (celladdress).getValue (); + catch + # No panic, probably a merged cell + val = {}; + end_try_catch + if (~isempty (val)) + if (ischar (val)) + # Text string + rawarr (ii, jj) = val; + elseif (isnumeric (val)) + # Boolean + if (val) rawarr (ii, jj) = true; else; rawarr (ii, jj) = false; endif + else + try + val = sh.getCellAt (celladdress).getValue ().doubleValue (); + rawarr (ii, jj) = val; + catch + # Probably empty Cell + end_try_catch + endif + endif + endfor + endfor + + ods.limits = [ lcol, lcol+ncols-1; toprow, toprow+nrows-1 ]; + +endfunction Added: trunk/octave-forge/main/io/inst/odsclose.m =================================================================== --- trunk/octave-forge/main/io/inst/odsclose.m (rev 0) +++ trunk/octave-forge/main/io/inst/odsclose.m 2009-12-13 20:51:27 UTC (rev 6643) @@ -0,0 +1,32 @@ +## Copyright (C) 2009 Philip Nienhuis <prnienhuis at users.sf.net> +## +## 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 2 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 Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## odsclose - close an ods spreadsheet file & if needed write out to disk +## Basic version - no checks yet + +## Author: Philip Nienhuis +## Created: 2009-12-13 + +function [ ods ] = odsclose (ods) + + if (ods.changed > 0) + file = java_new ('java.io.File', ods.filename); + ods.workbook.saveAs (file); + endif + + ods = []; + +endfunction Added: trunk/octave-forge/main/io/inst/odsopen.m =================================================================== --- trunk/octave-forge/main/io/inst/odsopen.m (rev 0) +++ trunk/octave-forge/main/io/inst/odsopen.m 2009-12-13 20:51:27 UTC (rev 6643) @@ -0,0 +1,30 @@ +## Copyright (C) 2009 Philip Nienhuis <prnienhuis at users.sf.net> +## +## 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 2 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 Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## odsopen - open ODS spreadsheet +## Basic version, no checks yet + +## Author: Philip Nienhuis +## Created: 2009-12-13 + +function [ ods ] = odsopen (filename, rw=0) + + file = java_new ('java.io.File', filename); + wb = java_invoke ('org.jopendocument.dom.spreadsheet.SpreadSheet', 'createFromFile', file); + + ods = struct ("xtype", 'JOD', "app", file, "filename", filename, "workbook", wb, "changed", 0, "limits", []); + +endfunction Added: trunk/octave-forge/main/io/inst/odsread.m =================================================================== --- trunk/octave-forge/main/io/inst/odsread.m (rev 0) +++ trunk/octave-forge/main/io/inst/odsread.m 2009-12-13 20:51:27 UTC (rev 6643) @@ -0,0 +1,61 @@ +## Copyright (C) 2009 Philip Nienhuis <prnienhuis at users.sf.net> +## +## 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 2 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 Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} @var{rawarr} = odsread (@var{filename}, @var{wsh}, @var{range}) +## . +## +## Proof-of-concept function for reading data from an ODS spreadsheet. +## It works but there are no error checks at all; erroneous function +## results and empty cells are not ignored but returned as 0. +## +## You need to have jOpenDocument-1.2b2.jar in your javaclasspath (get it at +## http://www.jopendocument.org/) and the octave-forge java package installed. +## +## @var{filename} must be a valid ODS spreadsheet file name. If @var{filename} +## does not contain any directory path, the file is saved in the current +## directory. +## +## @var{wsh} is the name or index number of a worksheet in the spreadsheet file. +## +## @var{range} must be a valid spreadsheet range and cannot be empty. +## +## Return args @var{numarr} and @var{txtarr} contain numeric & text data, +## resp.; @var{rawarr} contains the raw data and lim the actual cell ranges +## from where the data originated. +## +## Example: +## +## @example +## [ n, t, r, l ] = odsread ('test1.ods', '2ndSh', 'A1:AF250'); +## @end example +## +## @end deftypefn + +## Author: Philip Nienhuis <prnienhuis at users.sf.net> +## Created: 2009-12-12 + +function [ numarr, txtarr, rawarr, lim ] = odsread (filename, wsh, range) + + ods = odsopen (filename, 0); + + [rawarr, ods] = ods2oct (ods, wsh, range); + + [numarr, txtarr, lim] = parsecell (rawarr, ods.limits); + + odsclose (ods); + +endfunction This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |