Skip to content

Instantly share code, notes, and snippets.

@fliedonion
Created December 6, 2016 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fliedonion/629b061d580924251ae59b69bf73c133 to your computer and use it in GitHub Desktop.
Save fliedonion/629b061d580924251ae59b69bf73c133 to your computer and use it in GitHub Desktop.
FarPoint Spread, "INDIRECTCELL" custom function similar with Excel "INDIRECT".
using System.Windows.Forms;
using FarPoint.CalcEngine;
using FarPoint.Win.Spread.Model;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
// this is only behind code.
// put Spread in form designer.
// run and edit A1 cell. values appear in D3, D4 and E3 cells.
public Form1() {
InitializeComponent();
var fs = fpSpread1_Sheet1.Models.Data as ICustomFunctionSupport;
var indirect = fs.GetCustomFunction("indirectCell");
if (indirect == null) {
fs.AddCustomFunction(new InDirectCell(fpSpread1_Sheet1.Models.Data as DefaultSheetDataModel));
}
fpSpread1_Sheet1.SetFormula(3, 2, "indirectCell(ADDRESS(1,1))");
fpSpread1_Sheet1.SetFormula(3, 3, "indirectCell(A1)");
fpSpread1_Sheet1.SetFormula(4, 2, "indirectCell(\"A1\")");
}
}
public class InDirectCell : FunctionInfo {
DefaultSheetDataModel model;
public InDirectCell(DefaultSheetDataModel model)
{
this.model = model;
}
public override bool IsVolatile() {
// need accept string "A1". I don't know why.
return true;
}
public override object Evaluate(object[] args) {
if(args[0] == null) return null;
if (args[0] is CalcReference) {
bool? a1RefType = true;
if (args.Length > 1) {
a1RefType = args[1] as bool?;
if (a1RefType == null) {
a1RefType = true;
}
}
var range = (CalcReference) args[0];
return range.GetValue(range.Row, range.Column);
}
CellExpression expression = model.ParseFormula(0, 0, args[0].ToString()) as CellExpression;
if (expression == null) return null;
return model.GetValue(expression.Row, expression.Column);
}
public override bool AcceptsReference(int i) {
return i == 0;
}
public override int MaxArgs {
get { return 2; }
}
public override int MinArgs {
get { return 1; }
}
public override string Name {
get { return "INDIRECTCELL"; }
}
}
}
@fliedonion
Copy link
Author

R1C1 not test yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment