Docx: Table borders

Created on 21 Jan 2019  路  16Comments  路  Source: dolanmiu/docx

Is there any method for removing table borders by default or with one method for certain table? :)

feature request

Most helpful comment

Instead of setting the properties like that, I discovered it would be much more efficient to apply styles.

const docx = require('docx');
const { Attributes, XmlComponent } = docx;

class StyleCreator extends XmlComponent {
  constructor(styleId, mainId) {
    super(mainId);
    this.styleId = styleId;
    this.root.push(
      new Attributes({
        val: styleId,
      }),
    );
    return this;
  }
}

docx.Table.prototype.addTableStyle = function addTableStyle(styleId) {
  this.properties.root.push(new StyleCreator(styleId, 'w:tblStyle'));
  return this;
};

Then I could use some styles like this,

const table = doc.createTable(10, 10, 10);
table.addTableStyle('TableGridLight');

This way I could apply any style to any kind of element.

I did not have had time to create a PR or a separate repo explaining this yet.

All 16 comments

You can do BorderStyle.NONE to achieve the effect you want

I tested the demo20 (downloaded from npm) and all other table styles. The demo failed to run.
:/

.addParagraph(new Paragraph("Hello"))
     ^

TypeError: table.getCell(...).addParagraph is not a function
    at Object.<anonymous> (/home/taher/docx/app.js:9:6)
    at Module._compile (internal/modules/cjs/loader.js:721:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:774:12)
    at executeUserCode (internal/bootstrap/node.js:499:15)
    at startMainThreadExecution (internal/bootstrap/node.js:436:3)

I cannot apply any table styles and all methods specified on the demo is not working.

:D I was looking forward to apply styles somehow. Sadly I couldn't do anything about it.
image

What am I missing?

I recently changed addContent to addParagraph so that's most likely why

Try a fresh clone of the repo, or do npm run build to rebuild.

On master it definitely works, otherwise my CI would fail.

I just checked right now for sanity, and i can confirm demo 20 does indeed work

Yes I did check it works on master.
Some functions still did not work properly though.
Also, I had to create a function for the table style.
I will try to contribute in ways if possible.

Some functions still did not work properly though.
Also, I had to create a function for the table style.

What functions?

Later I will create another issue with other problems, features and missing documentation.

ok sure

Thanks

Closing because this thread is getting irrelevant

For reference:

To modify borders in 4.7.1: https://github.com/dolanmiu/docx/blob/4.7.1/demo/demo20.ts

For the record in 4.7.1

cell.CellProperties.Borders
        .addTopBorder(BorderStyle.NONE)
        .addBottomBorder(BorderStyle.NONE)
        .addStartBorder(BorderStyle.NONE)
        .addEndBorder(BorderStyle.NONE);

Does not remove borders as expected.

@entrptaher can you share how you removed the borders? As above, this doesn't seem to be working

This seems to remove all the borders from a table:

const header = doc.Header.createTable(1, 3)
header.properties.root[1] = [] //clear border

Instead of setting the properties like that, I discovered it would be much more efficient to apply styles.

const docx = require('docx');
const { Attributes, XmlComponent } = docx;

class StyleCreator extends XmlComponent {
  constructor(styleId, mainId) {
    super(mainId);
    this.styleId = styleId;
    this.root.push(
      new Attributes({
        val: styleId,
      }),
    );
    return this;
  }
}

docx.Table.prototype.addTableStyle = function addTableStyle(styleId) {
  this.properties.root.push(new StyleCreator(styleId, 'w:tblStyle'));
  return this;
};

Then I could use some styles like this,

const table = doc.createTable(10, 10, 10);
table.addTableStyle('TableGridLight');

This way I could apply any style to any kind of element.

I did not have had time to create a PR or a separate repo explaining this yet.

Hi @entrptaher I'm trying to extend the table as you outlined but get 'Class constructor XmlComponent cannot be invoked without 'new'' when I run it. Any idea whats causing this?

@entrptaher Thank you! You saved my day. Your code works, I FINALLY got rid of my borders. It should REALLY by added to the code base.

It is planned to be added!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iainfreestone picture iainfreestone  路  3Comments

jacwright picture jacwright  路  6Comments

hmouhtar picture hmouhtar  路  6Comments

huynhtansi picture huynhtansi  路  6Comments

liquidslr picture liquidslr  路  4Comments