Actualización y eliminación del calendario
Reemplazo de un calendario
Los calendarios se utilizan para ver si hay recursos disponibles y cuando se programan las tareas. Hay diferentes tipos de calendarios. Este artículo analiza cómo reemplazar un calendario base, es decir, el calendario utilizado para proyectos y tareas, con otro.
Los calendarios base vienen en diferentes formas:
- Estándar: el calendario predeterminado tiene una semana de trabajo de lunes a viernes y un día que se extiende de 08:00 a 17:00.
- 24 horas: utilizado para el trabajo durante todo el día, o para recursos que se ejecutan durante todo el día.
- Cambio nocturno: Similar al calendario estándar, el calendario de cambio de noche tiene una semana de trabajo de lunes a sábado por la mañana y un día que se extiende de 23:00 a 08:00.
Reemplazar un calendario con un nuevo calendario
La lista de todos los calendarios se puede recuperar como calendarcolection utilizando la clase de proyecto. La calendarCollection se puede usar para eliminar o agregar un nuevo calendario como se muestra en la siguiente muestra de código.
El ejemplo del código que se da a continuación demuestra cómo reemplazar un calendario existente con un nuevo calendario estándar.
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:
- Read the source MPP file.
- Add the calendar data to the project.
- 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