Tasks and Calendars
In Microsoft Project, it is possible to associate specific calendars with individual tasks. Aspose.Tasks for .NET provides the same capability through the Tsk.Calendar property, which links a task to a particular calendar.
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 the project file.
- On the Project menu, select Change Working Times → Create New Calendar.
- To assign the calendar, double-click a task in the Task Entry form and select the calendar in the task properties.
Example: Setting a Task Calendar
The following code shows how to create a calendar, add a task, and assign the calendar to it:
1Project project = new Project();
2
3// Add task
4Task task = project.RootTask.Children.Add("Task1");
5
6// Create calendar and assign to task
7Calendar cal = project.Calendars.Add("TaskCal1");
8task.Set(Tsk.Calendar, cal);Example: Reading a Task Calendar
You can also traverse tasks in a project and read their associated calendars:
1Project project = new Project("New Project.mpp");
2
3// Declare ChildTasksCollector class object
4ChildTasksCollector collector = new ChildTasksCollector();
5
6// Use TaskUtils to get all children tasks in RootTask
7TaskUtils.Apply(project.RootTask, collector, 0);
8
9// Parse all the recursive children
10foreach (Task task in collector.Tasks)
11{
12 Calendar cal = task.Get(Tsk.Calendar);
13 Console.WriteLine("Task calendar name: {0}", cal == null ? "None" : cal.Name);
14}Key Notes
- Task calendars override the project calendar for scheduling that specific task.
- If a task does not have its own calendar, it inherits the default project calendar.
- Calendars can include working times, exceptions, and holidays, providing fine-grained control over task scheduling.
- Works consistently across both MPP and XML formats.
FAQ
Q: Can a task have more than one calendar?
- No. Each task can only be assigned a single calendar.
Q: What happens if a task calendar conflicts with the project calendar?
- The task calendar takes precedence for that task.
Q: How can I remove a calendar from a task?
- Set the task’s
Calendarproperty tonullto make it use the project calendar again.
Q: Are resource calendars considered when assigning task calendars?
- Yes. The final schedule is calculated by combining task calendars, resource calendars, and the project calendar.