カレンダーの更新と削除
Contents
[
Hide
Show
]カレンダーの交換
カレンダーは、リソースが利用可能かどうか、およびタスクがスケジュールされたときに使用されるために使用されます。カレンダーにはさまざまな種類があります。この記事では、ベースカレンダー、つまりプロジェクトやタスクに使用されるカレンダーを別のカレンダーに置き換える方法について説明します。
ベースカレンダーにはさまざまな形式があります:
- 標準:デフォルトのカレンダーには、月曜日から金曜日の仕事の週と、08:00から17:00までの日があります。
- 24時間:24時間作業、または24時間走るリソースに使用されます。
- 夜勤:標準カレンダーと同様に、夜間シフトカレンダーには月曜日から土曜日の朝の仕事と23:00から08:00までの日があります。
カレンダーを新しいカレンダーに置き換える
すべてのカレンダーのリストは、プロジェクトクラスを使用してカレンダーコレクションとして取得できます。次のコードサンプルに示すように、カレンダーコレクションを使用して、新しいカレンダーを削除または追加できます。
以下に示すコードの例は、既存のカレンダーを新しい標準カレンダーに置き換える方法を示しています。
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