Adds table.
interface Border {
/** Color: HEX value. Default as auto */
color?: string;
/** Space: pt value. Default as 0 */
space?: number;
/** Border style: single, dotDash, none, thick... Default as single */
style?: string;
/** Width: eights of a point value. Default as 4. Minimum 2 and maximum 96 */
width?: number;
}
interface OptionsCaption extends OptionsParagraph, OptionsText {
/** Bookmark name. Default as _GoBack */
bookmarkName?: string;
/** Custom label */
label?: string;
/** Location. Default as below */
location?: "below" | "above";
/** Display label. Default as true */
showLabel?: boolean;
}
interface OptionsCellTable {
/** Background color: HEX value */
backgroundColor?: string;
/** Border */
border?: {
bottom?: Border;
left?: Border;
right?: Border;
top?: Border;
};
/** Grid span */
gridSpan?: number;
/** Fit text */
fitText?: boolean;
/** Margin: twips value */
margin?: {
top?: number;
right?: number;
bottom?: number;
left?: number;
};
/** Content */
text: string | WordFragment;
/** Text direction */
textDirection?: "btLr" | "tbLrV" | "tbRl" | "tbRlV" | "lrTb" | "lrTbV";
/** Vertical alignment */
verticalAlign?: "both" | "bottom" | "center" | "top";
/** Width: twips value */
width?: number;
/** Vertical merge */
vMerge?: "restart" | "continue";
/** Wrap text */
wrapText?: boolean;
}
interface OptionsRowTable {
/** Allow row to break accross pages */
cantSplit?: boolean;
/** Repeat as header row at the top of each page */
header?: boolean;
/** Height */
height?: {
/** Height size rule */
rule?: "auto" | "atLeast" | "exact";
/** Value: twips value */
value?: number;
};
}
interface OptionsTable {
/** Alignment */
align?: string;
/** Border */
border?: {
bottom?: Border;
insideH?: Border;
insideV?: Border;
left?: Border;
right?: Border;
top?: Border;
};
/** Caption */
caption?: OptionsCaption;
/** Cell margins: twips value */
cellMargin?: {
top?: number;
right?: number;
bottom?: number;
left?: number;
};
/** Cell spacing: twips value */
cellSpacing?: number;
/** Column widths */
columnWidths?: number | number[];
/** Contional formatting */
conditionalFormatting?: {
/** First column. Default as true */
firstColumn?: boolean;
/** First row. Default as true */
firstRow?: boolean;
/** Last column. Default as false */
lastColumn?: boolean;
/** Last row. Default as false */
lastRow?: boolean;
/** Horizontal banding. Default as false */
noHBand?: boolean;
/** Vertical banding. Default as true */
noVBand?: boolean;
};
/** Indentation: twips value */
indentation?: number;
/** Layout */
layout?: "autofit" | "fixed";
/** Paragraph options added after the table */
paragraphAfterOptions?: OptionsParagraph;
/** Paragraph styles applied to string cell contents */
pprStyles?: OptionsParagraph;
/** Table style. Default as TableGrid */
tblStyle?: string;
/** Text wrapping */
textWrapping?: "around" | "none";
/** Table width */
width?: {
/** Type: auto, dxa (twips value), pct (percent). Default as pct */
type?: "auto" | "dxa" | "pct";
/** Value: dxa (twips value), pct (percent value). Default as 100 if type is pct */
value?: number;
};
}
interface OptionsTblStylePr {
/** Background color: HEX value */
backgroundColor?: string;
/** Border */
border?: {
bottom?: Border;
insideH?: Border;
insideV?: Border;
left?: Border;
right?: Border;
top?: Border;
};
/** Paragraph styles applied to string cell contents */
pprStyles?: OptionsParagraph;
/** Character styles applied to string cell contents */
rprStyles?: OptionsRpr;
/** Vertical alignment */
verticalAlign?: "both" | "bottom" | "center" | "top";
}
interface OptionsTblPr {
/** Based on style. Default as TableNormal */
basedOn?: string;
/** Border */
border?: {
bottom?: Border;
insideH?: Border;
insideV?: Border;
left?: Border;
right?: Border;
top?: Border;
};
/** Cell background color: HEX value */
cellBackgroundColor?: string;
/** Cell margins: twips value */
cellMargin?: {
top?: number;
right?: number;
bottom?: number;
left?: number;
};
/** Cell vertical alignment */
cellVerticalAlign?: "both" | "bottom" | "center" | "top";
/** Column banded style */
colBandSize?: boolean;
/** Contional formatting */
conditionalFormatting?: {
/** First column. Default as true */
firstColumn?: boolean;
/** First row. Default as true */
firstRow?: boolean;
/** Last column. Default as false */
lastColumn?: boolean;
/** Last row. Default as false */
lastRow?: boolean;
/** Horizontal banding. Default as false */
noHBand?: boolean;
/** Vertical banding. Default as true */
noVBand?: boolean;
};
/** Indentation: twips value */
indentation?: number;
/** Paragraph style to be automatically applied to the next paragraph with the current style applied */
next?: string;
/** Paragraph styles applied to string cell contents */
pprStyles?: OptionsParagraph;
/** Row banded style */
rowBandSize?: boolean;
/** Character styles applied to string cell contents */
rprStyles?: OptionsRpr;
/** tblStylePr styles */
tblPrStyles?: {
band1Horz?: OptionsTblStylePr;
band1Vert?: OptionsTblStylePr;
band2Horz?: OptionsTblStylePr;
band2Vert?: OptionsTblStylePr;
firstCol?: OptionsTblStylePr;
firstRow?: OptionsTblStylePr;
lastCol?: OptionsTblStylePr;
lastRow?: OptionsTblStylePr;
neCell?: OptionsTblStylePr;
nwCell?: OptionsTblStylePr;
seCell?: OptionsTblStylePr;
swCell?: OptionsTblStylePr;
};
}
Example #1
import { CreateDocx, OptionsTable } from "build";
import * as fs from "fs";
const docx = new CreateDocx();
// add a table with text contents
const items = [
["11", "12", "13", "14"],
["21", "22", "23", "24"],
["31", "32", "33", "34"],
];
docx.addTable(items);
// add a table applying table styles
const optionsTableA: OptionsTable = {
align: "right",
border: {
top: {
color: "FF0000",
width: 4,
},
right: {
width: 4,
},
bottom: {
color: "FF0000",
width: 4,
},
left: {
width: 4,
},
insideH: {
color: "00FF00",
width: 10,
style: "dashed",
space: 10,
},
insideV: {
style: "dashed",
},
},
textWrapping: "around",
width: {
type: "pct",
value: 50,
},
};
docx.addTable(items, [], optionsTableA);
docx.addText("Text content added 'around' a table.");
// add a table applying table styles
const optionsTableB: OptionsTable = {
align: "center",
cellSpacing: 30,
cellMargin: {
top: 100,
left: 200,
bottom: 100,
right: 200,
},
columnWidths: [5000, 1000, 1000, 1000],
layout: "fixed",
paragraphAfterOptions: {
spacing: {
before: 0,
after: 0,
},
},
width: {
type: "dxa",
value: 8000,
},
};
docx.addTable(items, [], optionsTableB);
docx.save().then(contents => {
fs.writeFileSync("addTable_1.docx", Buffer.from(contents));
});
The resulting Word document looks like:
Example #2
import {
CreateDocx,
OptionsImage,
OptionsLink,
OptionsText,
WordFragment,
} from "build";
import * as fs from "fs";
const docx = new CreateDocx();
// create WordFragments
const wordFragmentText = new WordFragment();
const textA: OptionsText = {
bold: false,
italic: true,
fontSize: 14,
text: "A content with styles ",
};
const textB: OptionsText = {
color: "0000FF",
font: "Times New Roman",
text: "in a cell.",
};
wordFragmentText.addText([textA, textB]);
const wordFragmentLink = new WordFragment();
const optionsLink: OptionsLink = {
link: "https://www.phpdocx.com",
text: "phpdocx",
};
wordFragmentLink.addLink(optionsLink);
const filePng = fs.readFileSync("image.png").buffer;
const wordFragmentImage = new WordFragment();
const imagePng: OptionsImage = {
image: filePng,
};
wordFragmentImage.addImage(imagePng);
// add text contents and WordFragments in the table
const items = [
["Title A", "Title B", "Title C"],
[wordFragmentText, wordFragmentLink, wordFragmentImage],
];
docx.addTable(items);
docx.save().then(contents => {
fs.writeFileSync("addTable_2.docx", Buffer.from(contents));
});
The resulting Word document looks like: