식 사용하기

Aspose.Tasks에서 식 사용하기

Aspose.Tasks for .NET에서는 작업, 자원 및 프로젝트 필드를 수식 내에서 사용할 수 있습니다.
다음과 같은 필드를 사용하여 사용자 지정 계산을 만들 수 있습니다:

이러한 수식은 다음에 적용할 수 있습니다 확장 속성, 고급 프로젝트 맞춤 설정 및 보고를 가능하게 합니다.

수식 계산에서 작업 및 자원 필드 사용하기

다음 예제는 작업 및 자원 필드를 수식 기반 계산에 사용하는 방법을 보여줍니다:

 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}

산술 식 사용하기

필드 간 계산을 수행하기 위해 산술 식을 정의할 수 있습니다:

 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);

작업 숫자 필드 사용하기

수식은 또한 유연한 계산을 위해 숫자 필드를 참조할 수 있습니다:

 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);

참/거짓 값을 사용하는 수식

참/거짓 표현식은 수식 내에서 조건부 논리를 허용합니다:

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);

프로젝트 필드를 사용하는 수식

전체 작업 수 또는 자원 수와 같은 프로젝트 수준의 필드도 수식에 포함될 수 있습니다:

 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}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.