# Support for Evaluation of Functions in Formulas

Contents
[ Hide Show ]

Aspose.Tasks for .NET API supports evaluating functions defined as formula expression in Extended Attribute. These include calculation of Math, General, Text, and DateTime functions.

## Calculation of Math Expressions

1. Abs( number )
2. Atn( number )
3. Cos( number )
4. Exp( number )
5. Fix( number )
6. Int( number )
7. Log( number )
8. Rnd( number )
9. Sgn( number )
10. Sin( number )
11. Sqr( number )
12. Tan( number )
`````` 1static void EvaluateSine()
2{
3    Project project = CreateTestProjectWithCustomField();
4
5    // Set formula Sin(pi/2)
6    project.ExtendedAttributes[0].Formula = "Sin(3.1415926/2)";
7
8    // Print Calculated value
11}
12
13static Project CreateTestProjectWithCustomField()
14{
15    Project project = new Project();
18
20
21    ExtendedAttribute a = attr.CreateExtendedAttribute();
23    return project;
24}``````

## Calculation of General Functions

The following General functions can be calculated by the API.

1. Choose( index, choice-1, choice-2, … , choice-n|, choice-n)
2. IIf( expr, truepart, falsepart )
3. IsNumeric( expression)
4. IsNull( expression )
5. Switch( expr-1, value-1, expr-2, value-2, … , expr-n,value-n|, expr-n,value-n )
`````` 1static void EvaluateChoose()
2{
3    Project project = CreateTestProjectWithCustomField();
4
5    // Set Formula
6    project.ExtendedAttributes[0].Formula = "Choose(3, \"This is a\", \"right\", \"choice\")";
7
8    // Print extended attribute value
11}
12
13static void EvaluateIsNumeric()
14{
15    string[] numericFormulas = { "IsNumeric('AAA')", "IsNumeric(1)", "IsNumeric(1<0)", "IsNumeric(\"1.1\")", "IsNumeric(Choose((2 + Sgn(2^-3)), 123, \"one two three\"))" };
16
17    Project project = CreateTestProjectWithCustomField();
18
19    foreach (string numericFormula in numericFormulas)
20    {
21        // Set Formula
22        project.ExtendedAttributes[0].Formula = numericFormula;
23
24        // Print extended attribute value
27    }
28}
29
30static void EvaluateSwitch()
31{
32    Project project = CreateTestProjectWithCustomField();
33
34    // Set Formula
35    project.ExtendedAttributes[0].Formula = "Switch( 0 < 1, \"0 is lesser than 1\", 0 > 1, \"0 is greater than 1\")";
36
37    // Print extended attribute value
40}``````

## Calculation of Text Functions

1. Asc( string )
2. Chr( charcode )
3. Format( expression, format, firstdayofweek, firstweekofyear)
4. Instr( start,string1, string2, compare )
5. LCase( string )
6. Left( string, length )
7. Len( string )
8. LTrim( string )
9. Mid( string, start, length )
10. Right( string, length )
11. RTrim( string )
12. Space( number )
13. StrComp( string1, string2, compare )
14. StrConv( string, conversion, LCID )
15. String( number, character )
16. Trim( string )
17. UCase( string )
`````` 1static void EvaluateStrConv()
2{
3    Project project = CreateTestProjectWithCustomField();
5
6    // Set formulas and print extended attribute value
7    project.ExtendedAttributes[0].Formula = "StrConv(\"sTring and sTRINg\",3)";
9    project.ExtendedAttributes[0].Formula = "StrConv(\"sTring and sTRINg\",1)";
11    project.ExtendedAttributes[0].Formula = "StrConv(\"sTring and sTRINg\",2)";
13}
14
15static void EvaluateStringFunction()
16{
17    Project project = CreateTestProjectWithCustomField();
19
20    // Set formulas and print extended attribute value
21    project.ExtendedAttributes[0].Formula = "String(5, 40)";
23    project.ExtendedAttributes[0].Formula = "String(5, \"A\")";
25    project.ExtendedAttributes[0].Formula = "String(-5, \"A\")";
26    // #Error
28}``````

## Calculation of Date/Time Functions

1. CDate( expression )
2. Date ()
3. DateAdd( interval, number, date )
4. DateDiff( interval, date1, date2, firstdayofweek, firstweekofyear )
5. DatePart( interval, date, firstdayofweek, firstweekofyear)
6. DateSerial( year, month, day )
7. DateValue( date)
8. Day( date)
9. Hour( time )
10. IsDate( expression )
11. Minute( time)
12. Month( date)
13. Now ()
14. ProjDateAdd( date, duration, calendar )
15. ProjDateConv( expression, dateformat )
16. ProjDateDiff( date1, date2, calendar )
17. ProjDateSub( date, duration, calendar )
18. ProjDateValue( expression )
19. ProjDurConv( expression, durationunits )
20. ProjDurValue( duration )
21. Second( time )
22. Time ()
23. Timer ()
24. TimeSerial( hour, minute, second)
25. TimeValue( time)
26. Weekday( date, firstdayofweek )
27. Year( date)
`````` 1Project project = CreateTestProject();
3
6
7ExtendedAttribute numberAttribute = numberDefinition.CreateExtendedAttribute();
9
10// Set ProjDateDiff formula and print extended attribute value
11numberDefinition.Formula = "ProjDateDiff(\"03/23/2015\",\"03/18/2015\")";
12Console.WriteLine(numberAttribute.NumericValue);
13numberDefinition.Formula = "ProjDateDiff(\"03/23/2015\",\"03/25/2015\")";
14Console.WriteLine(numberAttribute.NumericValue);
15
18ExtendedAttribute dateAttribute = dateDefinition.CreateExtendedAttribute();
20
23ExtendedAttribute durationAttribute = durationDefinition.CreateExtendedAttribute();
25
28ExtendedAttribute textAttribute = textDefinition.CreateExtendedAttribute();
30
31// Set ProjDateSub formula and print extended attribute value
32dateDefinition.Formula = "ProjDateSub(\"3/19/2015\", \"1d\")";
33Console.WriteLine(dateAttribute.DateValue);
34
35// We can set ProjDurConv formula to duration-valued attribute as well as to text-valued attribute.
36
37// Set ProjDurConv formula to duration-valued extended attribute and print its value.
38durationDefinition.Formula = "ProjDurConv([Duration], pjHours)";
39Console.WriteLine(durationAttribute.DurationValue);
40
41// Set ProjDurConv formula to text-valued extended attribute and print its value.
42textDefinition.Formula = "ProjDurConv([Duration], pjHours)";
43Console.WriteLine(textAttribute.TextValue);
44
45textDefinition.Formula = "ProjDurConv([Duration], pjWeeks)";
46Console.WriteLine(textAttribute.TextValue);
47
48// Set Second formula and print entended attribute value
49numberDefinition.Formula = "Second(\"4/21/2015 2:53:41 AM\")";
50Console.WriteLine(numberAttribute.NumericValue);
51
52// Set Weekday formula and print entended attribute value
53numberDefinition.Formula = "Weekday(\"24/3/2015\", 1)";
54Console.WriteLine(numberAttribute.NumericValue);
55numberDefinition.Formula = "Weekday(\"24/3/2015\", 2)";
56Console.WriteLine(numberAttribute.NumericValue);
57numberDefinition.Formula = "Weekday(\"24/3/2015\", 3)";
58Console.WriteLine(numberAttribute.NumericValue);``````