尝试使用库MPXJ将任务分配给任务和子任务时遇到问题。实际上,当我将ressource分配给子任务时,所有任务持续时间都会被修改,我不明白为什么。 如果我没有分配资源,持续时间设置得很好(每个子任务4天),但是当我这样做时,是0。
有人能帮助我理解为什么吗?这是VB.net中的代码,我已尝试使用c#和同样的问题:
Dim xmlwriter As New MSPDIWriter
Dim projectfile As New ProjectFile
Dim personcount = 1
Dim pre As Task = Nothing
'Filling file file with some dummy data
For i As Integer = 1 To 10
Dim task As Task = projectfile.addTask
task.setName("Example Task" & i)
Dim presub As Task = Nothing
'Add some subtasks
For j As Integer = 1 To 10
Dim subtask As Task = task.addTask()
subtask.setName("Sub Task" & j)
'Set the subtasks duration = ' hours for every sub task
subtask.setDuration(Duration.getInstance(4, TimeUnit.DAYS))
'add Resources to the subtask = one resource for every task ^^
' 1) add resource to the general projectfile
Dim res As Resource = projectfile.addResource()
res.setName("person" & personcount)
personcount += 1
'Asociate the resource with the courent task
Dim assgmnt As ResourceAssignment = subtask.addResourceAssignment(res)
'Concatenate the subtasks, so that one subtask is performed after
'another in the timeline
'The first task has no predecessor
If j = 1 Then
presub = subtask
Else
subtask.addPredecessor(presub, RelationType.FINISH_START, Duration.getInstance(0, TimeUnit.DAYS))
presub = subtask
End If
'presub.setDuration(Duration.getInstance(4, TimeUnit.DAYS))
Next
'Concatenate the tasks, son that one main task is performed after
'another on the timeline
'The first task has no predecessor,
If i = 1 Then
'set the start date of the project
Dim rightnow As java.util.Calendar = java.util.Calendar.getInstance()
rightnow.set(2014, 11, 1)
task.setStart(rightnow.getTime())
pre = task
Else
task.addPredecessor(pre, RelationType.FINISH_START, Duration.getInstance(0, TimeUnit.DAYS))
pre = task
End If
Next
'Writng the project file
xmlwriter.write(projectfile, "C:\temp\exo.xml")
谢谢!
答案 0 :(得分:1)
如果您向任务添加资源分配,则还需要设置该任务的工作量。
例如,如果您有5天任务,没有资源分配,则为任务配置开始日期和五天持续时间就足够了:
Dim task As Task = projectfile.addTask
task.setName("Example Task")
task.setDuration(Duration.getInstance(5, TimeUnit.DAYS));
这应该像您期望的那样出现在MS Project中。
如果您为该任务分配资源,您现在需要设置完成任务所需的工作量,因为这将推动持续时间:
Dim task As Task = projectfile.addTask
task.setName("Example Task")
task.setWork(Duration.getInstance(5, TimeUnit.DAYS))
Dim res As Resource = projectfile.addResource()
res.setName("Example resource")
task.addResourceAssignment(res)
如果要表明任务已完成或部分完成,您还需要将实际工作属性设置为适当的值。例如,要使5天任务完成50%:
task.setActualWork(Duration.getInstance(2.5, TimeUnit.DAYS))
您添加到任务的资源越多,获得的资源就越短,例如,如果您的任务需要5天的工作才能完成并且您添加了两个资源,那么您的任务将在2.5天内完成。这是努力驱动的计划,如here所述。
如果您愿意,可以逐个任务地关闭工作量驱动的计划。在这种情况下,无论分配了多少资源,您的任务都将具有固定的持续时间:
Dim task As Task = projectfile.addTask
task.setName("Example Task")
task.setDuration(Duration.getInstance(5, TimeUnit.DAYS))
task.setEffortDriven(False)