JsonExcelExpressions examples - pkokki/DocumentCreator GitHub Wiki

Install the JsonExcelExpressions package

Install the JsonExcelExpressions library with NuGet:

dotnet add package JsonExcelExpressions

Examples

Single expression

// Create an evaluator
var evaluator = new ExpressionEvaluator();
// Evaluate an expression
var result = evaluator.Evaluate("1 + 1");
// Print the result
Console.WriteLine(result.Value); // 2

Expression on JSON object

// Get a JSON object
var json = JObject.Parse("{x: 2, y: 3}");
// Create an evaluator
var evaluator = new ExpressionEvaluator();
// Evaluate an expression
var result = evaluator.Evaluate("x * y", json);
// Print the result
Console.WriteLine(result.Value); // 6

Expression with Excel functions

// Get a JSON object
var json = JObject.Parse("{............}");
// Create an evaluator
var evaluator = new ExpressionEvaluator();
// Evaluate an expression
var result = evaluator.Evaluate("...........", json);
// Print the result
Console.WriteLine(result.Value); // abcmn

Multiple expressions

// Get a JSON object
var json = JObject.Parse("{x: 2, y: 3}");
// Create an evaluator
var evaluator = new ExpressionEvaluator();
// Create a set of expressions
var expressions = new string[] { "x + y", "x - y" };
// Evaluate an expression
var results = evaluator.Evaluate(expressions, json);
// Print the result
foreach (var result in results)
    Console.WriteLine(result.Value); // 5, -1