changeset 12351:cbcdc5f84079 octave-forge

Move some common code to caller function
author prnienhuis
date Wed, 29 Jan 2014 21:58:26 +0000
parents 7c5b7267eac5
children a2ba66e16516
files main/io/inst/private/__OCT_oct2ods__.m main/io/inst/private/__OCT_oct2spsh__.m
diffstat 2 files changed, 43 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/main/io/inst/private/__OCT_oct2ods__.m	Wed Jan 29 21:56:34 2014 +0000
+++ b/main/io/inst/private/__OCT_oct2ods__.m	Wed Jan 29 21:58:26 2014 +0000
@@ -28,36 +28,11 @@
 ## 2014-01-24 Integrated empty left columns with empty left vals in rawarr
 ##     ''     Same for empty upper rows & empty upper data rows
 ##     ''     Fix temp file name
-
-function [ ods, status ] = __OCT_oct2ods__ (obj, ods, wsh, crange, spsh_opts=0)
-
-  ## A. Analyze data and requested range
-  ## Get size of data to write
-  [nnr, nnc ] = size (obj);
-
-  ## Parse requested cell range
-  [~, nr, nc, tr, lc] = parse_sp_range (crange);
+## 2014-01-29 Move stuff common for all file types to caller or merge function
 
-  ## First check row size
-  if (nnr > nr)
-    ## Truncate obj
-    obj = obj(1:nr, :);
-  elseif (nnr < nr)
-    ## Truncate requested range
-    nr = nnr;
-  endif
-  ## Next, column size
-  if (nnc > nc)
-    ## Truncate obj
-    obj = obj(:, 1:nc);
-  elseif (nnc < nc)
-    ## Truncate requested range
-    nc = nnc;
-  endif
-  br = tr + nr - 1;
-  rc = lc + nc - 1;
+function [ ods, status ] = __OCT_oct2ods__ (obj, ods, wsh, crange, spsh_opts=0, obj_dims)
 
-  ## B. Find out if we write to existing or new sheet
+  ## Find out if we write to existing or new sheet
   new_sh = 0;
   if (isnumeric (wsh))
     if (wsh < 1)
@@ -91,6 +66,7 @@
     idx_s = ods.sheets.shtidx(wsh) ;               ## First position after last sheet
     idx_e = idx_s - 1;
     rawarr = {};
+    lims = [];
   else
     idx_s = ods.sheets.shtidx(wsh);
     idx_e = ods.sheets.shtidx(wsh+1) - 1;
@@ -98,51 +74,9 @@
     [rawarr, ods]  = __OCT_ods2oct__ (ods, wsh, "", struct ("formulas_as_text", 1));
     lims = ods.limits;
   endif
-
-  ## C . If required, adapt current data array size to disjoint new data
-  if (! isempty (rawarr))
-    ## Merge new & current data. Assess where to augment/overwrite current data
-    [onr, onc] = size (rawarr);
-    if (tr < lims(2, 1))
-      ## New data requested above current data. Add rows above current data
-      rawarr = [ cell(lims(2, 1) - tr, onc) ; rawarr];
-      lims(2, 1) = tr;
-    endif
-    if (br > lims(2, 2))
-      ## New data requested below current data. Add rows below current data
-      rawarr = [rawarr ; cell(br - lims(2, 2), onc)];
-      lims (2, 2) = br;
-    endif
-    ## Update number of rawarr rows
-    onr = size (rawarr, 1);
-    if (lc < lims(1, 1))
-      ## New data requested to left of curremnt data; prepend columns
-      rawarr = [cell(onr, lims(1, 1) - lc), rawarr];
-      lims(1, 1) = lc;
-    endif
-    if (rc > lims(1, 2))
-      ## New data to right of current data; append columns
-      rawarr = [rawarr, cell(onr, rc - lims(1, 2))];
-      lims(1, 2) = rc;
-    endif
-    ## Update number of columns
-    onc = size (rawarr, 2);
-
-    ## Copy new data into place
-    objtr = tr - lims(2, 1) + 1;
-    objbr = br - lims(2, 1) + 1;
-    objlc = lc - lims(1, 1) + 1;
-    objrc = rc - lims(1, 1) + 1;
-    rawarr(objtr:objbr, objlc:objrc) = obj; 
-
-  else
-    ## New sheet
-    lims = [lc, rc; tr, br];
-    onc = nc;
-    onr = nr;
-    rawarr = obj;
-  endif
-
+  
+  ## Merge old and new data. Provisionally allow empty new data to wipe old data
+  [rawarr, lims, onr, onc] = __OCT_merge_data__ (rawarr, lims, obj, obj_dims);
 
   ## D. Get default table/row/column styles
   ##    Open file content.xml
@@ -176,6 +110,7 @@
   if (fid < 0)
     error ("oct2ods: unable to write to file %s", tmpfil);
   endif
+
   ## Write data to sheet (actually table:table section in content.xml)
   status  = __OCT__oct2ods_sh__ (fid, rawarr, wsh, lims, onc, onr, ...
             ods.sheets.sh_names{wsh}, styles);
--- a/main/io/inst/private/__OCT_oct2spsh__.m	Wed Jan 29 21:56:34 2014 +0000
+++ b/main/io/inst/private/__OCT_oct2spsh__.m	Wed Jan 29 21:58:26 2014 +0000
@@ -22,19 +22,50 @@
 ## Author: Philip Nienhuis <prnienhuiS at users dot sf dot net>
 ## Created: 2014-01-24
 
-function [ ods, rstatus ] = __OCT_oct2spsh__ (c_arr, ods, wsh, crange, spsh_opts)
+function [ ods, rstatus ] = __OCT_oct2spsh__ (obj, ods, wsh, crange, spsh_opts)
+
+  ## Analyze data and requested range. Get size of data to write
+  [nnr, nnc ] = size (obj);
+
+  ## Parse requested cell range
+  [~, nr, nc, tr, lc] = parse_sp_range (crange);
 
+  ## First check row size
+  if (nnr > nr)
+    ## Truncate obj
+    obj = obj(1:nr, :);
+  elseif (nnr < nr)
+    ## Truncate requested range
+    nr = nnr;
+  endif
+  ## Next, column size
+  if (nnc > nc)
+    ## Truncate obj
+    obj = obj(:, 1:nc);
+  elseif (nnc < nc)
+    ## Truncate requested range
+    nc = nnc;
+  endif
+
+  obj_dims.tr = tr;
+  obj_dims.br = tr + nr - 1;
+  obj_dims.nr = nr;
+  obj_dims.lc = lc;
+  obj_dims.rc = lc + nc - 1;
+  obj_dims.nc = nc;
+
+  ## Invoke file type-dependent functions
   if (strcmpi (ods.app, "ods"))
     ## Write to .ods
-    [ ods, rstatus ] = __OCT_oct2ods__  (c_arr, ods, wsh, crange, spsh_opts);
+    [ ods, rstatus ] = __OCT_oct2ods__  (obj, ods, wsh, crange, spsh_opts, obj_dims);
 
   elseif (strcmpi (ods.app, "xlsx"))
     ## Write to .xlsx
-    [ ods, rstatus ] = __OCT_oct2xlsx__ (c_arr, ods, wsh, crange, spsh_opts);
+    [ ods, rstatus ] = __OCT_oct2xlsx__ (obj, ods, wsh, crange, spsh_opts, obj_dims);
 
   elseif (strcmpi (ods.app, "gnumeric"))
     ## Write to .gnumeric
-    [ ods, rstatus ] = __OCT_oct2gnm__  (c_arr, ods, wsh, crange, spsh_opts);
+    [ ods, rstatus ] = __OCT_oct2gnm__  (obj, ods, wsh, crange, spsh_opts, obj_dims);
 
   else
     error ("writing to file type %s not supported by OCT", xls.app);