主要内容

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

mlreportgen.dom.CustomElement 类

命名空间: mlreportgen.dom

文档的自定义元素

描述

使用 mlreportgen.dom.CustomElement 类的实例扩展 DOM API。您可以创建自定义 HTML 或 Microsoft® Word 元素,以提供 DOM API 中尚未包含的功能。MATLAB® Report Generator™ 不会检查您的自定义元素是否有效。

要了解可以将 mlreportgen.dom.CustomElement 对象追加到哪些 DOM 对象,请参阅将 mlreportgen.dom.CustomElement 对象追加到 DOM 类对象上

此类仅与以下 MATLAB Report Generator 输出类型兼容:

  • "docx"

  • "html"

  • "html-file"

  • "html-multipage" (自 R2024a 起)

构造

customElementObj = CustomElement 创建一个空元素。

customElementObj = CustomElement(name) 创建一个具有指定 name 的自定义元素。

输入参量

全部展开

此自定义元素所追加到的文档类型所支持的元素的名称。例如,为自定义 HTML div 元素指定 "div",或为自定义 Word 段落元素指定 "w:p"

输出参量

全部展开

自定义元素,由 mlreportgen.dom.CustomElement 对象表示。

属性

全部展开

自定义元素名称,指定为字符向量或字符串标量。

属性:

GetAccess
public
SetAccess
public
NonCopyable
true

数据类型: char

该类忽略此属性。

属性:

GetAccess
public
SetAccess
public
NonCopyable
true

该类忽略此属性。

属性:

GetAccess
public
SetAccess
public
NonCopyable
true

文档元素的自定义属性,指定为 mlreportgen.dom.CustomAttribute 对象数组。自定义属性必须被此对象所追加到的文档元素的输出格式支持。

属性:

GetAccess
public
SetAccess
public
NonCopyable
true

标记,指定为字符向量或字符串标量。DOM API 在创建此对象的过程中生成一个会话唯一标记。生成的标记形式为 CLASS:ID,其中 CLASS 是对象类,ID 是对象的 Id 属性的值。使用此值来帮助确定在文档生成过程中出现的问题的位置。

属性:

GetAccess
public
SetAccess
public
NonCopyable
true

数据类型: char | string

目标标识符,指定为字符向量或字符串标量。DOM API 在创建文档元素对象时会生成一个会话唯一标识符。

属性:

GetAccess
public
SetAccess
public
NonCopyable
true

数据类型: char | string

方法

方法

用途

append

appendedContent = append(classObj,contentToAppend) 将一个有效的 DOM 对象 contentToAppend 追加到 mlreportgen.dom.CustomElement 对象上。

输入参量

  • classObj - 要追加内容的类对象,指定为 mlreportgen.dom.CustomElement 对象。

  • contentToAppend - 要追加的内容,应指定为有效的 DOM 对象。

返回值

  • appendedContent - 追加的内容,以 DOM 对象形式返回。

如需查看可作为 classObj 参量使用的 DOM 类对象列表,请参阅“更多信息”部分。

有关信息,请参阅 mlreportgen.dom.Document 类中的等效方法 append

clone

copiedObj = clone(soureceObj) 返回 soureceObj 指定的 mlreportgen.dom.CustomElement 对象的副本。副本包含源对象的子级,但不包含父级。

注意

请勿将同一对象多次追加到文档中。当您希望在文档的其他位置重用相同内容时,请使用克隆功能创建副本。

输入参量

  • soureceObj - 要克隆的对象,指定为 mlreportgen.dom.CustomElement 对象

返回值

  • copiedObj - 对象副本,以 mlreportgen.dom.CustomElement 对象形式返回

有关详细信息,请参阅 mlreportgen.dom.Paragraph 类的等效方法 clone

示例

全部折叠

此示例显示如何在 HTML 报告中添加提供复选框的自定义元素。

创建自定义元素并向其追加文本。

import mlreportgen.dom.*;
d = Document('test');

input1 = CustomElement('input');
input1.CustomAttributes = { 
         CustomAttribute('type','checkbox'), ...
         CustomAttribute('name','vehicle'), ...
         CustomAttribute('value','Bike'), ...
         };
append(input1, Text('I have a bike'));

将自定义元素追加到有序列表并显示报告。

ol = OrderedList({input1});
append(d,ol);

close(d);
rptview(d.OutputPath);

此示例使用 mlreportgen.dom.CustomElementmlreportgen.dom.CustomAttribute 对象生成在 Word 文档中显示复选框控件的 Open Office XML (OOXML) 标记。有关详细信息,请参阅 Office Open XML 网站上的欢迎使用 Office Open XML SDK

导入 DOM API 命名空间。

import mlreportgen.dom.*;

使用 mlreportgen.dom.CustomElement 类的对象为复选框控件和 SDT 属性元素创建结构化文档标记 (SDT) 块级容器。

cbBlock = CustomElement("w:sdt");
cbBlockProps = CustomElement("w:stdPr");

initStateinitStateChar 变量设置复选框的初始状态。在此示例中,我们通过设置 initState="0"initStateChar="☐" 将复选框的初始状态设置为“未选中”。如果希望复选框的初始状态为“选中”,请设置 initState="1"initStateChar="☒"

initState = "0";
initStateChar = "☐";

创建一个复选框控制元素和一个复选框状态元素,然后将复选框状态元素追加到复选框控制元素。

cbControl = CustomElement("w14:checkbox"); 
cbState = CustomElement("w14:checked");
cbState.CustomAttributes = {CustomAttribute("w14:val",initState)};
append(cbControl,cbState);

指定字体系列和字符来呈现选中的复选框。

cbCheckedState = CustomElement("w14:checkedState");
cbCheckedState.CustomAttributes = { ...
            CustomAttribute("w14:val","2612"),... 
            CustomAttribute("w14:font","MS Gothic")};
append(cbControl,cbCheckedState);

指定字体系列和字符来呈现未选中的复选框。

cbUncheckedState = CustomElement("w14:uncheckedState");
cbUncheckedState.CustomAttributes = { ...
            CustomAttribute("w14:val","2610"),...
            CustomAttribute("w14:font","MS Gothic")};
append(cbControl,cbUncheckedState);

将复选框控件追加到 SDT 属性元素。

append(cbBlockProps,cbControl);

将复选框控件属性追加到 SDT 元素。

append(cbBlock,cbBlockProps);

追加一个元素以指示 SDT 元素的属性部分的结束。

append(cbBlock,CustomElement("w:stdEndPr"));

创建一个块级容器,指定复选框的初始状态和特性,然后将复选框元素追加到该容器中。

cbBlockContent = CustomElement("w:stdContent");
textRange = CustomElement("w:r"); % text-block element
append(textRange,Text(initStateChar));
append(cbBlockContent,textRange);
append(cbBlock,cbBlockContent);

创建一个 mlreportgen.dom.Document 对象,然后将标题追加到文档对象。

wordDoc = Document("worddoc-w-checkbox","docx");
docTitle = Text(...
  "Using CustomElement objects to create a check box" + ...
  "in a Microsoft® Word Document",...
  "Title");
docTitle.FontSize = "12pt";
append(wordDoc,docTitle);

创建一个 mlreportgen.dom.Paragraph 对象,然后将复选框元素追加到段落对象。

para = Paragraph();
append(para,cbBlock);

将文本追加到段落对象,然后将段落对象添加到文档对象。

checkBoxStr = Text(" This is my check box");
checkBoxStr.WhiteSpace = "pre"; % Preserve the white spaces
append(para,checkBoxStr);
append(wordDoc,para);

关闭用于生成报告的文档对象,然后打开该报告。

close(wordDoc);
rptview(wordDoc);

此示例显示如何在 HTML 报告中添加提供复选框的自定义元素。

创建自定义元素并向其追加文本。

import mlreportgen.dom.*;
d = Document("test");

input1 = CustomElement("input");
input1.CustomAttributes = { 
         CustomAttribute("type", "checkbox"), ...
         CustomAttribute("name", "vehicle"), ...
         CustomAttribute("value", "Bike"), ...
         };
append(input1, Text("I have a bike"));

将自定义元素追加到有序列表并显示报告。

ol = OrderedList({input1});
append(d,ol);

close(d);
rptview("test","html");

详细信息

全部展开

版本历史记录

在 R2014b 中推出