Skip to content

Commit c9030ad

Browse files
committed
Add support for setting table width
1 parent 20e2730 commit c9030ad

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

docs/jsdocx.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/TableFormat.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ import TableBorders from './TableBorders'
33

44
export default class extends Element {
55
constructor(
6-
borders
6+
borders,
7+
width,
8+
widthUnitType
79
) {
810
super({ 'w:tblPr': {} }, '["w:tblPr"]')
911
this.setBorders(borders || null)
12+
this.setWidth(width, widthUnitType)
1013
}
1114

1215
finalize (contents) {
1316
if (this.borders) contents.unshift(this.borders)
17+
if (this.width) contents.unshift(this.width)
1418
}
1519

1620
addBorders () {
@@ -29,4 +33,15 @@ export default class extends Element {
2933
getBorders () {
3034
return this.borders
3135
}
36+
37+
setWidth (width, unitType) {
38+
if (width) {
39+
this.width = new Element({ 'w:tblW': {
40+
'@w:type': unitType || 'dxa',
41+
'@w:w': width
42+
} }, '["w:tblW"]')
43+
} else {
44+
this.width = null
45+
}
46+
}
3247
}

test/TableFormat.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,10 @@ describe('#TableFormat', () => {
2727
fmt.addBorders().setInside(12)
2828
assert.equal(fmt.toXml(), '<w:tblPr><w:tblBorders><w:insideH w:sz="12" w:val="single" w:space="0" w:color="000000" w:shadow="false"/><w:insideV w:sz="12" w:val="single" w:space="0" w:color="000000" w:shadow="false"/></w:tblBorders></w:tblPr>')
2929
})
30+
it('should render cell width correctly', () => {
31+
let fmt = new jsdocx.Table().addFormat()
32+
fmt.setWidth('33.3%', 'pct')
33+
assert.equal(fmt.toXml(), '<w:tblPr><w:tblW w:type="pct" w:w="33.3%"/></w:tblPr>')
34+
})
3035
})
3136
})

0 commit comments

Comments
 (0)