diff --git a/src/client/spark_excel/algogen/CellRef.cs b/src/client/spark_excel/algogen/CellRef.cs
new file mode 100644
index 000000000..268b4cf3f
--- /dev/null
+++ b/src/client/spark_excel/algogen/CellRef.cs
@@ -0,0 +1,239 @@
+using System;
+using System.Diagnostics;
+using System.Text;
+
+namespace bts.excel
+{
+ ///
+ /// Defines a reference to some Cell instance, whether it exists or not.
+ ///
+ ///
+ /// Static methods are provided for conversion of column names etc.
+ ///
+ public sealed class CellRef : IComparable, IComparable
+ {
+ ///
+ /// Constructor for the class: CellRef
+ ///
+ /// Name of the sheet where the cell belongs.
+ /// Coordinate (column, row) for the cell in the sheet.
+ public CellRef(string sheetName, (int col1, int row1) coordinate)
+ {
+ SheetName = sheetName;
+ Coordinate = coordinate;
+ LocalAddress = ConvertCoordToAddress(coordinate);
+ }
+
+ ///
+ /// Maximum number of rows, to e compatible with Excel.
+ ///
+ private const int MaxRows = 1048576;
+
+ ///
+ /// Maximum number of columns, to be compatible with Excel.
+ ///
+ private const int MaxColumns = 16384;
+
+ // NOTE: Worksheet and workbook specifications and limits
+ // http://office.microsoft.com/en-gb/excel-help/excel-specifications-and-limits-HP010342495.aspx
+ //
+ // Worksheet size: 1,048,576 rows by 16,384 columns.
+ // * 1048575 is the highest number representable in 20 bits.
+ // * 16383 is the highest number representable in 14 bits. That leaves 2 spare bits in a 16-bit word.
+ // Column width: 255 characters (I'm not sure what the real significance of this is).
+ // Total number of characters that a cell can contain: 32,767 characters
+ // * 32766 is the highest number representable in 15 bits. That leaves 1 spare bit in a 16-bit word.
+ //
+
+ ///
+ /// Name of the sheet where the cell belongs.
+ ///
+ ///
+ /// All cells belong to some Sheet instance, so this name must never be null or empty.
+ ///
+ public string SheetName { get; }
+
+ ///
+ /// Coordinate (column, row) for the cell in the sheet.
+ ///
+ public (int col1, int row1) Coordinate { get; }
+
+ public CellRef Offset(int columnOffset, int rowOffset)
+ {
+ return new CellRef(SheetName, (Column + columnOffset, Row + rowOffset));
+ }
+
+ public int Column => Coordinate.col1;
+
+ public int Row => Coordinate.row1;
+
+ public string LocalAddress { get; internal set; }
+
+ #region Address conversion
+
+ ///
+ /// Converts a column identifier, using letters, to a column number.
+ ///
+ /// Column identifier using letters.
+ /// Column number.
+ /// When column number exceeds maximum.
+ public static int ConvertToColumnNumber(string columnName)
+ {
+ var number = 0;
+ var pow = 1;
+ for (var i = columnName.Length - 1; i >= 0; i--)
+ {
+ number += (columnName[i] - 'A' + 1) * pow;
+ pow *= 26;
+ }
+
+ if (number > MaxColumns)
+ {
+ throw new ArgumentOutOfRangeException(nameof(columnName),
+ "Column number from columnName exceeds maximum");
+ }
+
+ return number;
+ }
+
+ ///
+ /// Converts a column number to a column identifier using letters.
+ ///
+ /// Column number.
+ /// Column identifier using letters.
+ /// When column number is invalid.
+ public static string ConvertToColumnName(int columnNumber)
+ {
+ if (columnNumber < 1 || columnNumber > MaxColumns)
+ {
+ throw new ArgumentOutOfRangeException(nameof(columnNumber),
+ "Invalid column number");
+ }
+
+ var dividend = columnNumber;
+ var columnName = string.Empty;
+ while (dividend > 0)
+ {
+ var modulo = (dividend - 1) % 26;
+ columnName = Convert.ToChar(65 + modulo) + columnName;
+ dividend = (dividend - modulo) / 26;
+ }
+
+ return columnName;
+ }
+
+ ///
+ /// Converts an address, like "A1", to a cell coordinate.
+ ///
+ /// Cell address, like "A1".
+ /// Coordinate of cell on sheet.
+ /// When address argument is null.
+ /// When address is invalid.
+ public static (int col1, int row1) ConvertAddressToCoord(string address)
+ {
+ if (address == null)
+ {
+ throw new ArgumentNullException(nameof(address));
+ }
+
+ address = address.Replace("$", string.Empty);
+ address = address.ToUpper();
+
+ var letters = new StringBuilder();
+ foreach (var c in address)
+ {
+ if (c < 'A' || c > 'Z')
+ {
+ break;
+ }
+ letters.Append(c);
+ }
+
+ if (letters.Length == 0 || letters.Length > 3)
+ {
+ throw new ArgumentException(
+ "Column name requires 1 to 3 letters",
+ nameof(address));
+ }
+
+ var numbers = address.Substring(letters.Length);
+ if (numbers.Length == 0)
+ {
+ throw new ArgumentException("Row number is required", nameof(address));
+ }
+
+ Debug.Assert(letters.Length + numbers.Length == address.Length);
+
+ var row1 = int.Parse(numbers);
+ var col1 = ConvertToColumnNumber(letters.ToString());
+
+ return (col1, row1);
+ }
+
+ ///
+ /// Convert cell coordinate to cell address, like "A1".
+ ///
+ /// Coordinate of cell on sheet.
+ /// Provide absolute address, like "$A$1".
+ /// Cell address, like "A1", or optionally like "$A$1".
+ ///
+ public static string ConvertCoordToAddress((int col1, int row1) coordinate, bool absolute = false)
+ {
+ if (coordinate.row1 < 1 || coordinate.row1 > MaxRows ||
+ coordinate.col1 < 1 || coordinate.col1 > MaxColumns)
+ {
+ throw new ArgumentOutOfRangeException(nameof(coordinate),
+ "Invalid coordinate");
+ }
+
+ var (col1, row1) = coordinate;
+ return absolute
+ ? $"${ConvertToColumnName(col1)}${row1}"
+ : $"{ConvertToColumnName(col1)}{row1}";
+ }
+
+ #endregion
+
+ #region IComparable
+
+ public int CompareTo(CellRef other)
+ {
+ if (other == null) return -1;
+ return string.Compare(ToString(), other.ToString(),
+ StringComparison.Ordinal);
+ }
+
+ public int CompareTo(object obj)
+ {
+ if (obj.GetType() != GetType()) return -1;
+ return CompareTo((CellRef)obj);
+ }
+
+ #endregion
+
+ #region HashCode & Equals
+
+ /// Unique key
+ public override string ToString()
+ {
+ return $"'{SheetName}'!{LocalAddress}";
+ }
+
+ public override int GetHashCode()
+ {
+ return ToString().GetHashCode();
+ }
+
+ public override bool Equals(object obj)
+ {
+ if (obj == null) return false;
+
+ if (obj != null && !(obj is CellRef))
+ throw new ArgumentException("Object must be of type CellRef.");
+
+ return GetHashCode().Equals(obj.GetHashCode());
+ }
+
+ #endregion
+ }
+}
\ No newline at end of file
diff --git a/src/client/spark_excel/algogen/Expression.cs b/src/client/spark_excel/algogen/Expression.cs
deleted file mode 100644
index 932ecd4c3..000000000
--- a/src/client/spark_excel/algogen/Expression.cs
+++ /dev/null
@@ -1,22 +0,0 @@
-using LibWorkbooks.LibDep;
-using LibWorkbooks.Parser;
-
-namespace spark.excel
-{
- internal class Expression : IExpression
- {
- private readonly FormulaParser.ExprContext _context;
- private readonly EvaluationVisitor _visitor;
-
- public Expression(FormulaParser.ExprContext context, EvaluationVisitor visitor)
- {
- _context = context;
- _visitor = visitor;
- }
-
- public object Evaluate()
- {
- return _context.Accept(_visitor);
- }
- }
-}
\ No newline at end of file
diff --git a/src/client/spark_excel/algogen/Generate.cs b/src/client/spark_excel/algogen/Generate.cs
index d6af4f24e..af2c61ec9 100644
--- a/src/client/spark_excel/algogen/Generate.cs
+++ b/src/client/spark_excel/algogen/Generate.cs
@@ -36,16 +36,16 @@ namespace bts.excel
//https://stackoverflow.com/questions/31511610/antlr-source-to-target-language-conversion
public class Generate
{
- static ConcurrentDictionary _cache = new ConcurrentDictionary();
-
[ExcelFunction(IsMacroType = true)]
static public string bts_generate_code_from_sink(string name, [ExcelArgument(AllowReference=true)] object sink)
{
ExcelReference rref = (ExcelReference)sink;
- var form = XlCall.Excel(XlCall.xlfGetCell, 41, rref);
+ var formula = XlCall.Excel(XlCall.xlfGetCell, 41, rref);
+
+ string sheetname = (string)XlCall.Excel(XlCall.xlSheetNm, rref);
//var formabs = XlCall.Excel(XlCall.xlfFormulaConvert, form, ExcelDna.
- var antlrstream = new AntlrInputStream(form.ToString());
+ var antlrstream = new AntlrInputStream(formula.ToString());
var lexer = new ExcelFormulaLexer(antlrstream);
var tokens = new CommonTokenStream(lexer);
var parser = new ExcelFormulaParser(tokens);
@@ -59,7 +59,7 @@ namespace bts.excel
throw new ExcelFormulaException(error_listener.ErrorLocation, error_listener.ErrorMessage);
//AbstractParseTreeVisitor
- ExcelFormulaBaseVisitor visitor = new ExcelFormulaBaseVisitor();
+ GeneratorVisitor visitor = new GeneratorVisitor(rref, sheetname);
visitor.VisitMain(parsetree);
return parsetree.ToString();
diff --git a/src/client/spark_excel/algogen/GeneratorVisitor.cs b/src/client/spark_excel/algogen/GeneratorVisitor.cs
index 02d261fc8..530ea7a1d 100644
--- a/src/client/spark_excel/algogen/GeneratorVisitor.cs
+++ b/src/client/spark_excel/algogen/GeneratorVisitor.cs
@@ -4,28 +4,29 @@ using System.Collections.ObjectModel;
using System.Diagnostics;
using spark_excel.grammar;
+using ExcelDna.Integration;
+using Excel = Microsoft.Office.Interop.Excel;
+
namespace bts.excel
{
internal class GeneratorVisitor : ExcelFormulaBaseVisitor