google-apps-scriptgoogle-docs

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.


Solution

  • 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.