0

我在尝试使用库 MPXJ 将资源分配给任务和子任务时遇到问题。实际上,当我将资源分配给子任务时,所有任务持续时间都被修改了,我不明白为什么。如果我不分配资源,则持续时间设置得很好(每个子任务 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")

谢谢!

4

1 回答 1

1

如果您将资源分配添加到任务,您还需要设置该任务的工作量。

例如,如果您有一个 5 天的任务,没有资源分配,则为该任务配置开始日期和 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 天内完成。这是工作量驱动的调度,如此所述。

如果您愿意,您可以逐个任务关闭工作量驱动的调度。在这种情况下,无论分配了多少资源,您的任务都将具有固定的持续时间:

Dim task As Task = projectfile.addTask
task.setName("Example Task")
task.setDuration(Duration.getInstance(5, TimeUnit.DAYS))
task.setEffortDriven(False)
于 2014-09-17T10:26:18.383 回答