2016-03-07 04:37:01 +00:00
|
|
|
package com.bytezone.diskbrowser.visicalc;
|
|
|
|
|
2016-03-07 12:16:11 +00:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
// http://www.bricklin.com/history/refcard1.htm
|
|
|
|
// Functions:
|
|
|
|
// @AVERAGE
|
|
|
|
// @NPV
|
|
|
|
// @LOOKUP(v,range)
|
|
|
|
// @NA
|
|
|
|
// @ERROR
|
|
|
|
// @PI
|
|
|
|
// @ABS
|
|
|
|
// @INT
|
|
|
|
// @EXP
|
|
|
|
// @SQRT
|
|
|
|
// @LN
|
|
|
|
// @LOG10
|
|
|
|
// @SIN
|
|
|
|
// @ASIN
|
|
|
|
// @COS
|
|
|
|
// @ACOS
|
|
|
|
// @TAN
|
|
|
|
// @ATAN
|
|
|
|
|
|
|
|
// Unimplemented functions found so far:
|
|
|
|
// @IF
|
|
|
|
// @ISERROR
|
|
|
|
// @OR
|
|
|
|
// @AND
|
|
|
|
|
2016-03-09 10:38:53 +00:00
|
|
|
public abstract class Function implements Value
|
2016-03-07 04:37:01 +00:00
|
|
|
{
|
2016-03-07 12:16:11 +00:00
|
|
|
private static final Pattern functionPattern = Pattern
|
|
|
|
.compile ("\\(([A-B]?[A-Z])([0-9]{1,3})\\.\\.\\.([A-B]?[A-Z])([0-9]{1,3})\\)?");
|
|
|
|
private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)");
|
|
|
|
|
2016-03-09 10:38:53 +00:00
|
|
|
Sheet parent;
|
|
|
|
String text;
|
|
|
|
|
2016-03-07 04:37:01 +00:00
|
|
|
static Function getInstance (Sheet parent, String text)
|
|
|
|
{
|
|
|
|
if (text.startsWith ("@LOOKUP("))
|
|
|
|
return new Lookup (parent, text);
|
|
|
|
|
|
|
|
if (text.startsWith ("@COUNT("))
|
|
|
|
return new Count (parent, text);
|
|
|
|
|
|
|
|
if (text.startsWith ("@MIN("))
|
|
|
|
return new Min (parent, text);
|
|
|
|
|
|
|
|
if (text.startsWith ("@MAX("))
|
|
|
|
return new Max (parent, text);
|
|
|
|
|
|
|
|
if (text.startsWith ("@SUM("))
|
|
|
|
return new Sum (parent, text);
|
|
|
|
|
2016-03-09 10:38:53 +00:00
|
|
|
if (text.startsWith ("@IF("))
|
|
|
|
return new If (parent, text);
|
|
|
|
|
2016-03-07 12:16:11 +00:00
|
|
|
System.out.printf ("Unknown function: %s%n", text);
|
2016-03-09 10:38:53 +00:00
|
|
|
return new Error (parent, "@ERROR()");
|
2016-03-07 04:37:01 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 10:38:53 +00:00
|
|
|
public Function (Sheet parent, String text)
|
|
|
|
{
|
|
|
|
this.parent = parent;
|
|
|
|
this.text = text;
|
|
|
|
}
|
2016-03-07 12:16:11 +00:00
|
|
|
|
|
|
|
Range getRange (String text)
|
|
|
|
{
|
|
|
|
Range range = null;
|
|
|
|
Matcher m = functionPattern.matcher (text);
|
|
|
|
while (m.find ())
|
|
|
|
{
|
|
|
|
Address fromAddress = new Address (m.group (1), m.group (2));
|
|
|
|
Address toAddress = new Address (m.group (3), m.group (4));
|
|
|
|
range = new Range (fromAddress, toAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (range != null)
|
|
|
|
return range;
|
|
|
|
|
|
|
|
m = addressList.matcher (text);
|
|
|
|
while (m.find ())
|
|
|
|
{
|
|
|
|
String[] cells = m.group (1).split (",");
|
|
|
|
range = new Range (cells);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (range != null)
|
|
|
|
return range;
|
|
|
|
|
|
|
|
int pos = text.indexOf ("...");
|
|
|
|
if (pos > 0)
|
|
|
|
{
|
|
|
|
String from = text.substring (0, pos);
|
|
|
|
String to = text.substring (pos + 3);
|
|
|
|
Address fromAddress = new Address (from);
|
|
|
|
Address toAddress = new Address (to);
|
|
|
|
range = new Range (fromAddress, toAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (range != null)
|
|
|
|
return range;
|
|
|
|
System.out.println ("null range : " + text);
|
|
|
|
|
|
|
|
return range;
|
|
|
|
}
|
2016-03-09 10:38:53 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString ()
|
|
|
|
{
|
|
|
|
return String.format ("Function: %s", text);
|
|
|
|
}
|
2016-03-07 04:37:01 +00:00
|
|
|
}
|