brad
brad

Reputation: 9773

How do I (or can I) modify the border of a table in a document with Google Apps Script?

Is there any way of modifying the border of an individual cell within a document table using Google Apps Script? The TableCell Class reference documentation (here) doesn't have any methods that seem to allow this. This issue seems to suggest that there isn't a way but I thought I would ask as I am very new to GAS and don't know my way around yet.

Upvotes: 5

Views: 3341

Answers (2)

The Ratman
The Ratman

Reputation: 33

If you really really need to do this programmatically you can create a table in the regular Google Docs editor with the borders you need and use Google Apps Script to copy it - from the same document or from a different document. You can then edit the contents of the new table.

//copy first table in other doc
table_copy = other_doc.getBody().getTables()[0].copy();
//paste into this doc
this_doc.getBody().appendTable(table_copy);

Upvotes: 2

user3717023
user3717023

Reputation:

The issue you linked to describes the current state of things. Changing the border color and width should be possible with the setAttributes method, as the list of supported attributes includes BORDER_COLOR and BORDER_WIDTH. So it's an Apps Script bug that these attributes have no effect, and until it's fixed, we can't manipulate these borders programmatically.

Here is a demo script (similar to the one posted in the linked issue thread, though I wrote this before reading the issue):

function tableBorder() {  
  var body = DocumentApp.getActiveDocument().getBody();
  var table = body.appendTable([['aaa', 'bbb'], ['ccc', 'ddd']]);
  var cell = table.getCell(1, 1);
  var style = {};
  style[DocumentApp.Attribute.BORDER_COLOR] = '#ff0000';
  style[DocumentApp.Attribute.BORDER_WIDTH] = 5; 
  style[DocumentApp.Attribute.BOLD] = true;
  cell.setAttributes(style);
}

A table is added, and the contents of "ddd" are made bold, but neither the color nor width of the border is changed by the script.

Upvotes: 4

Related Questions