Main Content

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

mlreportgen.dom.StyleRef 类

命名空间: mlreportgen.dom

用于引用具有指定样式名称或大纲级别的内容的占位符

描述

为具有指定样式名称或大纲级别的内容的引用创建占位符。此对象适用于 Word 和 PDF 报 告。

对于 Microsoft® Word 文档,您可以将 StyleRef 对象追加到页眉、页脚或正文中。对于 PDF,您只能将 StyleRef 对象追加到页眉或页脚。

提示

使用 StyleRef 对象在文档中创建页眉和页脚。例如,您可以使用此对象在页眉中添加当前章节的标题。

mlreportgen.dom.StyleRef 类是一个 handle 类。

创建对象

描述

styleref = StyleRef 创建对最接近此对象的段落内容的引用,该对象的 OutlineLevel 属性为 1

在 Word 输出的页眉中,最近的段落是当前页面上具有指定大纲级别的第一个段落。如果当前页面上没有这样的段落,则最近的段落是当前页面之前或之后页面上具有指定大纲级别的第一个段落。

在 Word 输出的页脚中,最近的段落是当前页面上具有指定大纲级别的最后一个段落。如果当前页面上没有这样的段落,则最近的段落是当前页面之前或之后页面上具有指定大纲级别的第一个段落。

PDF 输出的页眉和页脚中,最近的段落是当前页面的第一个段落,或当前页面布局部分中当前页面之前或之后的页面上的第一个段落。

styleref = StyleRef(num) 创建对最接近此对象的段落内容的引用,该段落的 OutlineLevel 属性具有指定的级别。

styleref = StyleRef(styleName) 创建对最接近此对象且具有指定样式名称的段落内容的引用。

示例

输入参量

全部展开

引用的标题对象的级别,指定为正整数。

要引用的对象样式的名称,指定为字符向量。

属性

全部展开

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

属性:

NonCopyable
true

应用于参考的样式表样式,指定为字符向量。

此文档元素对象的格式规范,指定为格式对象数组。此属性指定的格式将覆盖此元素的 StyleName 属性指定的相应格式。不适用于此元素的格式将被忽略。

属性:

NonCopyable
true

数据类型: cell

样式名称,指定为字符向量或字符串标量。样式名称是此元素所追加到的文档或文档部件的样式表中指定的样式的名称。指定的样式定义了此元素在输出文档中的外观,除非由此元素的 Style 属性指定的格式覆盖。要了解有关使用样式表的更多信息,请参阅 使用样式表样式

注意

Microsoft Word 输 出忽略样式名称。

属性:

NonCopyable
true

数据类型: char | string

mlreportgen.dom.StyleRef 对象的父级,指定为文档元素对象。一个文档元素必须只有一个父元素。

属性:

SetAccess
private
NonCopyable
true

mlreportgen.dom.StyleRef 对象的子项,指定为文档元素对象的数组。此属性包含使用 append 方法追加的文档元素对象。

属性:

SetAccess
private
NonCopyable
true

mlreportgen.dom.StyleRef 对象的标签,指定为字符向量或字符串标量。DOM API 在创建此对象的过程中生成一个会话唯一标签。生成的标签形式为 CLASS:ID,其中 CLASS 是对象类,ID 是对象的 Id 属性的值。指定您自己的标签值,以帮助您确定在文档生成过程中出现问题时要查找的位置。

属性:

NonCopyable
true

数据类型: char | string

mlreportgen.dom.StyleRef 对象的对象标识符,指定为字符向量或字符串标量。DOM API 在创建文档元素对象时会生成一个会话唯一标识符。您可以为 Id 指定自己的值。

属性:

NonCopyable
true

数据类型: char | string

方法

全部展开

示例

全部折叠

此示例使用大纲级别来指定页脚的内容。

import mlreportgen.dom.*;
d = Document('mydoc','pdf');
open(d);

% Create page footer
footer = PDFPageFooter('default');
d.CurrentPageLayout.PageFooters = footer;

% Define and the StyleRef object using default (first level heading)
% Append it to the footer
ref = StyleRef();
append(footer,ref);

% Create several pages
% The footer content changes based on the last Heading1 object
h = Heading1('My First Head');
p = Paragraph('The above heading appears in the footer because it is a level 1 head.');
append(d,h);
append(d,p);

h2 = Heading1('My Next Head');
h2.Style = {PageBreakBefore(true)};
p2 = Paragraph('Now the above heading appears in the footer.');

append(d,h2);
append(d,p2);

h3 = Heading1('My Third Head');
h3.Style = {PageBreakBefore(true)};
append(d,h3);
append(d,clone(p2));

p3 = Paragraph(['Because I have not added another Heading1 object '...
    'since the last one, the heading from the previous page appears in the footer.']);
p3.Style = {PageBreakBefore(true)};
append(d,p3);

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

此示例显示如何为引用的内容指定样式名称。此示例创建两个 StyleRef 对象:一个使用默认值(Heading1 对象),另一个使用以 Subtitle 样式名称格式化的段落内容。将两个对象插入页脚,以便页脚包含 [Most Recent Heading1 Name]: [Most Recent Subtitle Name] 形式的文本。

import mlreportgen.dom.*;
d = Document('mydoc','docx');
open(d);

% Create page footer
footer = DOCXPageFooter('default');
d.CurrentPageLayout.PageFooters = footer;

% Create two StyleRef objects. ref uses content of Heading1 objects;
% ref2 uses content of paragraphs that use Subtitle style name.
ref = StyleRef();
ref2 = StyleRef('Subtitle');

% Assemble the footer text
footpara = Paragraph();
footpara.WhiteSpace = 'preserve';
append(footpara,ref);
append(footpara,': ');
append(footpara,ref2);
append(footer,footpara);

% Create Heading1 and Subtitle paragraphs
% Footers update based on most recent values
h = Heading1('My Document Title');
sub = Paragraph('Subtitle Text');
sub.StyleName = 'Subtitle';
p = Paragraph('Here''s some text.');
append(d,h);
append(d,sub);
append(d,p);

sub2 = Paragraph('Another Subtitle');
sub2.StyleName = 'Subtitle';
sub2.Style = {PageBreakBefore(true)};
append(d,sub2);
append(d,clone(p));

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

版本历史记录

在 R2016a 中推出