# informat.mpp Mpp文件

## 概述

使用`informat.mpp`执行mpp文件的读写操作

## createProjectFile

创建Mpp项目文件对象

```javascript
informat.mpp.createProjectFile()
```

**返回值**

类型为`MppProjectFile`

## read

从本地沙盒中读取mpp文件,如果文件不存在将会抛出异常

```javascript
informat.mpp.read(file)
```

| 参数   | 类型     | 描述                |
|------|--------|-------------------|
| file | String | app的沙盒环境中的MPP文件地址 |

**返回值**

类型为`MppProjectFile`

**示例**

```js
let projectFile = informat.mpp.read('软件开发.mpp');
var resourceAssignments = projectFile.getResourceAssignments();
resourceAssignments.list().forEach(as => {
    if (as.getTask() == null || as.getResource() == null) {
        return;
    }
    var taskId = as.getTask().getID();
    var resourceName = as.getResource().getName();
    console.log('taskId:' + taskId + ',resourceName:' + resourceName);
});
var tableContainer = projectFile.getTables();
if (tableContainer != null && tableContainer.size() > 0) {
    var columns = tableContainer.list().get(0).getColumns();
    columns.forEach(c => {
        console.log('column:' + c.getTitle());
    });
}
var tasks = projectFile.getTasks();
tasks.list().forEach(task => {
    console.log('taskId:' + task.getID() + ',taskName:' + task.getName());
});
```

## write

将projectFile数据写入xml文件

```js
informat.mpp.write(projectFile, targetFile, config)
```

| 参数          | 类型                                                        | 描述             |
|-------------|-----------------------------------------------------------|----------------|
| projectFile | `MppProjectFile`                                          | project文件对象    |
| targetFile  | `String`                                                  | app的沙盒环境中的文件地址 |
| config      | [MppWriterConfig](/guide/script/model.md#mppwriterconfig) | 配置文件           |

```js
var file = informat.mpp.createProjectFile();
var pmResource = file.addResource();
pmResource.setName("项目经理");
var devResource = file.addResource();
devResource.setName("开发人员");
var customFieldContainer = file.getCustomFields();
var field = customFieldContainer.getOrCreate('TEXT1');
field.setAlias("MyCustomField");
var task1 = file.addTask();
task1.setName("root");
task1.setTaskMode('MANUALLY_SCHEDULED');
task1.setStart(new Date());
task1.setFinish(new Date(2024, 3, 1));
task1.setText(1, '测试1')
task1.addResourceAssignment(pmResource);
task1.addResourceAssignment(devResource);
//
var task12 = task1.addTask();
task12.setName("任务A");
task12.setTaskMode('AUTO_SCHEDULED');
task12.setStart(new Date());
task12.setFinish(new Date(2024, 2, 1));
task12.setText(1, "text1");
task12.addResourceAssignment(devResource);
//
task1.addPredecessor(task12, null, null);
task12.addPredecessor(task1, null, null);
//
var task2 = file.addTask();
task2.setName("任务B");
task2.setTaskMode('MANUALLY_SCHEDULED');
task2.setStart(new Date(2024, 4, 1));
task2.setFinish(new Date(2024, 5, 1));
//
var config = {
    microsoftProjectCompatibleOutput: false,
    splitTimephasedAsDays: true,
    writeTimephasedData: false,
    saveVersion: "Project2016"
}
informat.mpp.write(file, 'output.xml', config);
```

::: info 参考文档：
[https://www.mpxj.org/apidocs/index.html](https://www.mpxj.org/apidocs/index.html)
:::

