5

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.

brad
  • 9,573
  • 12
  • 62
  • 89
  • did you ever get this working? i need to set a dashed border, which I can't even find the programmatic interface for! – Michael Apr 24 '18 at 20:38
  • 1
    No. Google seem to have a habit of leaving a lot of stuff half finished. Sad. – brad Apr 26 '18 at 08:40

2 Answers2

4

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.

  • 2
    Thanks. It's disappointing to discover what is a fairly big hole in the GAS functionality on my first visit! I've clicked the star (me and nine others in 18 months) in the issue so maybe someone at Google will notice. – brad Jun 13 '16 at 02:59
  • 1
    Any update on this? I need to set a dashed border, and "it can't be done" is NOT acceptable Google! – Michael Apr 24 '18 at 20:33
  • It's 2023, still not working.. – AHeraldOfTheNewAge Jan 05 '23 at 13:29
2

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);
The Ratman
  • 33
  • 3