用户指南 > 关于用户指南 > Word 和 Excel 模板 > Excel 模板 > 使用自定义模板将工作项的层次结构导出到 Excel
使用自定义模板将工作项的层次结构导出到 Excel
如果要从互为父项或子项的项执行 Excel 导出,则需形成一个层次结构。
例如:
然后,您希望得到此结果:
Codebeamer 7.10 或更高版本
与早期版本中的自定义模板相比,Codebeamer 7.10 中的自定义模板更加简单,因为添加了一些 Helper 类,更易于执行此任务。此模板包含所有必要的代码:excelWithHierarchy710.xlsx
此模板中的脚本片段:
调用 ExcelHierarchyCellDecorator 会将必要的 Excel 缩进添加到要导出的单元格中:

<cb:groovy template="false" silent="false">
com.intland.codebeamer.text.excel.ExcelHierarchyCellDecorator.indentCell(cell, hierarchyProvider, item);

return item.name;
</cb:groovy>
C 列中的调用:${hierarchyProvider.getLevel(item)} 返回父子层次结构中的项的级别/深度。
D 列中的调用:${hierarchyProvider.getPath(item)} 将打印出此层次结构中的项路径,如 1.3.5。
Codebeamer 的早期版本
如果要从父项/子项之间彼此进行 Excel 导出,从而形成层次结构,则可以使用附加的 Excel 模板作为示例:excelWithHierarchy.xlsx
* 
需要注意的是:在 Codebeamer 7.10 版本之前,此导出模板无法使用 Excel 的原生缩进正确设置缩进,并且此缩进无法导入回 Codebeamer
要解决此问题,您需要使用 Codebeamer 7.10,或者必须下载 ooxml-schemas.jar 并手动将其放入类路径
模板使用 groovy 脚本,用于计算导出项的深度。可在 Excel 工作表中找到下面提供的脚本:

<cb:groovy template="false" silent="false">
// the command contains exported items and their hierarchy as a map:
// this is a SortedMap of ReleaseId,TrackerItemDto
releaseIdToReq = exportCommand.getRequirements();

//find the ReleaseId contains the depth
releaseId = null;
releaseIdToReq.each({
if (it.value == item) { releaseId = it.key };
});

// the depth/level of the item can be get from the releaseId
depth = 0;
if (releaseId != null) {
depth = releaseId.getNumbers().size();
}

name = item.name;
try {
// indent the name with as many spaces as many the depth is
// caching style in StyleRegistry to avoid too many styles created
styleRegistry = com.intland.codebeamer.text.excel.StyleRegistry.getInstance(workbook);
styleName = "indented" + depth ;
indentedStyle = styleRegistry.get(styleName);
if (indentedStyle == null) {
workbook = cell.sheet.workbook;
indentedStyle = workbook.createCellStyle();
indentedStyle.cloneStyleFrom(cell.getCellStyle());
styleRegistry.put(styleName, indentedStyle);

// set indent on style
indentedStyle.indention = depth - 1;
}
cell.setCellStyle(indentedStyle);
} catch (Throwable th) {
//log.warn("You will need ooxml-schemas.jar on classpath to make the indentation correct", th);
// just indent with spaces
if (depth > 1) {
1.upto(depth) { name = " " + name; }
}
}

return name;
</cb:groovy>
这对您有帮助吗?