Hi,
I use the following code to generate the mpp file, and the content is correct, but if I create a new task and select a resource, the symbol “[]” will be followed by the resource. I know that this symbol represents the unit ratio of the resource. Yes, but what if the proportion of this resource is automatically 100% when a new task is created? I tried to create a new mpp file and found that there is no such problem. Please help to find out the specific reason.
public Dictionary<string, int> dicResource { get; set; } = new Dictionary<string, int>();
public int TaskIndex { get; set; }
public void SaveProjectOverviewReport()
{
var project = new Project();
project.CalculationMode = CalculationMode.Manual;
project.WorkFormat = TimeUnitType.Minute;
project.NewTasksAreManual = true;//新任务模式
project.TaskUpdatesResource = true;
project.AutoAddNewResourcesAndTasks = true;
project.Autolink = true;
project.StartDate = new DateTime(2022, 5, 27);
project.MinutesPerDay = 480;
project.TaskUpdatesResource = true;
//设置日历
var calendar = project.Calendar;
calendar.WeekDays.Clear();
calendar.Exceptions.Clear();
calendar.Name = "默认日历";
var sunday = new WeekDay(DayType.Sunday);
sunday.DayWorking = false;
calendar.WeekDays.Add(sunday);
var monday = new WeekDay(DayType.Monday);
monday.WorkingTimes.Add(new WorkingTime(new DateTime(2023, 1, 1, 8, 0, 0), new DateTime(2023, 1, 1, 12, 0, 0)));
monday.WorkingTimes.Add(new WorkingTime(new DateTime(2023, 1, 1, 13, 0, 0), new DateTime(2023, 1, 1, 17, 0, 0)));
monday.DayWorking = true;
calendar.WeekDays.Add(monday);
var tuesday = new WeekDay(DayType.Tuesday);
tuesday.WorkingTimes.Add(new WorkingTime(new DateTime(2023, 1, 1, 8, 0, 0), new DateTime(2023, 1, 1, 12, 0, 0)));
tuesday.WorkingTimes.Add(new WorkingTime(new DateTime(2023, 1, 1, 13, 0, 0), new DateTime(2023, 1, 1, 17, 0, 0)));
tuesday.DayWorking = true;
calendar.WeekDays.Add(tuesday);
var wednesday = new WeekDay(DayType.Wednesday);
wednesday.WorkingTimes.Add(new WorkingTime(new DateTime(2023, 1, 1, 8, 0, 0), new DateTime(2023, 1, 1, 12, 0, 0)));
wednesday.WorkingTimes.Add(new WorkingTime(new DateTime(2023, 1, 1, 13, 0, 0), new DateTime(2023, 1, 1, 17, 0, 0)));
wednesday.DayWorking = true;
calendar.WeekDays.Add(wednesday);
var thursday = new WeekDay(DayType.Thursday);
thursday.WorkingTimes.Add(new WorkingTime(new DateTime(2023, 1, 1, 8, 0, 0), new DateTime(2023, 1, 1, 12, 0, 0)));
thursday.WorkingTimes.Add(new WorkingTime(new DateTime(2023, 1, 1, 13, 0, 0), new DateTime(2023, 1, 1, 17, 0, 0)));
thursday.DayWorking = true;
calendar.WeekDays.Add(thursday);
var friday = new WeekDay(DayType.Friday);
friday.WorkingTimes.Add(new WorkingTime(new DateTime(2023, 1, 1, 8, 0, 0), new DateTime(2023, 1, 1, 12, 0, 0)));
friday.WorkingTimes.Add(new WorkingTime(new DateTime(2023, 1, 1, 13, 0, 0), new DateTime(2023, 1, 1, 17, 0, 0)));
friday.DayWorking = true;
calendar.WeekDays.Add(friday);
var saturday = new WeekDay(DayType.Saturday);
saturday.DayWorking = false;
calendar.WeekDays.Add(saturday);
//特殊日历
var exception = new CalendarException();
exception.EnteredByOccurrences = false;
exception.FromDate = new DateTime(2022, 1, 31, 0, 0, 0);
exception.ToDate = new DateTime(2022, 2, 6, 23, 59, 59);
exception.Type = CalendarExceptionType.Daily;
exception.DayWorking = false;
calendar.Exceptions.Add(exception);
var exception2 = new CalendarException();
exception2.EnteredByOccurrences = false;
exception2.FromDate = new DateTime(2021, 12, 31, 0, 0, 0);
exception2.ToDate = new DateTime(2022, 1, 2, 23, 59, 59);
exception2.Type = CalendarExceptionType.Daily;
exception2.DayWorking = false;
calendar.Exceptions.Add(exception2);
#region 设置项目资源
dicResource["xucan"] = 1;
dicResource["yangjunling"] = 2;
dicResource["yaoyuan.yd"] = 3;
dicResource["chenbo"] = 4;
dicResource["fuhongcai"] = 5;
dicResource["wanglingna"] = 6;
dicResource["zhangfeng"] = 7;
dicResource["liuqianlong"] = 8;
dicResource["hyq.hyq"] = 9;
dicResource["huangfei"] = 10;
dicResource["liuqi"] = 11;
dicResource["lijun"] = 12;
dicResource["wangwu"] = 13;
dicResource["wangzhao"] = 14;
dicResource["zhangzhenxing"] = 15;
dicResource["shuyuehong"] = 16;
dicResource["lisi"] = 17;
dicResource["zhuchuanli"] = 18;
dicResource["liukun"] = 19;
dicResource["liufeng"] = 20;
dicResource["zhangsan"] = 21;
dicResource["tangxiaofeng"] = 22;
foreach (var item in dicResource)
{
var resource = project.Resources.Add(item.Key);
}
#endregion
//任务
Task task = project.RootTask.Children.Add("我的测试计划");
//task.Id = 1;
task.IgnoreResourceCalendar = true;
task.OutlineLevel = 1;
task.Start = new DateTime(2022, 5, 27);
task.Finish = new DateTime(2022, 8, 30); ;
task.Duration = project.GetDuration(68, TimeUnitType.Day);
task.PercentComplete = 11;
task.IsMilestone = false;
//task.ConstraintType =
//task.ConstraintDate =
task.IsEstimated = true;//是否估计
task.IsManual = false;
project.ResourceAssignments.Add(task, project.Resources.GetById(dicResource["xucan"]));
//Task task1 = task.Children.Add("Task1");
TaskIndex = 2;
Task Task1 = TaskInit(project, parentTask: task, "Task1", durationDays: 3, new DateTime(2022, 5, 27), new DateTime(2022, 5, 31), 100, isMileStone: true, new string[1] { "xucan" });
Task Task11 = TaskInit(project, parentTask: Task1, "Task11", durationDays: 3, new DateTime(2022, 5, 27), new DateTime(2022, 5, 31), 100, isMileStone: false, new string[1] { "yangjunling" });
Task Task111 = TaskInit(project, parentTask: Task11, "Task111", durationDays: 3, new DateTime(2022, 5, 27), new DateTime(2022, 5, 31), 100, isMileStone: false, new string[1] { "yangjunling" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 05, 27, 08, 0, 0));
Task Task112 = TaskInit(project, parentTask: Task11, "Task112", durationDays: 3, new DateTime(2022, 5, 27), new DateTime(2022, 5, 31), 100, isMileStone: false, new string[1] { "yaoyuan.yd" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 05, 27, 08, 0, 0));
Task Task2 = TaskInit(project, parentTask: task, "Task2", durationDays: 6, new DateTime(2022, 6, 1), new DateTime(2022, 6, 8), 100, isMileStone: true, new string[1] { "xucan" }, Task1);
Task Task21 = TaskInit(project, parentTask: Task2, "Task21", durationDays: 3, new DateTime(2022, 6, 1), new DateTime(2022, 6, 3), 100, isMileStone: false, new string[1] { "chenbo" });
Task Task22 = TaskInit(project, parentTask: Task2, "Task22", durationDays: 3, new DateTime(2022, 6, 1), new DateTime(2022, 6, 3), 100, isMileStone: false, new string[1] { "xucan" });
Task Task221 = TaskInit(project, parentTask: Task22, "Task221", durationDays: 3, new DateTime(2022, 6, 1), new DateTime(2022, 6, 3), 100, isMileStone: false, new string[1] { "xucan" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 1, 12, 9, 0, 0));
Task Task23 = TaskInit(project, parentTask: Task2, "Task23", durationDays: 3, new DateTime(2022, 6, 6), new DateTime(2022, 6, 8), 100, isMileStone: false, new string[1] { "xucan" }, Task22);
Task Task3 = TaskInit(project, parentTask: task, "Task3", durationDays: 3, new DateTime(2022, 6, 6), new DateTime(2022, 6, 8), 25, isMileStone: true, new string[1] { "yaoyuan.yd" }, Task22);
Task Task31 = TaskInit(project, parentTask: Task3, "Task31", durationDays: 3, new DateTime(2022, 6, 6), new DateTime(2022, 6, 8), 0, isMileStone: false, new string[1] { "chenbo" });
Task Task311 = TaskInit(project, parentTask: Task31, "Task311", durationDays: 3, new DateTime(2022, 6, 6), new DateTime(2022, 6, 8), 0, isMileStone: false, new string[1] { "chenbo" });
Task Task312 = TaskInit(project, parentTask: Task31, "Task312", durationDays: 0, new DateTime(2022, 6, 6), new DateTime(2022, 6, 6), 0, isMileStone: true, new string[1] { "fuhongcai" });
//以下自动生成
Task Task32 = TaskInit(project, parentTask: Task3, "Task32", durationDays: 3, new DateTime(2022, 6, 6), new DateTime(2022, 6, 8), 0, isMileStone: false, new string[1] { "yaoyuan.yd" }, null);
Task Task33 = TaskInit(project, parentTask: Task3, "Task33", durationDays: 3, new DateTime(2022, 6, 6), new DateTime(2022, 6, 8), 0, isMileStone: false, new string[1] { "wanglingna" }, null);
Task Task331 = TaskInit(project, parentTask: Task33, "Task331", durationDays: 3, new DateTime(2022, 6, 6), new DateTime(2022, 6, 8), 0, isMileStone: false, new string[1] { "wanglingna" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 04, 15, 09, 00, 0));
Task Task34 = TaskInit(project, parentTask: Task3, "Task34", durationDays: 3, new DateTime(2022, 6, 6), new DateTime(2022, 6, 8), 100, isMileStone: false, new string[1] { "xucan" }, null);
Task Task4 = TaskInit(project, parentTask: task, "Task4", durationDays: 3, new DateTime(2022, 6, 8), new DateTime(2022, 6, 13), 0, isMileStone: true, new string[1] { "xucan" }, Task3);
Task Task41 = TaskInit(project, parentTask: Task4, "Task41", durationDays: 3, new DateTime(2022, 6, 9), new DateTime(2022, 6, 13), 0, isMileStone: false, new string[1] { "wanglingna" }, null);
Task Task42 = TaskInit(project, parentTask: Task4, "Task42", durationDays: 3, new DateTime(2022, 6, 9), new DateTime(2022, 6, 13), 0, isMileStone: false, new string[1] { "zhangfeng" }, null);
Task Task43 = TaskInit(project, parentTask: Task4, "Task43", durationDays: 0, new DateTime(2022, 6, 8), new DateTime(2022, 6, 8), 0, isMileStone: true, new string[1] { "liuqianlong" }, null);
Task Task44 = TaskInit(project, parentTask: Task4, "Task44", durationDays: 0, new DateTime(2022, 6, 8), new DateTime(2022, 6, 8), 0, isMileStone: true, new string[1] { "fuhongcai" }, null);
Task Task45 = TaskInit(project, parentTask: Task4, "Task45", durationDays: 3, new DateTime(2022, 6, 9), new DateTime(2022, 6, 13), 0, isMileStone: false, new string[1] { "hyq.hyq" }, null);
Task Task46 = TaskInit(project, parentTask: Task4, "Task46", durationDays: 2, new DateTime(2022, 6, 9), new DateTime(2022, 6, 10), 0, isMileStone: false, new string[1] { "huangfei" }, null);
Task Task47 = TaskInit(project, parentTask: Task4, "Task47", durationDays: 3, new DateTime(2022, 6, 9), new DateTime(2022, 6, 13), 0, isMileStone: false, new string[1] { "hyq.hyq" }, null);
Task Task48 = TaskInit(project, parentTask: Task4, "Task48", durationDays: 0, new DateTime(2022, 6, 8), new DateTime(2022, 6, 8), 0, isMileStone: true, new string[1] { "huangfei" }, null);
Task Task49 = TaskInit(project, parentTask: Task4, "Task49", durationDays: 0, new DateTime(2022, 6, 8), new DateTime(2022, 6, 8), 0, isMileStone: true, new string[1] { "yaoyuan.yd" }, null);
Task Task410 = TaskInit(project, parentTask: Task4, "Task410", durationDays: 0, new DateTime(2022, 6, 8), new DateTime(2022, 6, 8), 0, isMileStone: true, new string[1] { "hyq.hyq" }, null);
Task Task4101 = TaskInit(project, parentTask: Task410, "Task4101", durationDays: 0, new DateTime(2022, 6, 8), new DateTime(2022, 6, 8), 0, isMileStone: true, new string[1] { "hyq.hyq" }, null);
Task Task4102 = TaskInit(project, parentTask: Task410, "Task4102", durationDays: 0, new DateTime(2022, 6, 8), new DateTime(2022, 6, 8), 0, isMileStone: true, new string[1] { "wanglingna" }, Task4101);
Task Task411 = TaskInit(project, parentTask: Task41, "Task411", durationDays: 0, new DateTime(2022, 6, 8), new DateTime(2022, 6, 8), 0, isMileStone: true, new string[1] { "xucan" }, null);
Task Task5 = TaskInit(project, parentTask: task, "Task5", durationDays: 55, new DateTime(2022, 6, 14), new DateTime(2022, 8, 29), 11, isMileStone: true, new string[1] { "xucan" }, Task4);
Task Task51 = TaskInit(project, parentTask: Task5, "Task51", durationDays: 3, new DateTime(2022, 6, 14), new DateTime(2022, 6, 16), 0, isMileStone: false, new string[1] { "xucan" }, null);
Task Task511 = TaskInit(project, parentTask: Task51, "Task511", durationDays: 3, new DateTime(2022, 6, 14), new DateTime(2022, 6, 16), 0, isMileStone: false, new string[1] { "wanglingna" }, null);
Task Task5111 = TaskInit(project, parentTask: Task511, "Task5111", durationDays: 3, new DateTime(2022, 6, 14), new DateTime(2022, 6, 16), 0, isMileStone: false, new string[1] { "wanglingna" }, null);
Task Task5112 = TaskInit(project, parentTask: Task511, "Task5112", durationDays: 1, new DateTime(2022, 6, 14), new DateTime(2022, 6, 14), 0, isMileStone: false, new string[1] { "wanglingna" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 05, 27, 08, 00, 0));
Task Task52 = TaskInit(project, parentTask: Task5, "Task52", durationDays: 3, new DateTime(2022, 6, 14), new DateTime(2022, 6, 16), 0, isMileStone: false, new string[1] { "zhangfeng" }, null);
Task Task521 = TaskInit(project, parentTask: Task52, "Task521", durationDays: 2, new DateTime(2022, 6, 14), new DateTime(2022, 6, 15), 0, isMileStone: false, new string[2] { "zhangfeng", "liuqi" }, null);
Task Task522 = TaskInit(project, parentTask: Task52, "Task522", durationDays: 3, new DateTime(2022, 6, 14), new DateTime(2022, 6, 16), 0, isMileStone: false, new string[1] { "zhangfeng" }, null);
Task Task53 = TaskInit(project, parentTask: Task5, "Task53", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "liuqianlong" }, null);
Task Task531 = TaskInit(project, parentTask: Task53, "Task531", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "liuqianlong" }, null);
Task Task532 = TaskInit(project, parentTask: Task53, "Task532", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "liuqianlong" }, null);
Task Task5321 = TaskInit(project, parentTask: Task532, "Task5321", durationDays: 2, new DateTime(2022, 6, 14), new DateTime(2022, 6, 15), 0, isMileStone: false, new string[1] { "lijun" }, null);
Task Task5322 = TaskInit(project, parentTask: Task532, "Task5322", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "liuqianlong" }, null);
Task Task5323 = TaskInit(project, parentTask: Task532, "Task5323", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "wangwu" }, null);
Task Task5324 = TaskInit(project, parentTask: Task532, "Task5324", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "wangwu" }, null);
Task Task54 = TaskInit(project, parentTask: Task5, "Task54", durationDays: 7, new DateTime(2022, 6, 14), new DateTime(2022, 6, 22), 100, isMileStone: false, new string[1] { "fuhongcai" }, null);
Task Task541 = TaskInit(project, parentTask: Task54, "Task541", durationDays: 7, new DateTime(2022, 6, 14), new DateTime(2022, 6, 22), 100, isMileStone: false, new string[1] { "fuhongcai" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 05, 27, 08, 00, 0));
Task Task542 = TaskInit(project, parentTask: Task54, "Task542", durationDays: 7, new DateTime(2022, 6, 14), new DateTime(2022, 6, 22), 100, isMileStone: false, new string[1] { "fuhongcai" }, null);
Task Task543 = TaskInit(project, parentTask: Task54, "Task543", durationDays: 7, new DateTime(2022, 6, 14), new DateTime(2022, 6, 22), 100, isMileStone: false, new string[1] { "fuhongcai" }, null);
Task Task55 = TaskInit(project, parentTask: Task5, "Task55", durationDays: 6, new DateTime(2022, 6, 14), new DateTime(2022, 6, 21), 0, isMileStone: false, new string[1] { "chenbo" }, null);
Task Task551 = TaskInit(project, parentTask: Task55, "Task551", durationDays: 2, new DateTime(2022, 6, 14), new DateTime(2022, 6, 15), 0, isMileStone: false, new string[1] { "chenbo" }, null);
Task Task5511 = TaskInit(project, parentTask: Task551, "Task5511", durationDays: 2, new DateTime(2022, 6, 14), new DateTime(2022, 6, 15), 0, isMileStone: false, new string[1] { "chenbo" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 09, 08, 00, 0));
Task Task5512 = TaskInit(project, parentTask: Task551, "Task5512", durationDays: 2, new DateTime(2022, 6, 14), new DateTime(2022, 6, 15), 0, isMileStone: false, new string[1] { "wangzhao" }, null);
Task Task5513 = TaskInit(project, parentTask: Task551, "Task5513", durationDays: 2, new DateTime(2022, 6, 14), new DateTime(2022, 6, 15), 0, isMileStone: false, new string[1] { "hyq.hyq" }, null);
Task Task5514 = TaskInit(project, parentTask: Task551, "Task5514", durationDays: 2, new DateTime(2022, 6, 14), new DateTime(2022, 6, 15), 0, isMileStone: false, new string[1] { "huangfei" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 05, 19, 09, 00, 0));
Task Task552 = TaskInit(project, parentTask: Task55, "Task552", durationDays: 6, new DateTime(2022, 6, 14), new DateTime(2022, 6, 21), 0, isMileStone: false, new string[2] { "huangfei", "hyq.hyq" }, null);
Task Task56 = TaskInit(project, parentTask: Task5, "Task56", durationDays: 55, new DateTime(2022, 6, 14), new DateTime(2022, 8, 29), 0, isMileStone: false, new string[1] { "yaoyuan.yd" }, null);
Task Task561 = TaskInit(project, parentTask: Task56, "Task561", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "yaoyuan.yd" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 01, 08, 00, 0));
Task Task562 = TaskInit(project, parentTask: Task56, "Task562", durationDays: 5, new DateTime(2022, 6, 21), new DateTime(2022, 6, 27), 0, isMileStone: false, new string[1] { "yaoyuan.yd" }, Task531);
Task Task563 = TaskInit(project, parentTask: Task56, "Task563", durationDays: 30, new DateTime(2022, 6, 28), new DateTime(2022, 8, 8), 0, isMileStone: false, new string[1] { "yaoyuan.yd" }, Task562);
Task Task564 = TaskInit(project, parentTask: Task56, "Task564", durationDays: 15, new DateTime(2022, 8, 9), new DateTime(2022, 8, 29), 0, isMileStone: false, new string[1] { "yaoyuan.yd" }, Task563);
Task Task57 = TaskInit(project, parentTask: Task5, "Task57", durationDays: 15, new DateTime(2022, 6, 14), new DateTime(2022, 7, 4), 0, isMileStone: false, new string[1] { "xucan" }, null);
Task Task571 = TaskInit(project, parentTask: Task57, "Task571", durationDays: 15, new DateTime(2022, 6, 14), new DateTime(2022, 7, 4), 0, isMileStone: false, new string[1] { "zhangzhenxing" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 06, 08, 00, 0));
Task Task572 = TaskInit(project, parentTask: Task57, "Task572", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "shuyuehong" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 06, 08, 00, 0));
Task Task573 = TaskInit(project, parentTask: Task57, "Task573", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "shuyuehong" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 06, 08, 00, 0));
Task Task574 = TaskInit(project, parentTask: Task57, "Task574", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "liuqianlong" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 06, 08, 00, 0));
Task Task575 = TaskInit(project, parentTask: Task57, "Task575", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "lisi" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 06, 08, 00, 0));
Task Task576 = TaskInit(project, parentTask: Task57, "Task576", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "lisi" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 06, 08, 00, 0));
Task Task577 = TaskInit(project, parentTask: Task57, "Task577", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "zhuchuanli" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 06, 08, 00, 0));
Task Task578 = TaskInit(project, parentTask: Task57, "Task578", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "lisi" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 06, 08, 00, 0));
Task Task58 = TaskInit(project, parentTask: Task5, "Task58", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "yangjunling" }, null);
Task Task581 = TaskInit(project, parentTask: Task58, "Task581", durationDays: 5, new DateTime(2022, 6, 14), new DateTime(2022, 6, 20), 0, isMileStone: false, new string[1] { "yangjunling" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 06, 08, 00, 0));
Task Task582 = TaskInit(project, parentTask: Task58, "Task582", durationDays: 3, new DateTime(2022, 6, 14), new DateTime(2022, 6, 16), 0, isMileStone: false, new string[1] { "yangjunling" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 06, 08, 00, 0));
Task Task583 = TaskInit(project, parentTask: Task58, "Task583", durationDays: 3, new DateTime(2022, 6, 14), new DateTime(2022, 6, 16), 0, isMileStone: false, new string[1] { "chenbo" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 06, 08, 00, 0));
Task Task584 = TaskInit(project, parentTask: Task58, "Task584", durationDays: 3, new DateTime(2022, 6, 14), new DateTime(2022, 6, 16), 0, isMileStone: false, new string[1] { "yangjunling" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 06, 08, 00, 0));
Task Task59 = TaskInit(project, parentTask: Task5, "Task59", durationDays: 2, new DateTime(2022, 6, 14), new DateTime(2022, 6, 15), 0, isMileStone: false, new string[1] { "xucan" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 14, 08, 00, 0));
Task Task6 = TaskInit(project, parentTask: task, "Task6", durationDays: 50, new DateTime(2022, 6, 22), new DateTime(2022, 8, 30), 0, isMileStone: true, new string[1] { "xucan" }, Task55);
Task Task61 = TaskInit(project, parentTask: Task6, "Task61", durationDays: 3, new DateTime(2022, 6, 22), new DateTime(2022, 6, 24), 0, isMileStone: false, new string[1] { "liukun" }, null);
Task Task611 = TaskInit(project, parentTask: Task61, "Task611", durationDays: 3, new DateTime(2022, 6, 22), new DateTime(2022, 6, 24), 0, isMileStone: false, new string[1] { "lisi" }, null);
Task Task612 = TaskInit(project, parentTask: Task61, "Task612", durationDays: 2, new DateTime(2022, 6, 22), new DateTime(2022, 6, 23), 0, isMileStone: false, new string[1] { "liufeng" }, null);
Task Task62 = TaskInit(project, parentTask: Task6, "Task62", durationDays: 17, new DateTime(2022, 6, 22), new DateTime(2022, 7, 14), 0, isMileStone: false, new string[1] { "liukun" }, null);
Task Task621 = TaskInit(project, parentTask: Task62, "Task621", durationDays: 5, new DateTime(2022, 6, 27), new DateTime(2022, 7, 1), 0, isMileStone: false, new string[1] { "liukun" }, Task611);
Task Task622 = TaskInit(project, parentTask: Task62, "Task622", durationDays: 4, new DateTime(2022, 7, 4), new DateTime(2022, 7, 7), 0, isMileStone: false, new string[1] { "lisi" }, Task621);
Task Task623 = TaskInit(project, parentTask: Task62, "Task623", durationDays: 3, new DateTime(2022, 6, 22), new DateTime(2022, 6, 24), 0, isMileStone: false, new string[1] { "lisi" }, null);
Task Task624 = TaskInit(project, parentTask: Task62, "Task624", durationDays: 4, new DateTime(2022, 6, 27), new DateTime(2022, 6, 30), 0, isMileStone: false, new string[1] { "liukun" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 27, 08, 00, 0));
Task Task625 = TaskInit(project, parentTask: Task62, "Task625", durationDays: 10, new DateTime(2022, 7, 1), new DateTime(2022, 7, 14), 0, isMileStone: false, new string[1] { "liukun" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 07, 01, 08, 00, 0));
Task Task626 = TaskInit(project, parentTask: Task62, "Task626", durationDays: 4, new DateTime(2022, 7, 1), new DateTime(2022, 7, 6), 0, isMileStone: false, new string[1] { "zhangsan" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 07, 01, 08, 00, 0));
Task Task627 = TaskInit(project, parentTask: Task62, "Task627", durationDays: 5, new DateTime(2022, 6, 27), new DateTime(2022, 7, 1), 0, isMileStone: false, new string[1] { "zhangfeng" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 27, 08, 00, 0));
Task Task628 = TaskInit(project, parentTask: Task62, "Task628", durationDays: 5, new DateTime(2022, 6, 27), new DateTime(2022, 7, 1), 0, isMileStone: false, new string[1] { "lisi" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 27, 08, 00, 0));
Task Task629 = TaskInit(project, parentTask: Task62, "Task629", durationDays: 5, new DateTime(2022, 6, 27), new DateTime(2022, 7, 1), 0, isMileStone: false, new string[1] { "lisi" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 06, 27, 08, 00, 0));
Task Task63 = TaskInit(project, parentTask: Task6, "Task63", durationDays: 16, new DateTime(2022, 7, 15), new DateTime(2022, 8, 5), 0, isMileStone: false, new string[1] { "liukun" }, null);
Task Task631 = TaskInit(project, parentTask: Task63, "Task631", durationDays: 16, new DateTime(2022, 7, 15), new DateTime(2022, 8, 5), 0, isMileStone: false, new string[1] { "liukun" }, Task625);
Task Task632 = TaskInit(project, parentTask: Task63, "Task632", durationDays: 10, new DateTime(2022, 7, 15), new DateTime(2022, 7, 28), 0, isMileStone: false, new string[1] { "chenbo" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 07, 15, 08, 00, 0));
Task Task633 = TaskInit(project, parentTask: Task63, "Task633", durationDays: 10, new DateTime(2022, 7, 15), new DateTime(2022, 7, 28), 0, isMileStone: false, new string[1] { "tangxiaofeng" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 07, 15, 08, 00, 0));
Task Task634 = TaskInit(project, parentTask: Task63, "Task634", durationDays: 5, new DateTime(2022, 7, 25), new DateTime(2022, 7, 29), 0, isMileStone: false, new string[1] { "liukun" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 07, 25, 08, 00, 0));
Task Task635 = TaskInit(project, parentTask: Task63, "Task635", durationDays: 5, new DateTime(2022, 7, 25), new DateTime(2022, 7, 29), 0, isMileStone: false, new string[1] { "zhangfeng" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 07, 25, 08, 00, 0));
Task Task64 = TaskInit(project, parentTask: Task6, "Task64", durationDays: 5, new DateTime(2022, 8, 1), new DateTime(2022, 8, 5), 0, isMileStone: false, new string[1] { "yangjunling" }, null);
Task Task641 = TaskInit(project, parentTask: Task64, "Task641", durationDays: 5, new DateTime(2022, 8, 1), new DateTime(2022, 8, 5), 0, isMileStone: false, new string[1] { "yangjunling" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 08, 01, 08, 00, 0));
Task Task642 = TaskInit(project, parentTask: Task64, "Task642", durationDays: 5, new DateTime(2022, 8, 1), new DateTime(2022, 8, 5), 0, isMileStone: false, new string[1] { "yangjunling" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 08, 01, 08, 00, 0));
Task Task643 = TaskInit(project, parentTask: Task64, "Task643", durationDays: 5, new DateTime(2022, 8, 1), new DateTime(2022, 8, 5), 0, isMileStone: false, new string[1] { "yangjunling" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 08, 01, 08, 00, 0));
Task Task644 = TaskInit(project, parentTask: Task64, "Task644", durationDays: 5, new DateTime(2022, 8, 1), new DateTime(2022, 8, 5), 0, isMileStone: false, new string[1] { "yangjunling" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 08, 01, 08, 00, 0));
Task Task645 = TaskInit(project, parentTask: Task64, "Task645", durationDays: 5, new DateTime(2022, 8, 1), new DateTime(2022, 8, 5), 0, isMileStone: false, new string[1] { "yangjunling" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 08, 01, 08, 00, 0));
Task Task646 = TaskInit(project, parentTask: Task64, "Task646", durationDays: 5, new DateTime(2022, 8, 1), new DateTime(2022, 8, 5), 0, isMileStone: false, new string[1] { "yangjunling" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 08, 01, 08, 00, 0));
Task Task647 = TaskInit(project, parentTask: Task64, "Task647", durationDays: 5, new DateTime(2022, 8, 1), new DateTime(2022, 8, 5), 0, isMileStone: false, new string[1] { "yangjunling" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 08, 01, 08, 00, 0));
Task Task65 = TaskInit(project, parentTask: Task6, "Task65", durationDays: 5, new DateTime(2022, 7, 25), new DateTime(2022, 7, 29), 0, isMileStone: false, new string[1] { "yaoyuan.yd" }, null);
Task Task651 = TaskInit(project, parentTask: Task65, "Task651", durationDays: 5, new DateTime(2022, 7, 25), new DateTime(2022, 7, 29), 0, isMileStone: false, new string[1] { "yaoyuan.yd" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 07, 25, 08, 00, 0));
Task Task66 = TaskInit(project, parentTask: Task6, "Task66", durationDays: 4, new DateTime(2022, 8, 25), new DateTime(2022, 8, 30), 0, isMileStone: false, new string[1] { "liukun" }, null, ConstraintType.StartNoEarlierThan, new DateTime(2022, 08, 25, 08, 00, 0));
project.CalculationMode = CalculationMode.Automatic;
project.NewTasksAreManual = false;//新任务模式
project.Recalculate();
// one can save the overview report to PDF file to the specified path
string fileName = $"CreateNewTask_out{DateTime.Now.ToString("MMddHHmmss") }.mpp";
project.Save(OutDir + fileName, SaveFileFormat.Mpp);
#region 增加扩展列
var attr = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Text, ExtendedAttributeTask.Text1, "Custom Field");
project.ExtendedAttributes.Add(attr);
foreach (var item in project.RootTask.Children)
{
var attribute = attr.CreateExtendedAttribute("Test value");
item.ExtendedAttributes.Add(attribute);
}
var table = project.Tables.FirstOrDefault(t => t.Name == "&Entry"); // Table of Gantt chart view which is displayed by default when MPP file is opened.
// Adding column
//table.TableFields.Add(new TableField() { Field = Field.TaskText1, Title = "Task Text1", Width = 20 });
table.TableFields.Insert(3, new TableField() { Field = Field.TaskOutlineNumber, Width = 12 }); //大纲号
table.TableFields.Add(new TableField() { Field = Field.TaskMilestone, Width = 10 });
table.TableFields.Add(new TableField() { Field = Field.TaskWork, Width = 12 }); //任务工时
table.TableFields.Add(new TableField() { Field = Field.TaskPercentComplete, Width = 12 }); //任务完成百分比
table.TableFields.Add(new TableField() { Field = Field.TaskActualWork, Width = 12 }); //实际工时
project.Save(OutDir + fileName, new MPPSaveOptions()
{
WriteViewData = true // WriteViewData flag should be specified to save changes in TableFields.
});
#endregion
// ExEnd:SaveProjectOverviewReport
}
private Task TaskInit(Project project, Task parentTask, string currentTaskName, int durationDays,
DateTime start, DateTime finish, int persent, bool isMileStone, string[] resources,
Task beforeTask = null, ConstraintType limitType = ConstraintType.AsSoonAsPossible, DateTime? limitDateTime = null)
{
Task task = parentTask.Children.Add(currentTaskName);
task.IgnoreResourceCalendar = true;
if (start.Hour == 0)
{
start = start.AddHours(8);
}
task.Start = start;// new DateTime(2022, 6, 1);
if (finish.Hour == 0)
{
finish = finish.AddHours(17);
}
task.Finish = finish;// new DateTime(2022, 6, 8);
task.Duration = project.GetDuration(durationDays, TimeUnitType.Day);
task.PercentComplete = persent;// 100;
task.IsMilestone = isMileStone;
task.ConstraintType = limitType;
if (limitDateTime.HasValue)
{
task.ConstraintDate = limitDateTime.Value;
}
task.IsEstimated = false;//是否估计
task.IsManual = false;
task.ActualWork = project.GetWork(durationDays * project.MinutesPerDay * (persent / 100D));
task.RemainingWork = project.GetWork(durationDays * project.MinutesPerDay * ((100 - persent) / 100D));
task.Work = project.GetWork(durationDays * project.MinutesPerDay);
foreach (var item in resources)
{
var resAss = project.ResourceAssignments.Add(task, project.Resources.GetById(dicResource[item]));
resAss.Set(Asn.Start, start);
resAss.Set(Asn.Work, task.Duration);
resAss.Set(Asn.Finish, finish);
resAss.Set(Asn.Units, 1);
resAss.Set(Asn.PeakUnits, 1);
}
if (beforeTask != null)
{
project.TaskLinks.Add(beforeTask, task, TaskLinkType.FinishToStart, project.GetWork(0));
}
TaskIndex++;
return task;
}