如何使用 vsto 插件将员工的工作时间导入到 Microsoft Project 文件中的 TimeScaleData?
这是我的样本数据,
[{Name:"Phase 1", BaselineStart:"2023-01-01", BaselineFinish:"2023-03-30",
Employees:[
{Name:"John",EntryDates:[{DateEntered:"2023-12-01",Hours:6},{DateEntered:"2023-12-02",Hours:8},{DateEntered:"2023-12-05",Hours:9}]},
{Name:"Rosy",EntryDates:[{DateEntered:"2023-11-04",Hours:2},{DateEntered:"2023-12-02",Hours:8},{DateEntered:"2023-10-16",Hours:9}]}
]
}]
这是我的代码,但它在这一行给了我一个类似
System.Runtime.InteropServices.COMException: 'The argument value is not valid.'
的错误 Assignment newAssignment = newPhaseTask.Assignments.Add(resource);
Project project = Globals.ThisAddIn.Application.ActiveProject;
Task newPhaseTask = project.Tasks.Add(phase.Name);
foreach (Employee emp in Employees)
{
Resource resource = null;
foreach (Resource projResource in project.Resources)
{
if (projResource.Name.Equals(emp.Name, StringComparison.OrdinalIgnoreCase))
{
resource = projResource;
break;
}
}
if (resource == null)
{
resource = project.Resources.Add(emp.Name);
}
foreach (var date in emp.EntryDates)
{
foreach (Assignment assignment in newPhaseTask.Assignments)
{
if (assignment.Resource == resource)
{
assignmentExists = true;
break; // Exit the loop if an assignment for the resource is found
}
}
if (!assignmentExists)
{
// Create a new assignment for the resource on the task
Assignment newAssignment = newPhaseTask.Assignments.Add(resource);
// Assign work hours to the specified date using TimeScaleData
newAssignment.TimeScaleData(date.DateEntered, date.DateEntered, PjAssignmentTimescaledData.pjAssignmentTimescaledWork, PjTimescaleUnit.pjTimescaleDays, 1).Add(date.DateEntered, date.Hours);
}
}
}
这是我的代码,但它给了我一个错误,比如 System.Runtime.InteropServices.COMException:'参数值为 无效。'在这一行分配 newAssignment = newPhaseTask.Assignments.Add(资源);
参数值无效,因为 Assignments.Add 方法将资源的 ID 作为参数,而不是资源对象。
尝试:
Assignment newAssignment = newPhaseTask.Assignments.Add(resource.ID);