Updating and Removing Calendar

Replacing a Calendar

Calendars are used to see whether resources are available, and when tasks are scheduled. There are different types of calendars. This article looks at how to replace a base calendar, that is, the calendar used for projects and tasks, with another one.

Base calendars come in different forms:

Replacing a Calendar with a New Calendar

The list of all calendars can be retrieved as CalendarCollection using the Project class. The CalendarCollection can then be used to Remove or Add a new calendar as shown in the following code sample.

The code example given below demonstrates how to replace an existing calendar with a new standard calendar.

 1// Create project
 2System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"Project5.mpp");
 3        
 4// Access project calendars
 5System::SharedPtr<CalendarCollection> calColl = project->get_Calendars();
 6calColl->Remove(calColl->GetByName(u"TestCalendar"));
 7        
 8// Add new calendar
 9System::SharedPtr<Calendar> newCalendar = calColl->Add(u"TestCalendar");
10project->Save(dataDir + u"ReplaceCalendar_out.mpp", Aspose::Tasks::Saving::SaveFileFormat::MPP);

Writing Updated Calendar Data to MPP

With Aspose.Tasks for C++, you can update calendar data in a Microsoft Project MPP file and save it back.

The following code shows how to update the calendar data of a project by adding a new calendar and saving it back to the original MPP file. The steps involved in this activity are:

  1. Read the source MPP file.
  2. Add the calendar data to the project.
  3. Save the updated project data back to the MPP file.
 1System::String resultFile = u"result_WriteUpdatedCalendarDataToMPP_out.mpp";
 2System::String newFile = u"project_update_test.mpp";
 3System::String dataDir = Examples::CPP::RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
 4    
 5try
 6{
 7    // Create project instance and access calendar
 8    System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + newFile);
 9    System::SharedPtr<Calendar> cal = project->get_Calendars()->GetByUid(3);
10    
11    // Update the calendar information
12    Calendar::MakeStandardCalendar(cal);
13    cal->set_Name(u"Test calendar");
14    System::SharedPtr<CalendarException> exc = System::MakeObject<CalendarException>();
15    exc->set_FromDate(System::DateTime::get_Now());
16    exc->set_ToDate(System::DateTime::get_Now().AddDays(2));
17    exc->set_DayWorking(true);
18    
19    System::SharedPtr<WorkingTime> wt1 = System::MakeObject<WorkingTime>();
20    wt1->set_FromTime(System::DateTime(10, 1, 1, 9, 0, 0));
21    wt1->set_ToTime(System::DateTime(10, 1, 1, 13, 0, 0));
22    
23    System::SharedPtr<WorkingTime> wt2 = System::MakeObject<WorkingTime>();
24    wt2->set_FromTime(System::DateTime(10, 1, 1, 14, 0, 0));
25    wt2->set_ToTime(System::DateTime(10, 1, 1, 19, 0, 0));
26    
27    System::SharedPtr<WorkingTime> wt3 = System::MakeObject<WorkingTime>();
28    wt3->set_FromTime(System::DateTime(10, 1, 1, 20, 0, 0));
29    wt3->set_ToTime(System::DateTime(10, 1, 1, 21, 0, 0));
30    
31    exc->get_WorkingTimes()->Add(wt1);
32    exc->get_WorkingTimes()->Add(wt2);
33    exc->get_WorkingTimes()->Add(wt3);
34    cal->get_Exceptions()->Add(exc);
35    
36    System::SharedPtr<CalendarException> exc2 = System::MakeObject<CalendarException>();
37    exc2->set_FromDate(System::DateTime::get_Now().AddDays(7));
38    exc2->set_ToDate(exc2->get_FromDate());
39    exc2->set_DayWorking(false);
40    cal->get_Exceptions()->Add(exc2);
41    
42    project->Set<System::SharedPtr<Calendar>>(Prj::Calendar(), cal);
43    
44    // Save project
45    project->Save(dataDir + resultFile, Aspose::Tasks::Saving::SaveFileFormat::MPP);
46}
47catch (System::Exception& ex)
48{
49    System::Console::WriteLine(ex->get_Message() + u"\nThis example will only work if you apply a valid Aspose License. You can purchase full license or get 30 day temporary license from http:// Www.aspose.com/purchase/default.aspx.");
50}
51    
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.