Trabajar con expresiones
Trabajar con expresiones en Aspose.Tasks
Aspose.Tasks para .NET le permite usar campos Task, Resource y Project en expresiones de fórmula.
Puede crear cálculos personalizados con campos tales como:
- Work
- Cost
- Number fields
- DateTime
- Boolean values
- Project-level fields (p. ej., Task Count, Resource Count)
Estas fórmulas se pueden aplicar en Extended Attributes, lo que permite una personalización avanzada del proyecto y la generación de informes.
Uso de campos Task y Resource en cálculos de fórmulas
El siguiente ejemplo muestra cómo se pueden usar los campos Task y Resource en cálculos basados en fórmulas:
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}
Uso de expresiones aritméticas
Puede definir expresiones aritméticas para realizar cálculos entre campos:
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);
Uso de campos Number de Task
Las fórmulas también pueden hacer referencia a campos Number para cálculos 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);
Fórmula con Boolean values
Las expresiones Boolean permiten lógica condicional dentro de las fórmulas:
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);
Fórmula con campos Project
Los Project-level fields, como Task Count o Resource Count, también pueden incluirse en las fórmulas:
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}