主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

通过编程修改模板

如果您需要更新现有文档对象模型 (DOM) API 模板的文档部件,您可以根据模板所需的更改类型从几种方法中进行选择。如果更改很小并且您可能不需要重复它们,请考虑在命令行上手动进行更改。对于更广泛的更改或可能需要重复执行的更改,请通过编写脚本来实现更改。无论采用何种方法,更新后的模板都包含源模板中所有未修改的文档部件。

修改模板的一些常见原因:

  • 添加额外的内容或空位或改变源样式

  • 用其他文本替换文本(尤其是在 PDF 中)

  • 除了添加额外内容或更改源样式之外,还进行了大量更改

您可以使用 tmplview 预览您的更改。 (自 R2024b 起)

通过从现有的 TemplateDocumentPart 创建新的 TemplateDocumentPart 来更新模板

要添加额外的内容或空位,或更改现有模板的样式,请创建一个新的 TemplateDocumentPart 对象来替换现有的 TemplateDocumentPart 对象。您可以替换相关文档部件,同时源模板的其余内容保持不变。

使用 tmplview 在源模板中显示模板文档部分 "partToModify"。本文件部分目前包含静态文本 "Text in template document part",随后是一个模板占位符 "templateHole"。在此示例中,您通过查找需要修改的部分、复制原始文本和模板孔,然后在模板孔前后添加额外文本,来在模板孔前后添加额外文本。

tmplview("existingTemplate","html",OpenDocument="partToModify");

tmplview window showing the contents of the partToModify template document part from the template existingTemplate.htmtx.

导入 DOM API 命名空间,这样您就不必使用完全限定的类名。

import mlreportgen.dom.*

通过复制现有模板创建一个新模板。

t = Template("updatedTemplate","html","existingTemplate");
open(t);

在文档部件中找到需要修改的部分。

toModifyIdx = strcmp({t.TemplateDocumentParts.Name},"partToModify");
toModifyPart = t.TemplateDocumentParts(toModifyIdx);

基于现有文档部件定义一个新的模板文档部件。

newTemplateDocPart = TemplateDocumentPart("partToModify");

将原始模板文档部分中的子级复制到新文档部分的模板空位前后位置。当您的 for 循环到达模板位置时,添加新文本的第一行,复制模板位置,然后添加新文本的第二行。

for child = toModifyPart.Children
    % If the child is a template hole
    if isa(child,"mlreportgen.dom.TemplateHole")
        switch child.HoleId       
            case "templateHole"
                append(newTemplateDocPart,...
                    Text("Text before template hole."));
                append(newTemplateDocPart,TemplateHole(child.HoleId));
                append(newTemplateDocPart,...
                    Text("Content after template hole"));
            otherwise
                append(newTemplateDocPart,TemplateHole(child.HoleId))
        end
        % If the child is not the template hole, copy the child to 
        % the new document part
    else  
        append(newTemplateDocPart,clone(child));
    end
end

用新的模板文档部分替换需要修改的部分。

t.TemplateDocumentParts(toModifyIdx) = newTemplateDocPart;

关闭模板。

close(t);

使用 tmplview 确认模板文档部分的更改。

tmplview("updatedTemplate.htmtx",OpenDocument="partToModify");

tmplview window showing the contents of the partToModify template document part from the template existingTemplate.htmtx.

要了解如何直接检查源文件或更新的模板文件,请参阅 打开模板文件

通过修改现有的 TemplateDocumentPart 对象来更新模板

要更新模板文档部件内的文本,同时保持源模板的其余部分不变,您可以修改现有的模板文档部件。此方法最适合 PDF 模板。

使用 tmplview 显示需要在源模板中修改的模板文档部分,"partToModify"。它目前包含静态文本 "Text in template document part",后面跟着一个模板空位 "templateHole"

tmplview("existingTemplate","html",OpenDocument="partToModify");

tmplview window showing the contents of the partToModify template document part from the template existingTemplate.htmtx.

导入 DOM API 命名空间,这样您就不必使用完全限定的类名。

import mlreportgen.dom.*

根据现有模板创建模板。

t = Template("updatedTemplate","html","existingTemplate");
open(t);

在模板的文档部件中找到需要修改的部件。

toModifyIdx = strcmp({t.TemplateDocumentParts.Name},"partToModify");
toModifyPart = t.TemplateDocumentParts(toModifyIdx);

修改文档部件以输出 PDF

如果子项是 Paragraph 对象,则将文本更新为 "Updated text in template part"

for child = toModifyPart.Children
    if isa(child,"mlreportgen.dom.Paragraph")
        child.Children(1).Content = "Updated text in template part";
    end
end

您还可以进行其他更改,例如更改样式和添加额外内容。例如,您可以使用 child.Style = [child.Style, {Bold}]; 将文本变为粗体。有关在模板中使用样式表的更多信息,请参阅使用样式表样式

修改文档部件以进行 HTML 输出

如果子项是 TemplateText 对象并且该对象中的 HTMLtext"Text in template part",则将文本更新为 "Updated text in template part"

for child = toModifyPart.Children
    if isa(child,"mlreportgen.dom.TemplateText")
        child.HTMLText = strrep(child.HTMLText,...
            "Text in template part","Updated text in template part");
    end
end

关闭模板。

close(t);

使用 tmplview 在更新后的模板中显示模板文档部分。它现在包含一个新的模板空位 "newTemplateHole" ,后面跟着一个 2×2 的表。

tmplview("updatedTemplate","html",OpenDocument="partToModify");

tmplview window showing the contents of the partToModify template document part from the new template updatedTemplate.htmtx.

要了解如何直接检查源文件或更新的模板文件,请参阅 打开模板文件

通过创建新的 TemplateDocumentPart 对象来更新模板

通过创建新的 TemplateDocumentPart 对象来更新模板,以向源模板添加附加内容。这是对源模板进行重大更改的好方法。

使用 tmplview 显示需要在源模板中修改的模板文档部分,"partToModify"。它目前包含静态文本 "Text in template document part",后面跟着一个模板空位 "templateHole"

tmplview("existingTemplate","html",OpenDocument="partToModify");

tmplview window showing the contents of the partToModify template document part from the template existingTemplate.htmtx.

导入 DOM API 命名空间,这样您就不必使用完全限定的类名。

import mlreportgen.dom.*

根据现有模板创建模板。

t = Template("updatedTemplate", "html", "existingTemplate");
open(t);

在模板的文档部件中找到需要修改的部件。

toModifyIdx = strcmp({t.TemplateDocumentParts.Name}, "partToModify");
toModifyPart = t.TemplateDocumentParts(toModifyIdx);

创建一个新的模板文档部件。

newTemplateDocPart = TemplateDocumentPart("partToModify");

将新内容追加到模板文档部件。

append(newTemplateDocPart, ...
    TemplateHole("newTemplateHole", "Completely new template hole"));
append(newTemplateDocPart, Table([1, 2; 3, 4]));

用新的模板文档部件替换旧的模板文档部件。

t.TemplateDocumentParts(toModifyIdx) = newTemplateDocPart;

关闭模板。

close(t);

使用 tmplview 在更新后的模板中显示模板文档部分。它现在包含一个新的模板空位 "newTemplateHole" ,后面跟着一个 2×2 的表。

tmplview("updatedTemplate","html",OpenDocument="partToModify");

tmplview window showing the contents of the partToModify template document part from the new template updatedTemplate.htmtx.

要了解如何直接检查源文件或更新的模板文件,请参阅 打开模板文件

另请参阅

| | |

主题