Travailler avec des attributs étendus d'un projet
Microsoft Project dispose d’un vaste schéma d’interchange de données XML qui facilite l’échange d’informations entre les applications et la programmation avec des fichiers de projet. Le schéma vous permet d’ajouter des attributs étendus aux tâches, aux ressources et aux affectations. Cet article montre comment travailler avec des attributs étendus dans Aspose.Tasks.
Travailler avec des champs personnalisés à l’aide du projet Microsoft
Dans cet exemple, nous montrerons comment travailler avec Text1 Extended Attribut d’un projet pour associer les informations à une tâche.
- Dans MSP, accédez à Project-> Fields personnalisés.
- De là, sélectionnez la tâche.
- Sélectionnez le texte en tant que type de champ personnalisé dans la boîte combinée de type.
- Sélectionnez Text1 comme champ personnalisé avec lequel vous souhaitez travailler
- Utilisez le bouton “Renommer” pour renommer l’alias du champ si elle est souhaitée et appuyez sur le bouton OK
- Ajoutez une nouvelle tâche et insérez une nouvelle colonne à la ligne de tâche avec le champ personnalisé que vous avez utilisé dans l’étape ci-dessus
Travailler avec des champs personnalisés / attributs étendus en utilisant Aspose.Tasks pour .net
Aspose.Tasks pour .NET L’API offre la capacité de créer de nouveaux attributs étendus ainsi que de travailler avec des attributs étendus déjà présents dans un document. Les champs personnalisés ou les attributs étendus sont représentés par la collection ExtendAttributes d’un projet dans Aspose.Tasks. Il contient toute la définition des attributs étendus d’un document de projet. Certains des mappages de la définition du champ personnalisé MSP sont comme indiqué dans l’image ci-dessous.
Création d’un nouvel attribut étendu et l’ajout à la tâche
Pour ajouter un nouvel attribut étendu pour la tâche ou la ressource, nous devons d’abord définir et ajouter la définition d’attribut étendu à la collection ExtendAttributes. ExtendedAttributeDefinition La classe est utilisée pour définir un nouveau prolongation d’extension dans un projet. Le FieldID doit être défini pour la définition appropriée d’un attribut étendu qui est lié à ExtendedAttributeTask (en cas de tâche) ou ExtendedAttributeResource (en cas de ressource). L’exemple de code suivant montre comment définir un nouvel attribut étendu pour le champ de projet text1. Une fois la définition d’attribut étendue terminée, vous pouvez désormais créer un nouvel attribut étendu et l’attribuer à une tâche.
1Project project = new Project("New Project.mpp");
2
3ExtendedAttributeDefinition myTextAttributeDefinition = project.ExtendedAttributes.GetById((int)ExtendedAttributeTask.Text1);
4
5// If the Custom field doesn't exist in Project, create it
6if (myTextAttributeDefinition == null)
7{
8 myTextAttributeDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Text1, "My text field");
9 project.ExtendedAttributes.Add(myTextAttributeDefinition);
10}
11
12// Generate Extended Attribute from definition
13ExtendedAttribute text1TaskAttribute = myTextAttributeDefinition.CreateExtendedAttribute();
14
15text1TaskAttribute.TextValue = "Text attribute value";
16
17// Add extended attribute to task
18Task task = project.RootTask.Children.Add("Task 1");
19task.ExtendedAttributes.Add(text1TaskAttribute);
20
21project.Save("CreateExtendedAttributes_out.mpp", SaveFileFormat.MPP);
Writing Updated Extended Attribute Definitions and Values to MPP
Aspose.Tasks for .NET supports updating extended attribute data in a Microsoft Project MPP file and save it back.
The example code below adds new extended attributes of the Resource and Task types to the source MPP file. The steps involved in this activity are:
- Create an instance of Project Reader.
- Read the source MPP file.
- Define a new extended attribute and update its values.
- Save the project using the Project Writer.
The following example shows setting the extended attributes of a resource.
1Project project = new Project("New Project.mpp");
2
3// Add new text3 extended attribute with lookup and one lookup value
4ExtendedAttributeDefinition taskTextAttributeDefinition = ExtendedAttributeDefinition.CreateLookupTaskDefinition(ExtendedAttributeTask.Text3, "New text3 attribute");
5taskTextAttributeDefinition.ElementType = ElementType.Task;
6project.ExtendedAttributes.Add(taskTextAttributeDefinition);
7
8Value textVal = new Value();
9textVal.Id = 1;
10textVal.Description = "Text value descr";
11textVal.Val = "Text value1";
12
13taskTextAttributeDefinition.AddLookupValue(textVal);
14
15// Add new cost1 extended attribute with lookup and two cost values
16ExtendedAttributeDefinition taskCostAttributeDefinition = ExtendedAttributeDefinition.CreateLookupTaskDefinition(ExtendedAttributeTask.Cost1, "New cost1 attribute");
17project.ExtendedAttributes.Add(taskCostAttributeDefinition);
18
19Value costVal1 = new Value();
20costVal1.Id = 2;
21costVal1.Description = "Cost value 1 descr";
22costVal1.Val = "99900";
23
24Value costVal2 = new Value();
25costVal2.Id = 3;
26costVal2.Description = "Cost value 2 descr";
27costVal2.Val = "11100";
28
29taskCostAttributeDefinition.AddLookupValue(costVal1);
30taskCostAttributeDefinition.AddLookupValue(costVal2);
31
32// Add new task and assign attribute lookup value.
33Task task = project.RootTask.Children.Add("New task");
34
35ExtendedAttribute taskAttr = taskCostAttributeDefinition.CreateExtendedAttribute(costVal1);
36task.ExtendedAttributes.Add(taskAttr);
37
38ExtendedAttributeDefinition taskStartAttributeDefinition = ExtendedAttributeDefinition.CreateLookupTaskDefinition(ExtendedAttributeTask.Start7, "New start 7 attribute");
39
40Value startVal = new Value();
41startVal.Id = 4;
42startVal.DateTimeValue = DateTime.Now;
43startVal.Description = "Start 7 value description";
44
45taskStartAttributeDefinition.AddLookupValue(startVal);
46
47project.ExtendedAttributes.Add(taskStartAttributeDefinition);
48
49ExtendedAttributeDefinition taskFinishAttributeDefinition = ExtendedAttributeDefinition.CreateLookupTaskDefinition(ExtendedAttributeTask.Finish4, "New finish 4 attribute");
50
51Value finishVal = new Value();
52finishVal.Id = 5;
53finishVal.DateTimeValue = DateTime.Now;
54finishVal.Description = "Finish 4 value description";
55
56taskFinishAttributeDefinition.ValueList.Add(finishVal);
57
58project.ExtendedAttributes.Add(taskFinishAttributeDefinition);
59
60ExtendedAttributeDefinition numberAttributeDefinition = ExtendedAttributeDefinition.CreateLookupTaskDefinition(ExtendedAttributeTask.Number20, "New number attribute");
61
62Value val1 = new Value();
63val1.Id = 6;
64val1.Val = "1";
65val1.Description = "Number 1 value";
66Value val2 = new Value();
67val2.Id = 7;
68val2.Val = "2";
69val2.Description = "Number 2 value";
70Value val3 = new Value();
71val2.Id = 8;
72val3.Val = "3";
73val3.Description = "Number 3 value";
74
75numberAttributeDefinition.AddLookupValue(val1);
76numberAttributeDefinition.AddLookupValue(val2);
77numberAttributeDefinition.AddLookupValue(val3);
78
79project.ExtendedAttributes.Add(numberAttributeDefinition);
80
81ExtendedAttributeDefinition resourceStartAttributeDefinition = ExtendedAttributeDefinition.CreateLookupResourceDefinition(ExtendedAttributeResource.Start5, "New start5 attribute");
82
83Value startVal2 = new Value();
84startVal2.Id = 9;
85startVal2.DateTimeValue = DateTime.Now;
86startVal2.Description = "this is start5 value descr";
87
88resourceStartAttributeDefinition.AddLookupValue(startVal2);
89
90project.ExtendedAttributes.Add(resourceStartAttributeDefinition);
91
92// Define a duration attribute without lookup.
93ExtendedAttributeDefinition taskDurationAttributeDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Duration1, "New Duration");
94project.ExtendedAttributes.Add(taskDurationAttributeDefinition);
95
96// Add new task and assign duration value to the previously defined duration attribute.
97Task timeTask = project.RootTask.Children.Add("New task");
98
99ExtendedAttribute durationExtendedAttribute = taskDurationAttributeDefinition.CreateExtendedAttribute();
100
101durationExtendedAttribute.DurationValue = project.GetDuration(3.0, TimeUnitType.Hour);
102timeTask.ExtendedAttributes.Add(durationExtendedAttribute);
103
104MPPSaveOptions mppSaveOptions = new MPPSaveOptions();
105mppSaveOptions.WriteViewData = true;
106
107project.Save("WriteUpdatedExtendedAttributeDefinitions_out.mpp", mppSaveOptions);