Utilisation des expressions
Utilisation des expressions dans Aspose.Tasks
Aspose.Tasks for .NET vous permet d’utiliser champs de tâche, de ressource et de projet dans les expressions de formule.
Vous pouvez créer des calculs personnalisés avec des champs tels que :
- Work
- Cost
- Number fields
- DateTime
- Boolean values
- Champs au niveau du projet (par exemple, Task Count, Resource Count)
Ces formules peuvent être appliquées dans Extended Attributes, permettant une personnalisation avancée du projet et des rapports.
Utilisation des champs de tâches et de ressources dans les calculs de formules
L’exemple suivant montre comment les champs de tâche et de ressource peuvent être utilisés dans des calculs basés sur des formules :
1static void Run()
2{
3 Project project = CreateTestProjectWithCustomField();
4 Task task = project.RootTask.Children.GetById(1);
5
6 // Set formula for extended attribute
7 ExtendedAttributeDefinition extendedAttributeDefinition1 = project.ExtendedAttributes[0];
8 extendedAttributeDefinition1.Alias = "Days from finish to deadline";
9 extendedAttributeDefinition1.Formula = "[Deadline] - [Finish]";
10
11 // Set Task Deadline and save project
12 Task task = project.RootTask.Children.GetById(1);
13 task.Set(Tsk.Deadline, new DateTime(2015, 3, 20, 17, 0, 0));
14 project.Save("UsingTasksAndResourceFields_out.mpp", SaveFileFormat.MPP);
15}
16
17// Helper method to create project
18static Project CreateTestProjectWithCustomField()
19{
20 // Create new project instance
21 Project project = new Project("New Project.mpp");
22 project.Set(Prj.StartDate, new DateTime(2015, 3, 6, 8, 0, 0));
23
24 // Add new task with extended attribute
25 Task task = project.RootTask.Children.Add("Task");
26 ExtendedAttributeDefinition extendedAttributeDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Text, ExtendedAttributeTask.Text5, "My Ext Attr");
27 project.ExtendedAttributes.Add(extendedAttributeDefinition);
28 ExtendedAttribute extendedAttribute = extendedAttributeDefinition.CreateExtendedAttribute();
29 task.ExtendedAttributes.Add(extendedAttribute);
30
31 // Add resource and resource assignment
32 Resource resource = project.Resources.Add("Rsc");
33 ResourceAssignment assignment = project.ResourceAssignments.Add(task, resource);
34 return project;
35}
Utilisation d’une expression arithmétique
Vous pouvez définir des expressions arithmétiques pour effectuer des calculs entre les champs :
1Project project = CreateTestProjectWithCustomField();
2
3// Set arithmetic formula for extended attribute
4ExtendedAttributeDefinition attr = project.ExtendedAttributes[0];
5attr.Alias = "Arithmetic Expression";
6attr.Formula = "(1+3*(2+ -5)+8/2)^3";
7
8// Display extended attribute value
9Task task = project.RootTask.Children.GetById(1);
10Console.WriteLine(task.ExtendedAttributes[0].NumericValue);
Utilisation des Task Number Fields
Les formules peuvent également référencer des Number fields pour des calculs flexibles :
1Project project = CreateTestProjectWithCustomField();
2
3// Set formula
4ExtendedAttributeDefinition attr = project.ExtendedAttributes[0];
5attr.Alias = "Task number fields";
6attr.Formula = "([Outline Level] + [Priority] + [% Complete])/2";
7
8Task task = project.RootTask.Children.GetById(1);
9
10// Print extended attribute value before and after updating task percent complete
11Console.WriteLine(task.ExtendedAttributes[0].NumericValue);
12task.Set(Tsk.PercentComplete, 50);
13Console.WriteLine(task.ExtendedAttributes[0].NumericValue);
Formule avec Boolean values
Les expressions Boolean permettent une logique conditionnelle dans les formules :
1Project project = CreateTestProjectWithCustomField();
2
3// Set formula for extended attribute
4project.ExtendedAttributes[0].Formula = "[Critical]-[Marked]+4+[Active]-Not [Active]";
5
6// Print value of extended attribute
7Task task = project.RootTask.Children.GetById(1);
8Console.WriteLine("Formula with boolean values: " + task.ExtendedAttributes[0].TextValue);
Formule avec des champs de projet
Les champs au niveau du projet, tels que le nombre total de tâches ou le nombre de ressources, peuvent également être inclus dans les formules :
1static void Run()
2{
3 Project project = CreateTestProjectWithCustomFieldWithoutResource();
4
5 // Set formula
6 project.ExtendedAttributes[0].Formula = "\"Total tasks: \" & [Task Count] & \" Total resources: \" & [Resource Count]";
7
8 // Print if formula value is computed correctly
9 Task task = project.RootTask.Children.GetById(1);
10 Console.WriteLine("Check Total tasks: 1 Total resources: 0 - {0}", task.ExtendedAttributes[0].TextValue.Equals("Total tasks: 1 Total resources: 0"));
11}
12
13static Project CreateTestProjectWithCustomFieldWithoutResource()
14{
15 Project project = new Project();
16 project.Set(Prj.StartDate, new DateTime(2015, 3, 6, 8, 0, 0));
17 ExtendedAttributeDefinition attr = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Text, ExtendedAttributeTask.Text1, "Custom");
18 project.ExtendedAttributes.Add(attr);
19
20 Task task = project.RootTask.Children.Add("Task");
21 ExtendedAttribute a = attr.CreateExtendedAttribute();
22 task.ExtendedAttributes.Add(a);
23 return project;
24}