|
2 | 2 | title: Support Evaluation Functions in Aspose.Tasks Formulas |
3 | 3 | linktitle: Support Evaluation Functions in Aspose.Tasks Formulas |
4 | 4 | second_title: Aspose.Tasks Java API |
5 | | -description: |
| 5 | +description: Learn how to support evaluation of MS Project functions in Aspose.Tasks formulas using Java. Boost your productivity with Aspose.Tasks. |
6 | 6 | type: docs |
7 | 7 | weight: 10 |
8 | 8 | url: /java/formulas/evaluation-functions/ |
9 | 9 | --- |
10 | 10 |
|
11 | | -## Complete Source Code |
| 11 | +## Introduction |
| 12 | +Aspose.Tasks for Java is a powerful library that enables developers to manipulate Microsoft Project files programmatically. One of its key features is the ability to support evaluation of MS Project functions within Aspose.Tasks formulas. This capability allows users to perform complex calculations and analysis directly within their Java applications. |
| 13 | +## Prerequisites |
| 14 | +Before getting started with integrating MS Project functions into Aspose.Tasks formulas, ensure you have the following: |
| 15 | +1. Java Development Environment: Make sure you have Java installed on your system along with a compatible IDE for Java development such as IntelliJ IDEA or Eclipse. |
| 16 | +2. Aspose.Tasks for Java Library: Download and include the Aspose.Tasks for Java library in your Java project. You can download it from the [Aspose.Tasks for Java download page](https://releases.aspose.com/tasks/java/). |
| 17 | +## Import Packages |
| 18 | +To begin, import the necessary packages in your Java class to utilize Aspose.Tasks functionalities: |
12 | 19 | ```java |
13 | | -/* |
14 | | - * Copyright 2001-2022 Aspose Pty Ltd. All Rights Reserved. |
15 | | - * |
16 | | - * This file is part of Aspose.Tasks. The source code in this file |
17 | | - * is only intended as a supplement to the documentation, and is provided |
18 | | - * "as is", without warranty of any kind, either expressed or implied. |
19 | | - */ |
20 | | - |
21 | | - |
22 | | - |
23 | 20 | import com.aspose.tasks.*; |
| 21 | +``` |
24 | 22 |
|
25 | | -public class SupportEvaluationFunctions { |
26 | | - public static void main(String[] args) { |
27 | | - CalculationOfMathExpressions(); |
28 | | - |
29 | | - EvaluateChoose(); |
30 | | - EvaluateIsNumeric(); |
31 | | - EvaluateSwitch(); |
32 | | - |
33 | | - CalculationOfTextFunctions(); |
34 | | - CalculationOfDateTimeFunctions(); |
35 | | - |
36 | | - //Display result of conversion. |
37 | | - System.out.println("Process completed Successfully"); |
38 | | - } |
39 | | - |
40 | | - private static Project CreateTestProjectWithCustomField() { |
41 | | - Project project = new Project(); |
42 | | - ExtendedAttributeDefinition attr = ExtendedAttributeDefinition.createTaskDefinition(CustomFieldType.Number, ExtendedAttributeTask.Number1, "Sine"); |
43 | | - project.getExtendedAttributes().add(attr); |
44 | | - |
45 | | - Task task = project.getRootTask().getChildren().add("Task"); |
46 | | - |
47 | | - ExtendedAttribute a = attr.createExtendedAttribute(); |
48 | | - task.getExtendedAttributes().add(a); |
49 | | - return project; |
50 | | - } |
51 | | - |
52 | | - public static void CalculationOfMathExpressions() { |
53 | | - //ExStart: CalculationOfMathExpressions |
54 | | - Project project = CreateTestProjectWithCustomField(); |
55 | | - |
56 | | - // Set formula Sin(pi/2) |
57 | | - project.getExtendedAttributes().get(0).setFormula("Sin(3.1415926/2)"); |
58 | | - |
59 | | - // Print Calculated value |
60 | | - Task task = project.getRootTask().getChildren().getById(1); |
61 | | - System.out.println("Sin(pi/2): " + task.getExtendedAttributes().get(0).getNumericValue()); |
62 | | - //ExEnd: CalculationOfMathExpressions |
63 | | - } |
64 | | - |
65 | | - //ExStart: CalculationOfGeneralFunctions |
66 | | - public static void EvaluateChoose() { |
67 | | - Project project = CreateTestProjectWithCustomField(); |
68 | | - |
69 | | - // Set Formula |
70 | | - project.getExtendedAttributes().get(0).setFormula("Choose(3, \"This is a\", \"right\", \"choice\")"); |
71 | | - |
72 | | - // Print extended attribute value |
73 | | - Task task = project.getRootTask().getChildren().getById(1); |
74 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
75 | | - } |
76 | | - |
77 | | - public static void EvaluateIsNumeric() { |
78 | | - String[] numericFormulas = { |
79 | | - "IsNumeric('AAA')", |
80 | | - "IsNUmeric(1)", |
81 | | - "IsNumeric(1<0)", |
82 | | - "IsNumeric(\"1.1\")", |
83 | | - "IsNumeric(Choose((2 + Sgn(2^-3)), 123, \"one two three\"))" |
84 | | - }; |
85 | | - |
86 | | - Project project = CreateTestProjectWithCustomField(); |
87 | | - |
88 | | - for (String numericFormula : numericFormulas) { |
89 | | - // Set Formula |
90 | | - project.getExtendedAttributes().get(0).setFormula(numericFormula); |
91 | | - |
92 | | - // Print extended attribute value |
93 | | - Task task = project.getRootTask().getChildren().getById(1); |
94 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
95 | | - } |
96 | | - } |
97 | | - |
98 | | - public static void EvaluateSwitch() { |
99 | | - Project project = CreateTestProjectWithCustomField(); |
100 | | - |
101 | | - // Set Formula |
102 | | - project.getExtendedAttributes().get(0).setFormula("Switch( 0 < 1, \"0 is lesser than 1\", 0 > 1, \"0 is greater than 1\")"); |
103 | | - |
104 | | - // Print extended attribute value |
105 | | - Task task = project.getRootTask().getChildren().getById(1); |
106 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
107 | | - } |
108 | | - //ExEnd: CalculationOfGeneralFunctions |
109 | | - |
110 | | - public static void CalculationOfTextFunctions() { |
111 | | - //ExStart: CalculationOfTextFunctions |
112 | | - Project project = CreateTestProjectWithCustomField(); |
113 | | - Task task = project.getRootTask().getChildren().getById(1); |
114 | | - |
115 | | - // EvaluateStrConv |
116 | | - // Set formulas and print extended attribute value |
117 | | - project.getExtendedAttributes().get(0).setFormula("StrConv(\"sTring and sTRINg\",3)"); |
118 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
119 | | - |
120 | | - project.getExtendedAttributes().get(0).setFormula("StrConv(\"sTring and sTRINg\",1)"); |
121 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
122 | | - |
123 | | - project.getExtendedAttributes().get(0).setFormula("StrConv(\"sTring and sTRINg\",2)"); |
124 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
125 | | - |
126 | | - // EvaluateStringFunction |
127 | | - // Set formulas and print extended attribute value |
128 | | - project.getExtendedAttributes().get(0).setFormula("String(5, 40)"); |
129 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
130 | | - |
131 | | - project.getExtendedAttributes().get(0).setFormula("String(5, \"A\")"); |
132 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
133 | | - |
134 | | - project.getExtendedAttributes().get(0).setFormula("String(-5, \"A\")"); |
135 | | - // #Error |
136 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
137 | | - //ExEnd: CalculationOfTextFunctions |
138 | | - } |
139 | | - |
140 | | - public static void CalculationOfDateTimeFunctions() { |
141 | | - //ExStart: CalculationOfDateTimeFunctions |
142 | | - Project project = CreateTestProjectWithCustomField(); |
143 | | - Task task = project.getRootTask().getChildren().getById(1); |
144 | | - |
145 | | - ExtendedAttributeDefinition numberDefinition = ExtendedAttributeDefinition.createTaskDefinition(ExtendedAttributeTask.Number1, null); |
146 | | - project.getExtendedAttributes().add(numberDefinition); |
147 | | - |
148 | | - ExtendedAttribute numberAttribute = numberDefinition.createExtendedAttribute(); |
149 | | - task.getExtendedAttributes().add(numberAttribute); |
150 | | - |
151 | | - // Set ProjDateDiff formula and print extended attribute value |
152 | | - numberDefinition.setFormula("ProjDateDiff(\"03/23/2015\",\"03/18/2015\")"); |
153 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
154 | | - |
155 | | - numberDefinition.setFormula("ProjDateDiff(\"03/23/2015\",\"03/25/2015\")"); |
156 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
157 | | - |
158 | | - ExtendedAttributeDefinition dateDefinition = ExtendedAttributeDefinition.createTaskDefinition(ExtendedAttributeTask.Date1, null); |
159 | | - project.getExtendedAttributes().add(dateDefinition); |
160 | | - ExtendedAttribute dateAttribute = dateDefinition.createExtendedAttribute(); |
161 | | - task.getExtendedAttributes().add(dateAttribute); |
162 | | - |
163 | | - ExtendedAttributeDefinition durationDefinition = ExtendedAttributeDefinition.createTaskDefinition(ExtendedAttributeTask.Duration4, "Custom duration field"); |
164 | | - project.getExtendedAttributes().add(durationDefinition); |
165 | | - ExtendedAttribute durationAttribute = durationDefinition.createExtendedAttribute(); |
166 | | - task.getExtendedAttributes().add(durationAttribute); |
167 | | - |
168 | | - ExtendedAttributeDefinition textDefinition = ExtendedAttributeDefinition.createTaskDefinition(ExtendedAttributeTask.Text5, "Custom text field"); |
169 | | - project.getExtendedAttributes().add(textDefinition); |
170 | | - ExtendedAttribute textAttribute = textDefinition.createExtendedAttribute(); |
171 | | - task.getExtendedAttributes().add(textAttribute); |
172 | | - |
173 | | - // Set ProjDateSub formula and print extended attribute value |
174 | | - dateDefinition.setFormula("ProjDateSub(\"3/19/2015\", \"1d\")"); |
175 | | - System.out.println(dateAttribute.getDateValue()); |
176 | | - |
177 | | - // We can set ProjDurConv formula to duration-valued attribute as well as to text-valued attribute. |
178 | | - |
179 | | - // Set ProjDurConv formula to duration-valued extended attribute and print its value. |
180 | | - durationDefinition.setFormula("ProjDurConv([Duration], pjHours)"); |
181 | | - System.out.println(durationAttribute.getDurationValue()); |
182 | | - |
183 | | - // Set ProjDurConv formula to text-valued extended attribute and print its value. |
184 | | - textDefinition.setFormula("ProjDurConv([Duration], pjHours)"); |
185 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
186 | | - |
187 | | - textDefinition.setFormula("ProjDurConv([Duration], pjWeeks)"); |
188 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
189 | | - |
190 | | - // Set Second formula and print extended attribute value |
191 | | - numberDefinition.setFormula("Second(\"4/21/2015 2:53:41 AM\")"); |
192 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
193 | | - |
194 | | - // Set Weekday formula and print extended attribute value |
195 | | - numberDefinition.setFormula("Weekday(\"24/3/2015\", 1)"); |
196 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
197 | | - |
198 | | - numberDefinition.setFormula("Weekday(\"24/3/2015\", 2)"); |
199 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
200 | | - |
201 | | - numberDefinition.setFormula("Weekday(\"24/3/2015\", 3)"); |
202 | | - System.out.println(task.getExtendedAttributes().get(0).getNumericValue()); |
203 | | - //ExEnd: CalculationOfDateTimeFunctions |
204 | | - } |
205 | | -} |
206 | | - |
207 | | - |
208 | | - |
209 | | - |
210 | | - |
| 23 | +## Step 1: Create a New Project Object |
| 24 | +First, create a new `Project` object to work with: |
| 25 | +```java |
| 26 | +Project project = new Project(); |
| 27 | +``` |
| 28 | +This initializes a new empty project. |
| 29 | +## Step 2: Define an Extended Attribute for Tasks |
| 30 | +Next, define an extended attribute for tasks. This attribute will hold custom data associated with tasks: |
| 31 | +```java |
| 32 | +ExtendedAttributeDefinition attr = ExtendedAttributeDefinition.createTaskDefinition(CustomFieldType.Number, ExtendedAttributeTask.Number1, "Sine"); |
| 33 | +``` |
| 34 | +Here, we create an extended attribute of type `Number` with the name "Sine" for tasks. |
| 35 | +## Step 3: Add the Extended Attribute to the Project |
| 36 | +Add the extended attribute definition to the project's list of extended attributes: |
| 37 | +```java |
| 38 | +project.getExtendedAttributes().add(attr); |
| 39 | +``` |
| 40 | +This adds the custom attribute to the project. |
| 41 | +## Step 4: Create a New Task |
| 42 | +Now, let's create a new task within the project: |
| 43 | +```java |
| 44 | +Task task = project.getRootTask().getChildren().add("Task"); |
| 45 | +``` |
| 46 | +This adds a new task named "Task" to the project. |
| 47 | +## Step 5: Associate the Extended Attribute with the Task |
| 48 | +Associate the extended attribute created earlier with the task: |
| 49 | +```java |
| 50 | +ExtendedAttribute a = attr.createExtendedAttribute(); |
| 51 | +task.getExtendedAttributes().add(a); |
211 | 52 | ``` |
| 53 | +This associates the "Sine" extended attribute with the task. |
| 54 | + |
| 55 | +## Conclusion |
| 56 | +In conclusion, integrating MS Project functions into Aspose.Tasks formulas in Java is a straightforward process. By following the provided steps, you can effectively utilize the powerful capabilities of Aspose.Tasks for Java to manipulate and analyze Microsoft Project files programmatically. |
| 57 | +## FAQ's |
| 58 | +### Q: Can Aspose.Tasks for Java handle complex MS Project formulas? |
| 59 | +A: Yes, Aspose.Tasks for Java supports evaluation of a wide range of MS Project functions, allowing for complex calculations within Java applications. |
| 60 | +### Q: Is Aspose.Tasks for Java compatible with different versions of Microsoft Project files? |
| 61 | +A: Yes, Aspose.Tasks for Java supports various versions of Microsoft Project files, including MPP, MPT, and XML formats. |
| 62 | +### Q: Can I try Aspose.Tasks for Java before purchasing? |
| 63 | +A: Yes, you can download a free trial version of Aspose.Tasks for Java from the website [here](https://purchase.aspose.com/buy). |
| 64 | +### Q: How can I get support for Aspose.Tasks for Java? |
| 65 | +A: You can get support from the Aspose.Tasks community forum [here](https://forum.aspose.com/c/tasks/15). |
| 66 | +### Q: Is there a temporary license available for Aspose.Tasks for Java? |
| 67 | +A: Yes, you can obtain a temporary license for testing purposes from the Aspose website [here](https://purchase.aspose.com/temporary-license/). |
0 commit comments