# create_table


创建表格

函数库: TyReportGenerator

# 语法

create_table(rows, cols; key=value)
create_table(tabledata; key=value)

# 说明

create_table(rows, cols; key=value) 在文档中创建表格。示例


create_table(tabledata; key=value) 在文档中创建表格。示例

# 示例

合并表格单元格

创建 Word 文档,创建 6*6 表格并合并单元格。生成文档报告。

using TyReportGenerator

doc = create_document()

table = create_table(6, 6; mergeinfo=[[1 1; 2 2], [2 3; 4 5]])
add_table(doc, table)  # 添加表格

# 设置单元格的值
for i in 1:size(table.children)[1]
    for j in 1:size(table.children)[2]
        t = create_text(string(i * 10 + j))
        c = create_cell([t])
        table.children[i, j] = c
    end
end

doc_file = joinpath(tempdir(), "Merge table cells.docx");
generate_report(doc, doc_file)
rptview(doc_file)
数组创建表格

在表格构造函数中使用一个单元格数组,从具有多个数据类型的数据中创建一个表格。单元格数组可以包含双数、字符串、字符向量、DOM对象和其他数组。

表头标签是字符串,所以它们被存储在一个字符串数组中,而不是单元格数组。

using TyReportGenerator

doc = create_document()
headerLabels = ["Book", "Year Published", "Characters"]

定义要在表格中显示的数据。表格数据包括外部链接、数字和字符串数组,所以它包含在一个单元格数组中。

book1 = create_linktarget(
    create_text("The Three Musketeers"),
    "https://en.wikipedia.org/wiki/The_Three_Musketeers",
)
book2 = create_linktarget(
    create_text("Little Women"), "https://en.wikipedia.org/wiki/Little_Women"
)
book3 = create_linktarget(
    create_text("The Hound of the Baskervilles"),
    "https://en.wikipedia.org/wiki/The_Hound_of_the_Baskervilles",
)

books = [book1; book2; book3];
yearPublished = [1844; 1868; 1902];
characters = [
    ["d'Artagnan", "Athos", "Porthos", "Aramis"],
    ["Meg", "Jo", "Beth", "Amy"],
    ["Sherlock", "Watson"],
];

list = create_list.(characters)
tableData = [books, yearPublished, list]

表格数据中的单行字符串数组在构建表格时被转换为 document 对象。

将表和表头的样式设置为之前定义的样式,将表添加到文档中。生成文档报告。

table = create_table(tableData;style=Style(style_type = "Table Grid"; halign="left", valign="top"))

add_header(table, headerLabels)
add_table(doc, table)

for j in 1:size(table.children)[2]
    modify_cell(table.children[1, j];style=Style(;background_color_cell=(211, 211, 211), bold = true))
end

pkg_dir = pkgdir(TyReportGenerator)
doc_file = joinpath(tempdir(), "Create a Table from a Cell Array.docx");
tpl_file = joinpath(pkg_dir, "examples/Resources/文档模板-空.docx");

generate_report(doc, doc_file, tpl_file)

rptview(doc_file)
并排图像

创建 Word 文档,创建一个标题为 "Types of Cosine Value Plots with Random Noise" 的章节。

using TyReportGenerator
using TyPlot

doc = create_document()
add_heading1(doc, "Types of Cosine Value Plots with Random Noise")

创建要绘制的变量。将 x 创建为 200 个等间距值,介于 0 和 3pi 之间。创建 y 作为具有随机噪声的余弦值。

x = reshape(LinRange(0, 3 * pi, 200), 1, 200);
y = cos.(x) .+ rand(1, 200);

创建x和y值的图形对象:条形图(fig1)、散点图(fig2)和二维线图(fig3)。

pkg_dir = pkgdir(TyReportGenerator)
figure()
fig1 = bar(x, y)
saveas(gcf(), joinpath(tempdir(), "fig1.png"));
figure()
fig2 = scatter(x, y)
saveas(gcf(), joinpath(tempdir(), "fig2.png"));
figure()
fig3 = plot(x, y)
saveas(gcf(), joinpath(tempdir(), "fig3.png"));

设置图形对象的缩放比例,使其适合于表格中的条目,插入 1x3 表格

img1 = create_cell(
    [create_image(joinpath(tempdir(), "fig1.png"))]; width=(2, "in"), height=(2, "in")
)
text1 = create_cell([create_text("")]; width=(0.2, "in"), height=(2, "in"))
img2 = create_cell(
    [create_image(joinpath(tempdir(), "fig2.png"))]; width=(2, "in"), height=(2, "in")
)
text2 = create_cell([create_text("")]; width=(0.2, "in"), height=(2, "in"))
img3 = create_cell(
    [create_image(joinpath(tempdir(), "fig3.png"))]; width=(2, "in"), height=(2, "in")
)
table = create_table([img1 text1 img2 text2 img3])

将表添加到报告中,生成并查看文档。

add_table(doc, table)

doc_file = joinpath(tempdir(), "Side-By-Side Images in Table.docx");
generate_report(doc, doc_file)
rptview(doc_file)
并排表格

创建 word 文档,设置表格对象的缩放比例,使其适合于表格中的条目。插入 1x3 表格,在 1x3 表格的单元格中插入子表格table1、table2和文本text1。

using TyReportGenerator
using TyMath

doc = create_document()

table1 = create_cell([create_table(magic(8);style=Style(outer_margin = (0,0), halign = "center"))]; width=(7.59, "cm"))
text1 = create_cell([create_text("")]; width=(0.51, "cm"))
table2 = create_cell([create_table(magic(4);style=Style(outer_margin = (0,0), halign = "center"))]; width=(7.59, "cm"))

table = create_table([table1 text1 table2]; style=Style(; border_width="5x",style_type = "Normal Table"))

将表添加到报告中,生成并查看文档。

add_table(doc, table)

doc_file = joinpath(tempdir(), "Side-By-Side Tables.docx");

generate_report(doc, doc_file)

rptview(doc_file)

# 输入参数

rows — 行数
正整数

表中的行数,指定为正整数。

cols — 列数
正整数

表中的列数,指定为正整数。

tabledata — 表格数据
向量

tabledata为表格数据,为向量类型。

# 名称-值对组参数

指定可选的、以逗号分隔的 Key=Value 对组参数。Key 为参数名称,Value 为对应的值。您可采用任意顺序指定多个名称-值对组参数,如 Key1=Value1,...,KeyN=ValueN 所示。

mergeinfo — 表格合并
二维数组

表格合并指定的二维数组。

示例: create_table(6, 6; mergeinfo=[[1 1; 2 2], [2 3; 4 5]])

style — 表格的格式
document 对象的单元格数组

表格的格式。

caption — caption对象
caption对象

文档中的 caption 对象,存储题注信息。

# 输出参数

table — table对象
table对象

文档中的 table 对象,存储表格的行列数、合并范围、表格格式等信息。