Task Calendars
Contents
[
Hide
Show
]It is possible to associate calendars with particular tasks in Microsoft Project. Aspose.Task supports this functionality.
Tasks with Calendars
The Tsk class exposes the Calendar field used to set or get the calendar associated with a task. This property accepts or returns an Aspose.Tasks.Calendar object.
To create a calendar for a task in Microsoft Project:
- Open a project in Microsoft Project.
- On the Project menu, select Change Working Times, then Create New Calendar.
- To assign the calendar to a task, double-click the task in the Task Entry form.
- Select the Advanced tab.
Setting Task Calendar
Create a standard calendar and create a task. Assign the Calendar to the task.
1// Create project instance
2System::SharedPtr<Project> project = System::MakeObject<Project>();
3
4// Add task
5System::SharedPtr<Task> tsk1 = project->get_RootTask()->get_Children()->Add(u"Task1");
6
7// Create calendar and assign to task
8System::SharedPtr<Aspose::Tasks::Calendar> cal = project->get_Calendars()->Add(u"TaskCal1");
9tsk1->Set<System::SharedPtr<Calendar>>(Tsk::Calendar(), cal);
Getting Task Calendar
Get task calendar by traversing the tasks in a project.
1// The path to the documents directory.
2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
3
4// Create project instance
5System::SharedPtr<Project> prj = System::MakeObject<Project>(dataDir + u"ReadTaskCalendar.mpp");
6
7// Declare ChildTasksCollector class object
8System::SharedPtr<ChildTasksCollector> collector = System::MakeObject<ChildTasksCollector>();
9
10// Use TaskUtils to get all children tasks in RootTask
11TaskUtils::Apply(prj->get_RootTask(), collector, 0);
12
13// Parse all the recursive children
14
15{
16 auto tsk_enumerator = (collector->get_Tasks())->GetEnumerator();
17 decltype(tsk_enumerator->get_Current()) tsk;
18 while (tsk_enumerator->MoveNext() && (tsk = tsk_enumerator->get_Current(), true))
19 {
20 System::SharedPtr<Calendar> tskCal = tsk->Get<System::SharedPtr<Calendar>>(Tsk::Calendar());
21 System::Console::WriteLine(u"Task calendar name: {0}", System::ObjectExt::Box<System::String>(tskCal == nullptr ? u"None" : tskCal->get_Name()));
22 }
23}