created lookup function

This commit is contained in:
Denis Molony 2016-03-06 17:58:14 +11:00
parent d93e676739
commit 4859c81511
2 changed files with 45 additions and 22 deletions

View File

@ -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;
}
}

View File

@ -88,28 +88,8 @@ class VisicalcCell implements Comparable<VisicalcCell>
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);