main content

span a table entry across rows and columns -凯发k8网页登录

these examples show how to make a table entry span rows and columns.

informal table

this example shows how to use row and column spanning in a dom informal table object to create the following table.

an informal table is a table that does not include pre-defined head, body, and foot sections. however, you can format the first few rows of an informal table as a header as this example shows.

import the dom package so you do not have to use long, fully qualified class names.

import mlreportgen.dom.*

set up the document and add a heading to describe the table.

% to create a pdf report, change the output type from "docx" to "pdf". 
% to create an html report, change "docx" to "html" or "html-file" for
% a multifile or single-file report, respectively.
rpt = document('myreport','docx');
h = heading(1, 'multiple row and column table entries using an informal table');
h.style = [h.style {halign('center')}];
append(rpt, h);

create cell arrays for the styles to be used by the different document components. the containing table spans the width of the page and has solid lines separating the entries. the main header is steel blue, and then sub-header is a lighter sky blue.

tablestyle = {width('100%'), border('solid'), colsep('solid'), rowsep('solid')};
mainheaderrowstyle = {valign('middle'), innermargin('2pt', '2pt', '2pt', '2pt'), ...
    backgroundcolor('steelblue')};
mainheadertextstyle = {bold, outermargin('0pt', '0pt', '0pt', '0pt'), fontfamily('arial')};
subheaderrowstyle = {valign('middle'), innermargin('2pt', '2pt', '2pt', '2pt'), backgroundcolor('skyblue')};
subheadertextstyle = {bold, outermargin('0pt', '0pt', '0pt', '0pt'), fontfamily('arial')};
bodystyle = {outermargin('0pt', '0pt', '0pt', '0pt'), innermargin('2pt', '2pt', '2pt', '0pt')};

create some sample data to include in the table. then, create the table object.

data = {'john', 'smith', 'natick, ma';
    'jane', 'doe', 'boston, ma'};
t = table(3);
t.style = [t.style tablestyle];

create a tablerow object for the first row of the table that contains the name and address headers. the name header has two sub-headers, so it spans two columns. this is set with the colspan property of the table entry. the address header does not have any sub-headers, so it spans two rows. this is set with the rowspan property of the table entry.

r = tablerow;
r.style = [r.style mainheaderrowstyle];
p = paragraph('name');
p.style = [p.style mainheadertextstyle {halign('center')}];
te = tableentry(p);
te.colspan = 2;
append(r, te);
p = paragraph('address');
p.style = [p.style mainheadertextstyle];
te = tableentry(p);
te.rowspan = 2;
append(r, te);
append(t, r);

create a second row in the table for the first and last sub-headers of name. even though the table is three columns wide, the second row contains only two entries because the address field spans both the first and second rows.

r = tablerow;
r.style = [r.style subheaderrowstyle];
p = paragraph('first');
p.style = [p.style subheadertextstyle];
te = tableentry(p);
append(r, te);
p = paragraph('last');
p.style = [p.style subheadertextstyle];
te = tableentry(p);
append(r, te);
append(t, r);

loop through the sample data to add it to the table.

for k = 1:size(data, 1)
    r = tablerow;
    r.style = [r.style bodystyle];
    te = tableentry(data{k,1});
    append(r, te);
    te = tableentry(data{k,2});
    append(r, te);
    te = tableentry(data{k,3});
    append(r, te);
    append(t, r);
end

add the table to the document.

append(rpt, t);

formal table

this example shows how to use row and column spanning in a dom formal table object to create the following table.

a formal table is a table that consists of three sub-tables for the header, body, and footer sections of the table, respectively. using a formal table instead of an informal table separates the header, which requires special formatting, from the data, which can be passed directly to the formal table constructor to populate the body table.

import the dom package so you do not have to use long, fully-qualified class names.

import mlreportgen.dom.*

move to a new page in the existing document and add a heading to describe the table.

br = pagebreak();
append(rpt, br);
h = heading(1, 'multiple row and column table entries using a formal table');
h.style = [h.style {halign('center')}];
append(rpt, h);

create cell arrays for the styles to be used by the different document components. the containing table spans the width of the page and has solid lines separating the entries. the main header is steel blue, and then sub-header is a lighter sky blue.

tablestyle = {width('100%'), border('solid'), colsep('solid'), rowsep('solid')};
mainheaderrowstyle = {valign('middle'), innermargin('2pt', '2pt', '2pt', '2pt'), ...
    backgroundcolor('steelblue')};
mainheadertextstyle = {bold, outermargin('0pt', '0pt', '0pt', '0pt'), fontfamily('arial')};
subheaderrowstyle = {valign('middle'), innermargin('2pt', '2pt', '2pt', '2pt'), backgroundcolor('skyblue')};
subheadertextstyle = {bold, outermargin('0pt', '0pt', '0pt', '0pt'), fontfamily('arial')};
bodystyle = {outermargin('0pt', '0pt', '0pt', '0pt')};

create some sample data to include in the table. then, create a formaltable object with the sample data in the body section.

data = {'john', 'smith', 'natick, ma';
    'jane', 'doe', 'boston, ma'};
t = formaltable(data);
t.style = [t.style tablestyle];
t.body.tableentriesstyle = [t.body.tableentriesstyle, bodystyle];

construct the header table of the formaltable object using the same method as the previous example. create a tablerow object for the first row of the table that contains the name and address headers. the name header has two sub-headers, so it spans two columns. this is set with the colspan property of the table entry. the address header does not have any sub-headers, so it spans two rows. this is set with the rowspan property of the table entry.

r = tablerow;
r.style = [r.style mainheaderrowstyle];
p = paragraph('name');
p.style = [p.style mainheadertextstyle {halign('center')}];
te = tableentry(p);
te.style = [te.style mainheadertextstyle];
te.colspan = 2;
append(r, te);
p = paragraph('address');
p.style = [p.style mainheadertextstyle];
te = tableentry(p);
te.style = [te.style mainheadertextstyle];
te.rowspan = 2;
append(r, te);
append(t.header, r);

create a second row in the table for the first and last sub-headers of name. even though the table is three columns wide, the second row only contains two entries because the address field spans both the first and second rows.

r = tablerow;
r.style = [r.style subheaderrowstyle];
p = paragraph('first');
p.style = [p.style subheadertextstyle];
te = tableentry(p);
append(r, te);
p = paragraph('last');
p.style = [p.style subheadertextstyle];
te = tableentry(p);
append(r, te);
append(t.header, r);

add the table to the document.

append(rpt, t);

page layout table

this example shows how use row and column spanning to create an invisible page layout table for a complex layout.

import the dom package so you do not have to use long, fully-qualified class names.

import mlreportgen.dom.*

move to a new page in the existing document and add a heading to describe the table.

br = pagebreak();
append(rpt, br);
h = heading(1, 'multi-column invisible table');
h.style = [h.style {halign('center')}];
append(rpt, h);

determine the styles for the different components on the page. the main table is invisible, so it does not have a border or separators.

maintablestyle = {width('100%'), border('none') colsep('none'), rowsep('none')};
datatablestyle = {border('solid'), colsep('dotted'), rowsep('solid'),...
    outermargin('0pt', '0pt', '0pt', '0pt')};
datatableentriesstyle = {outermargin('0pt', '0pt', '0pt', '0pt'), valign('middle')};
histstyle = {innermargin('2pt', '2pt', '2pt', '2pt'), ...
    halign('center'), valign('bottom'), width('5in'), height('4in')};

create example data representing 500 random coin flips. then, create text, formaltable, and image objects to display information about the data.

coinflips = randi(2, [500, 10]);
numheads = sum(coinflips-1,2);
p = text(['the plot below shows the results from 500 trials in which a coin was flipped 10 times, ',...
    'and the number of times the coin landed on heads was counted. ',...
    'the table displays the results for the first 20 trials.']);
dataheader = {'trial', 'heads'};
databody = [(1:20)', numheads(1:20)];
datatable = formaltable(dataheader, databody);
datatable.tableentriesstyle = [datatable.tableentriesstyle datatableentriesstyle]; 
datatable.header.style = [datatable.header.style {bold}];
datatable.style = [datatable.style datatablestyle];
histogram(numheads);
title('results')
xlabel('number of heads')
ylabel('number of trials')
saveas(gcf,'histogram_img.png');
close(gcf)
h = image('histogram_img.png');

create the invisible table and begin adding components. add the data table to the first row and specify that it spans two rows.

t = table(2);
t.style = [t.style maintablestyle];
row1 = tablerow;
row1.style = [row1.style {width('100%')}];
entry1 = tableentry;
append(entry1, datatable);
entry1.rowspan = 2;
entry1.style = [entry1.style {width("40%")}];
append(row1, entry1);

add the text object to the first row as well and then append the row to the table.

entry2 = tableentry(p);
entry2.style = [entry2.style {width("60%")}];
append(row1, entry2);
append(t, row1);

create a new row and add the histogram image as the only entry. the data table already fills the first columns of both the first and second rows, so the histogram will be placed in the second column.

row2 = tablerow;
entry3 = tableentry;
h.style = [h.style histstyle];
append(entry3, h);
append(row2, entry3);
entry3.style = [entry3.style {width('60%')}];
append(t,row2);

generate and display the report.

append(rpt,t);
close(rpt);
rptview(rpt);
网站地图