You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sparx EA 16脚本实现多过滤条件文档生成(无需多报告包)

在Sparx EA 16中通过脚本复用报告包生成多类型文档

完全可以做到不用新建三个报告包,通过VBScript脚本就能动态修改模板片段的SQL过滤条件,批量生成针对Tunnel、Barrage的文档,同时保留原Bridge的配置。下面是具体实现思路和示例脚本:

核心思路

  1. 定位你已创建的报告包和对应的模型文档
  2. 找到模型文档关联模板里的目标SQL片段
  3. 动态替换SQL中的Where Name=Bridge为Where Name=Tunnel或Where Name=Barrage
  4. 调用EA的文档生成接口输出对应文档
  5. (可选)恢复原SQL条件,避免影响后续正常使用

示例VBScript脚本

' 连接到当前EA实例
Dim eaApp
Set eaApp = CreateObject("EA.App")
Dim rep
Set rep = eaApp.Repository

' 配置参数:替换成你的报告包GUID、模型文档名称、模板片段名称
Const REPORT_PACKAGE_GUID = "{你的报告包GUID}"
Const MODEL_DOC_NAME = "你的模型文档名称"
Const TEMPLATE_FRAGMENT_NAME = "你的SQL片段名称"
Const OUTPUT_FOLDER = "C:\你的输出路径\"

' 要生成的目标对象类型数组
Dim targetNames
targetNames = Array("Bridge", "Tunnel", "Barrage")

' 获取报告包
Dim reportPackage
Set reportPackage = rep.GetPackageByGuid(REPORT_PACKAGE_GUID)
If reportPackage Is Nothing Then
    MsgBox "找不到指定的报告包"
    Exit Sub
End If

' 获取模型文档
Dim modelDoc
Set modelDoc = Nothing
Dim doc
For Each doc In reportPackage.Elements
    If doc.Type = "Model Document" And doc.Name = MODEL_DOC_NAME Then
        Set modelDoc = doc
        Exit For
    End If
Next
If modelDoc Is Nothing Then
    MsgBox "找不到指定的模型文档"
    Exit Sub
End If

' 获取模型文档关联的模板
Dim template
Set template = rep.GetDocumentTemplate(modelDoc.DocumentTemplate)
If template Is Nothing Then
    MsgBox "模型文档未关联有效模板"
    Exit Sub
End If

' 获取模板中的目标SQL片段
Dim fragment
Set fragment = Nothing
Dim frag
For Each frag In template.Fragments
    If frag.Name = TEMPLATE_FRAGMENT_NAME Then
        Set fragment = frag
        Exit For
    End If
Next
If fragment Is Nothing Then
    MsgBox "找不到指定的SQL片段"
    Exit Sub
End If

' 保存原SQL语句,用于后续恢复
Dim originalSQL
originalSQL = fragment.SQLQuery

' 遍历目标类型,生成文档
Dim targetName
For Each targetName In targetNames
    ' 替换SQL中的过滤条件
    Dim newSQL
    newSQL = Replace(originalSQL, "Where Name=Bridge", "Where Name=" & targetName)
    fragment.SQLQuery = newSQL
    template.Update ' 更新模板
    
    ' 生成文档,文件名包含目标类型
    Dim outputPath
    outputPath = OUTPUT_FOLDER & modelDoc.Name & "_" & targetName & ".docx"
    ' 调用EA生成文档的方法,参数根据需要调整格式(0=Word,1=PDF等)
    modelDoc.DocumentGenerator outputPath, 0, 0, 0
    
    MsgBox targetName & " 文档已生成:" & outputPath
Next

' 恢复原SQL语句,避免影响后续使用
fragment.SQLQuery = originalSQL
template.Update

MsgBox "所有文档生成完成"

注意事项

  • 替换脚本中的REPORT_PACKAGE_GUID、MODEL_DOC_NAME等参数为你实际的项目值
  • 确保在EA脚本编辑器(Tools > Scripting > Script Editor)中运行此脚本,或者保存为VBS文件后通过EA的脚本菜单执行
  • 若SQL语句中Name字段带有单引号,要调整替换逻辑(比如原SQL是Where Name='Bridge',替换时要改成Where Name='Tunnel')
  • 文档生成格式参数可按需修改:0=Word(.docx),1=PDF,2=RTF,3=HTML等

内容的提问来源于stack exchange,提问作者Bas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 01:19:54