Ajout de liens pour les tâches

La liaison des tâches est souvent requise en travaillant avec des fichiers Microsoft Project (MPP / XML). Dans VSTO, TaskDependces.Add () est utilisé pour ajouter des liens de tâche; Aspose.Tasks utilise project.tasklinks.add () pour lier les tâches.

VSTO donne accès à ces liens à l’aide de msproject.project.tasks.get_unitiqueid (taskID) .TaskDependces. Il fournit une collection de dépendances qui peuvent être utilisées pour afficher et autrement traiter les exigences comme l’accès à des propriétés détaillées.

Dans Aspose.Tasks pour .NET Project.TaskLinks fournit une collection de liens qui peuvent être utilisés pour l’affichage et l’accès aux propriétés détaillées de ces liens comme LinkType, PredTask, Succedask, etc.

Cet article montre comment charger des fichiers MPP dans votre application .NET et ajouter / afficher les tâches de liaison à l’aide de VSTO et Aspose.Tasks pour .NET.

Tâches de liaison à l’aide de Vsto

Pour lier une tâche à l’aide de VSTO:

  1. Créez un nouveau projet dans Visual Studio.

  2. Dans l’explorateur de solution, cliquez avec le bouton droit et sélectionnez Ajouter une référence.

  3. Sélectionnez l’onglet COM Composants et sélectionnez Microsoft Project 12.0 Bibliothèque d’objets.

  4. Cliquez sur OK.

Cela importe l’espace de noms Microsoft.office.interop.msproject au début de votre code. Utilisez le code à partir de l’exemple suivant pour lier les tâches.

 1Microsoft.office.interop.msproject.application projectApplication = new Application ();
 2
 3objet manquantValue = System.Reflection.Missing.Value;
 4
 5projectApplication.FileOPenex (@ "D: \ Aspose \ Migration \ Sampleproject.mp",
 6    missingValue, missingValue, missingValue, missingValue,
 7
 8    missingValue, missingValue, missingValue, missingValue,
 9
10    missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
11
12    missingValue, missingValue, missingValue, missingValue,
13
14
15
16
17Microsoft.Office.Interop.MSProject.Project project = projectApplication.ActiveProject;
18
19project.Tasks.get_UniqueID(2).TaskDependencies.Add(project.Tasks.get_UniqueID(1), PjTaskLinkType.pjFinishToStart);
20
21project.Tasks.get_UniqueID(3).TaskDependencies.Add(project.Tasks.get_UniqueID(2), PjTaskLinkType.pjFinishToStart);
22
23project.Tasks.get_UniqueID(4).TaskDependencies.Add(project.Tasks.get_UniqueID(3), PjTaskLinkType.pjFinishToStart);
24
25project.Tasks.get_UniqueID(5).TaskDependencies.Add(project.Tasks.get_UniqueID(4), PjTaskLinkType.pjFinishToStart);
26
27project.Tasks.get_UniqueID(5).TaskDependencies.Add(project.Tasks.get_UniqueID(2), PjTaskLinkType.pjFinishToStart);
28
29// Display the dependencies
30
31foreach (Task tsk in project.Tasks)
32
33{
34
35    foreach (TaskDependency dep in project.Tasks.get_UniqueID(tsk.ID).TaskDependencies)
36
37    {
38
39        Console.WriteLine("From ID = " + dep.From.ID  + "=>To ID = " + dep.To.ID);
40
41    }
42
43    Console.WriteLine("____________________________________________________________");
44
45}
46
47// Save the project
48
49projectApplication.FileCloseAll(Microsoft.Office.Interop.MSProject.PjSaveType.pjSave);

To link tasks in a project using Aspose.Tasks for .NET:

  1. Create a new project in Visual Studio.

  2. In the Solution Explorer, right-click and select Add Reference.

  3. Select .NET tab and select Aspose.Tasks.

  4. Click OK.

This imports the Aspose.Tasks namespace at the start of your code. Use the code from the following example to link tasks.

 1Project project = new Project("New Project.mpp");
 2
 3Task task1 = project.RootTask.Children.GetById(1);
 4Task task2 = project.RootTask.Children.GetById(2);
 5Task task3 = project.RootTask.Children.GetById(3);
 6Task task4 = project.RootTask.Children.GetById(4);
 7Task task5 = project.RootTask.Children.GetById(5);
 8
 9// Link the tasks
10TaskLink taskLink = project.TaskLinks.Add(task1, task2, TaskLinkType.FinishToStart);
11taskLink = project.TaskLinks.Add(task2, task3, TaskLinkType.FinishToStart);
12taskLink = project.TaskLinks.Add(task3, task4, TaskLinkType.FinishToStart);
13taskLink = project.TaskLinks.Add(task4, task5, TaskLinkType.FinishToStart);
14taskLink = project.TaskLinks.Add(task2, task5, TaskLinkType.FinishToStart);
15
16// Display links among the tasks
17TaskLinkCollection allinks = project.TaskLinks;
18foreach (TaskLink link in allinks)
19{
20    Console.WriteLine("From ID = " + link.PredTask.Get(Tsk.Id) + " => To ID = " + link.SuccTask.Get(Tsk.Id));
21    Console.WriteLine("________________________________________");
22}
23 
24project.Save("LinkTasks_out.mpp", SaveFileFormat.MPP);
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.