Creating Calendars

Creating a Calendar

Calendars and other information is used by Microsoft Project to build project schedules. With Aspose.Tasks for C++, adding a calendar to a project is easy. The Calendar class constructors allow you to assign an ID or a calendar name to the calendar, or use the default constructor. The options are described below.

There are three ways of creating a calendar in Aspose.Tasks. The Calendar class exposes the following three constructors:

The following code creates three different calendars, one using each constructor. Open the output file in Microsoft Project and, on the Project menu, select Project Information to access the calendars.

Project information showing the calendars info

update resource calendar in Microsoft Project

 1// Create a project instance
 2System::SharedPtr<Project> project = System::MakeObject<Project>();
 3    
 4// New calendar can be added to a project's calendar collection using the collection's Add method.
 5System::SharedPtr<Aspose::Tasks::Calendar> cal1 = project->get_Calendars()->Add(u"New Info");
 6System::SharedPtr<Aspose::Tasks::Calendar> cal2 = project->get_Calendars()->Add(u"no name");
 7System::SharedPtr<Aspose::Tasks::Calendar> cal3 = project->get_Calendars()->Add(u"cal3");
 8    
 9// Save the Project
10System::String dataDir = Examples::CPP::RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
11project->Save(dataDir + u"CreatingCalendar_out.Xml", Aspose::Tasks::Saving::SaveFileFormat::XML);

Defining Weekdays for Calendar

Microsoft Project keeps track of which days are considered weekdays in a calendar so that it can calculate project end dates and so on. Aspose.Tasks for C++ API allows developers to define weekdays for a calendar associated with a project.

The Days collection exposed by the Calendar class is used to define the weekdays for a calendar. The Days collection represents an array list of WeekDay objects.

The CreateDefaultWorkingDay method exposed by the WeekDay class can further be implemented to define the default day timings, while the FromTime and ToTime properties exposed by the WorkingTime class is used to define the specific timing for a day. FromTime and ToTime support the DateTime datatype.

The code samples below set weekdays for a project. After running the code, open the output file in Microsoft Project and, on the Tools menu, select Change Work Timing to establish that the changes have been applied.

Defining weekdays in Microsoft Project

edit calendar in Microsoft Project 2010

The code sample given below defines Monday through Thursday as weekdays with default timings, whereas Friday as a weekday with special timings.

 1// This example shows how Aspose.Tasks API can be used to define a new Calendar, add week days to it and define working times for days
 2// Create a project instance
 3System::SharedPtr<Project> project = System::MakeObject<Project>();
 4    
 5// Define Calendar
 6System::SharedPtr<Aspose::Tasks::Calendar> cal = project->get_Calendars()->Add(u"Calendar1");
 7    
 8// Add working days monday through thursday with default timings
 9cal->get_WeekDays()->Add(WeekDay::CreateDefaultWorkingDay(Aspose::Tasks::DayType::Monday));
10cal->get_WeekDays()->Add(WeekDay::CreateDefaultWorkingDay(Aspose::Tasks::DayType::Tuesday));
11cal->get_WeekDays()->Add(WeekDay::CreateDefaultWorkingDay(Aspose::Tasks::DayType::Wednesday));
12cal->get_WeekDays()->Add(WeekDay::CreateDefaultWorkingDay(Aspose::Tasks::DayType::Thursday));
13cal->get_WeekDays()->Add(System::MakeObject<WeekDay>(Aspose::Tasks::DayType::Saturday));
14cal->get_WeekDays()->Add(System::MakeObject<WeekDay>(Aspose::Tasks::DayType::Sunday));
15    
16// Set friday as short working day
17System::SharedPtr<WeekDay> myWeekDay = System::MakeObject<WeekDay>(Aspose::Tasks::DayType::Friday);
18    
19// Sets working time. Only time part of DateTime is important
20System::SharedPtr<WorkingTime> wt1 = System::MakeObject<WorkingTime>();
21wt1->set_FromTime(System::DateTime(1, 1, 1, 9, 0, 0, 0));
22wt1->set_ToTime(System::DateTime(1, 1, 1, 12, 0, 0, 0));
23System::SharedPtr<WorkingTime> wt2 = System::MakeObject<WorkingTime>();
24wt2->set_FromTime(System::DateTime(1, 1, 1, 13, 0, 0, 0));
25wt2->set_ToTime(System::DateTime(1, 1, 1, 16, 0, 0, 0));
26myWeekDay->get_WorkingTimes()->Add(wt1);
27myWeekDay->get_WorkingTimes()->Add(wt2);
28myWeekDay->set_DayWorking(true);
29cal->get_WeekDays()->Add(myWeekDay);
30    
31// Save the Project
32System::String dataDir = Examples::CPP::RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
33project->Save(u"Project_DefineCalendarWeekdays_out.xml", Aspose::Tasks::Saving::SaveFileFormat::XML);

Making a Standard Calendar

A standard calendar provides the most common work days, work hours and holidays. By default, a standard calendar is added whenever a project is created using Microsoft Project. Aspose.Tasks for C++ API has features for defining a standard calendar for a project.

The Calendar class exposes the following two static overloaded methods for creating a standard calendar:

In Microsoft Project, open the file output by the code below and, on the Tools menu, select Change Working Times Information, to see the standard calendar.

The Change Working Times Information dialogue shows the standard calendar used by the project

editing calendar working times in Microsoft Project

The following lines of code create a standard calendar using both methods.

 1// Create a project instance
 2System::SharedPtr<Project> project = System::MakeObject<Project>();
 3    
 4// Define Calendar and make it standard
 5System::SharedPtr<Aspose::Tasks::Calendar> cal1 = project->get_Calendars()->Add(u"My Cal");
 6Aspose::Tasks::Calendar::MakeStandardCalendar(cal1);
 7    
 8// Save the Project
 9System::String dataDir = Examples::CPP::RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
10project->Save(dataDir + u"Project_MakeStandardCalendar_out.xml", Aspose::Tasks::Saving::SaveFileFormat::XML);
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.