This is my code which adding production calendars
public static void AddProductionCalendar(this Project project, List<ProductionCalendarDto> productionCalendars)
{
project.Calendar = null;
var calendarsToDelete = project.Calendars.ToList() ?? new List<Calendar>();
foreach(var calendar in calendarsToDelete)
{
project.Calendars.Remove(calendar);
}
foreach (var productionCalendar in productionCalendars)
{
try
{
var cal = project.Calendars.Add(productionCalendar.Name);
cal.Uid = productionCalendar.Id;
if (productionCalendar.IsDefault)
{
project.Set(Prj.Calendar, cal);
project.Calendar = cal;
}
cal.Exceptions.AddRange(productionCalendar.Calendars
.Select(c =>
{
var clException = new CalendarException();
clException.Name = c.Name;
clException.ToDate = c.ToDate;
clException.FromDate = c.FromDate;
if(c.IsDayWorking)
{
clException.DayWorking = c.IsDayWorking;
clException.WorkingTimes.Add(new WorkingTime(DateTime.ParseExact("0001-01-01T09:00:00", "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture), DateTime.ParseExact("0001-01-01T13:00:00", "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture)));
clException.WorkingTimes.Add(new WorkingTime(DateTime.ParseExact("0001-01-01T14:00:00", "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture), DateTime.ParseExact("0001-01-01T18:00:00", "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture)));
}
return clException;
}));
foreach (var weekDay in productionCalendar.WeekDays)
{
var newWorkingTimes = weekDay.WorkingTimes
.Select(w => new WorkingTime(w.From, w.To));
var weekDays = new WeekDay(weekDay.DayType, newWorkingTimes)
{
DayWorking = weekDay.DayWorking,
};
cal.WeekDays.Add(weekDays);
}
}
catch (Exception ex)
{
throw new Exception($"Ошибка во время создания производственного календаря {productionCalendar.Name}: {ex.Message}");
}
}
}
Example of ProductionCalendarDto in json
{
"id": 1,
"name": "Импортированный производственный календарь: 2023-05-04",
"calendars": [
{
"name": "День народного единства",
"fromDate": "2030-11-04T00:00:00",
"toDate": "2030-11-04T23:59:00",
"isDayWorking": false
},
{
"name": "Новогодние каникулы",
"fromDate": "2014-01-01T00:00:00",
"toDate": "2014-01-01T23:59:00",
"isDayWorking": false
}
],
"weekDays": [
{
"dayType": 7,
"workingTimes": []
},
{
"dayType": 1,
"workingTimes": []
},
{
"dayType": 2,
"workingTimes": [
{
"to": "0001-01-01T13:00:00",
"from": "0001-01-01T09:00:00"
},
{
"to": "0001-01-01T18:00:00",
"from": "0001-01-01T14:00:00"
}
],
"dayWorking": true
},
{
"dayType": 3,
"workingTimes": [
{
"to": "0001-01-01T13:00:00",
"from": "0001-01-01T09:00:00"
},
{
"to": "0001-01-01T18:00:00",
"from": "0001-01-01T14:00:00"
}
],
"dayWorking": true
},
{
"dayType": 4,
"workingTimes": [
{
"to": "0001-01-01T13:00:00",
"from": "0001-01-01T09:00:00"
},
{
"to": "0001-01-01T18:00:00",
"from": "0001-01-01T14:00:00"
}
],
"dayWorking": true
},
{
"dayType": 5,
"workingTimes": [
{
"to": "0001-01-01T13:00:00",
"from": "0001-01-01T09:00:00"
},
{
"to": "0001-01-01T18:00:00",
"from": "0001-01-01T14:00:00"
}
],
"dayWorking": true
},
{
"dayType": 6,
"workingTimes": [
{
"to": "0001-01-01T13:00:00",
"from": "0001-01-01T09:00:00"
},
{
"to": "0001-01-01T18:00:00",
"from": "0001-01-01T14:00:00"
}
],
"dayWorking": true
}
],
"isDefault": true
}
When I save mpp file after that I cant change any data in calendar when I open that file in Microsoft Project. I pin screenshot of it.
Screenshot_157.png (31.6 KB)