From 4859c81511dec9fb60db9143f5e8fdd7fc6062e2 Mon Sep 17 00:00:00 2001 From: Denis Molony Date: Sun, 6 Mar 2016 17:58:14 +1100 Subject: [PATCH] created lookup function --- .../bytezone/diskbrowser/visicalc/Lookup.java | 43 +++++++++++++++++++ .../diskbrowser/visicalc/VisicalcCell.java | 24 +---------- 2 files changed, 45 insertions(+), 22 deletions(-) create mode 100644 src/com/bytezone/diskbrowser/visicalc/Lookup.java diff --git a/src/com/bytezone/diskbrowser/visicalc/Lookup.java b/src/com/bytezone/diskbrowser/visicalc/Lookup.java new file mode 100644 index 0000000..f75ee1d --- /dev/null +++ b/src/com/bytezone/diskbrowser/visicalc/Lookup.java @@ -0,0 +1,43 @@ +package com.bytezone.diskbrowser.visicalc; + +public class Lookup +{ + Range range; + VisicalcCell source; + VisicalcSpreadsheet parent; + boolean hasValue; + + public Lookup (VisicalcSpreadsheet parent, String text) + { + this.parent = parent; + + int pos = text.indexOf (','); + String sourceText = text.substring (8, pos); + String rangeText = text.substring (pos + 1, text.length () - 1); + + source = parent.getCell (new Address (sourceText)); + range = parent.getRange (rangeText); + } + + // need a mechanism to return NA and ERROR + public boolean hasValue () + { + return hasValue; + } + + public double getValue () + { + Address target = null; + for (Address address : range) + { + if (parent.getCell (address).getValue () > source.getValue ()) + break; + target = address; + } + + if (target != null) + return parent.getCell (target.nextColumn ()).getValue (); + + return 0; + } +} \ No newline at end of file diff --git a/src/com/bytezone/diskbrowser/visicalc/VisicalcCell.java b/src/com/bytezone/diskbrowser/visicalc/VisicalcCell.java index bd7aef5..babfecc 100644 --- a/src/com/bytezone/diskbrowser/visicalc/VisicalcCell.java +++ b/src/com/bytezone/diskbrowser/visicalc/VisicalcCell.java @@ -88,28 +88,8 @@ class VisicalcCell implements Comparable if (formula.startsWith ("@LOOKUP(")) { - int pos = formula.indexOf (','); - String sourceText = formula.substring (8, pos); - String rangeText = formula.substring (pos + 1, formula.length () - 1); - VisicalcCell source = parent.getCell (new Address (sourceText)); - Range range = parent.getRange (rangeText); - // System.out.printf ("lookup[%s][%s]%n", source, range); - - Address target = null; - for (Address address : range) - { - // System.out.printf ("%s %s%n", source, parent.getCell (address)); - if (parent.getCell (address).value > source.value) - break; - target = address; - } - if (target != null) - { - value = parent.getCell (target.nextColumn ()).value; - valid = true; - return value; - } - return result; + Lookup lookup = new Lookup (parent, formula); + return lookup.getValue (); } Matcher m = cellContents.matcher (formula);