수식에서 함수 평가 지원
Contents
[
Hide
Show
]Aspose.Tasks for .NET API는 다음을 지원합니다 수식으로 정의된 함수 평가 프로젝트의 확장 속성 내에서.
이를 통해 개발자는 Microsoft Project에서 제공되는 것과 유사한 계산을 수행할 수 있으며, 사용자 지정 논리에 따라 달라지는 동적 필드를 구현할 수 있습니다.
이 API는 다음 범주의 함수에 대한 평가를 제공합니다:
- 수학 함수
- 일반 함수
- 문자열 함수
- 날짜/시간 함수
이 기능은 다음과 같은 경우에 특히 유용합니다:
- 프로젝트 계산 자동화,
- 사용자 지정 업무 규칙 구현,
- MS Project와 Aspose.Tasks 간의 수식 호환성 보장.
수학식 계산
다음의 수학 함수들이 지원됩니다:
- Abs( number )
- Atn( number )
- Cos( number )
- Exp( number )
- Fix( number )
- Int( number )
- Log( number )
- Rnd( number )
- Sgn( number )
- Sin( number )
- Sqr( number )
- 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
9 Task task = project.RootTask.Children.GetById(1);
10 Console.WriteLine("Sin(pi/2): {0}", task.ExtendedAttributes[0].NumericValue);
11}
12
13static Project CreateTestProjectWithCustomField()
14{
15 Project project = new Project();
16 ExtendedAttributeDefinition attr = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Number, ExtendedAttributeTask.Number1, "Sine");
17 project.ExtendedAttributes.Add(attr);
18
19 Task task = project.RootTask.Children.Add("Task");
20
21 ExtendedAttribute a = attr.CreateExtendedAttribute();
22 task.ExtendedAttributes.Add(a);
23 return project;
24}일반 함수 계산
다음 함수들은 논리 및 조건 평가를 제공합니다:
Choose( index, choice-1, choice-2, ... , choice-n )– 목록에서 값을 선택합니다.IIf( expr, truepart, falsepart )– 조건에 따라 두 값 중 하나를 반환합니다.IsNumeric( expression )– 표현식이 숫자인지 확인합니다.IsNull( expression )– 표현식이 null인지 확인합니다.Switch( expr-1, value-1, expr-2, value-2, ... )– 표현식을 순서대로 평가하여 첫 번째로 일치하는 값을 반환합니다.
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
9 Task task = project.RootTask.Children.GetById(1);
10 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
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
25 Task task = project.RootTask.Children.GetById(1);
26 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
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
38 Task task = project.RootTask.Children.GetById(1);
39 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
40}문자열 함수 계산
문자열 조작 함수는 서식 지정 및 비교를 허용합니다:
Asc( string )– 첫 번째 문자에 대한 ASCII 코드를 반환합니다.Chr( charcode )– ASCII 코드에 해당하는 문자를 반환합니다.Format( expression, format, firstdayofweek, firstweekofyear )– 숫자/날짜를 서식화합니다.Instr( start, string1, string2, compare )– 한 문자열 내에서 다른 문자열의 위치를 찾습니다.LCase( string )– 소문자로 변환합니다.Left( string, length )– 왼쪽에서부터 문자열을 추출합니다.Len( string )– 문자열 길이를 반환합니다.LTrim( string )– 앞쪽 공백을 제거합니다.Mid( string, start, length )– 부분 문자열을 추출합니다.Right( string, length )– 오른쪽에서부터 문자열을 추출합니다.RTrim( string )– 뒤쪽 공백을 제거합니다.Space( number )– 공백이 포함된 문자열을 반환합니다.StrComp( string1, string2, compare )– 두 문자열을 비교합니다.StrConv( string, conversion, LCID )– 로케일에 따라 문자열을 변환합니다.String( number, character )– 문자열을 지정된 횟수만큼 반복하여 반환합니다n회.Trim( string )– 앞뒤 공백을 제거합니다.UCase( string )– 대문자로 변환합니다.
1static void EvaluateStrConv()
2{
3 Project project = CreateTestProjectWithCustomField();
4 Task task = project.RootTask.Children.GetById(1);
5
6 // Set formulas and print extended attribute value
7 project.ExtendedAttributes[0].Formula = "StrConv(\"sTring and sTRINg\",3)";
8 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
9 project.ExtendedAttributes[0].Formula = "StrConv(\"sTring and sTRINg\",1)";
10 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
11 project.ExtendedAttributes[0].Formula = "StrConv(\"sTring and sTRINg\",2)";
12 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
13}
14
15static void EvaluateStringFunction()
16{
17 Project project = CreateTestProjectWithCustomField();
18 Task task = project.RootTask.Children.GetById(1);
19
20 // Set formulas and print extended attribute value
21 project.ExtendedAttributes[0].Formula = "String(5, 40)";
22 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
23 project.ExtendedAttributes[0].Formula = "String(5, \"A\")";
24 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
25 project.ExtendedAttributes[0].Formula = "String(-5, \"A\")";
26 // #Error
27 Console.WriteLine(task.ExtendedAttributes[0].TextValue);
28}날짜/시간 함수 계산
Aspose.Tasks는 표준 VB 스타일 날짜 함수와 MS Project에 특화된 스케줄링 함수를 모두 지원합니다:
CDate( expression )– 표현식을 날짜로 변환합니다.Date()– 현재 시스템 날짜를 반환합니다.DateAdd( interval, number, date )– 날짜에 간격을 추가합니다.DateDiff( interval, date1, date2 )– 두 날짜 간의 차이를 구합니다.DatePart( interval, date )– 날짜의 일부(연, 월, 일 등)를 추출합니다.DateSerial( year, month, day )– 구성요소로부터 날짜를 생성합니다.DateValue( date )– 문자열을 날짜로 변환합니다.Day( date )– 월의 일자.Hour( time )– 시(시간)를 추출합니다.IsDate( expression )– 표현식이 유효한 날짜인지 확인합니다.Minute( time )– 분을 추출합니다.Month( date )– 월을 추출합니다.Now()– 현재 날짜와 시간을 반환합니다.ProjDateAdd( date, duration, calendar )– 프로젝트 캘린더를 고려하여 기간을 추가합니다.ProjDateConv( expression, dateformat )– 날짜를 프로젝트 형식으로 변환합니다.ProjDateDiff( date1, date2, calendar )– 프로젝트 캘린더를 고려한 차이를 구합니다.ProjDateSub( date, duration, calendar )– 기간을 빼줍니다.ProjDateValue( expression )– 날짜 표현식을 평가합니다.ProjDurConv( expression, durationunits )– 단위 간 기간을 변환합니다.ProjDurValue( duration )– 기간 표현식을 평가합니다.Second( time )– 초를 추출합니다.Time()– 현재 시스템 시간을 반환합니다.Timer()– 자정 이후 경과된 초 수.TimeSerial( hour, minute, second )– 시간 값을 생성합니다.TimeValue( time )– 시간을 변환합니다.Weekday( date, firstdayofweek )– 요일을 반환합니다.Year( date )– 연도를 추출합니다.
1Project project = CreateTestProject();
2Task task = project.RootTask.Children.GetById(1);
3
4ExtendedAttributeDefinition numberDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Number1, null);
5project.ExtendedAttributes.Add(numberDefinition);
6
7ExtendedAttribute numberAttribute = numberDefinition.CreateExtendedAttribute();
8task.ExtendedAttributes.Add(numberAttribute);
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
16ExtendedAttributeDefinition dateDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Date1, null);
17project.ExtendedAttributes.Add(dateDefinition);
18ExtendedAttribute dateAttribute = dateDefinition.CreateExtendedAttribute();
19task.ExtendedAttributes.Add(dateAttribute);
20
21ExtendedAttributeDefinition durationDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Duration4, "Custom duration field");
22project.ExtendedAttributes.Add(durationDefinition);
23ExtendedAttribute durationAttribute = durationDefinition.CreateExtendedAttribute();
24task.ExtendedAttributes.Add(durationAttribute);
25
26ExtendedAttributeDefinition textDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Text5, "Custom text field");
27project.ExtendedAttributes.Add(textDefinition);
28ExtendedAttribute textAttribute = textDefinition.CreateExtendedAttribute();
29task.ExtendedAttributes.Add(textAttribute);
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);실무 예제
다음은 수식을 사용하는 실제 시나리오입니다:
1 var project = new Project();
2 var task = project.RootTask.Children.Add("Task 1");
3
4 // Create an extended attribute with a custom formula
5 ExtendedAttributeDefinition attrDef = ExtendedAttributeDefinition.CreateTaskDefinition(
6 ExtendedAttributeTask.Text1, "Custom Formula");
7 attrDef.Formula = "IIf([Duration] > 480, \"Long Task\", \"Short Task\")";
8 project.ExtendedAttributes.Add(attrDef);
9
10 var attr = attrDef.CreateExtendedAttribute();
11 task.ExtendedAttributes.Add(attr);
12
13 // The formula is automatically evaluated by Aspose.Tasks
14 Console.WriteLine(task.Get(Tsk.Name) + " -> " + task.ExtendedAttributes[0].TextValue);수식 지원의 주요 이점
- MS Project 수식과의 완전한 호환성.
- 자동화 기능 동적 필드 및 조건부 논리.
- 내장 지원 프로젝트 캘린더 인식 함수 (ProjDateAdd, ProjDateDiff 등).
- 사용자 지정 C# 코드를 작성하지 않고 복잡한 계산을 단순화합니다.