Murilo Sitonio
Murilo Sitonio

Reputation: 305

How to insert rows in a table maintaining the style?

The code works fine, but the new rows are different from the style of the table (the new rows are just blank). What should I do to add a row with the same style (color, border, width, height...) of the original table?

  var gtx = DocumentApp.getUi();
  var answer = gtx.alert('Do you want do add a row to the table?', gtx.ButtonSet.YES_NO);
  while (answer == gtx.Button.YES) 
  {
     var body = DocumentApp.getActiveDocument().getBody();
    var searchElement = body.findElement(DocumentApp.ElementType.TABLE);
    var element = searchElement.getElement();
    var tablecell = element.getParent();
    var tablerow = tablecell.getParent();
    var table = tablerow.getParent();
    var row = table.asTable().insertTableRow(table.getChildIndex(tablerow)+1);
        row.insertTableCell(0);
    var answer = gtx.alert('Do you want do add another row to the table?', gtx.ButtonSet.YES_NO);
     }
     }

The other code when I try to read and set the attributes.

function myFunction() {
 var gtx = DocumentApp.getUi();
  var answer = gtx.alert('Do you want do add a row to the table?', gtx.ButtonSet.YES_NO);
  while (answer == gtx.Button.YES) 
  {
     var body = DocumentApp.getActiveDocument().getBody();
     var searchElement = body.findElement(DocumentApp.ElementType.TABLE);
      var element = searchElement.getElement();
      var table = element.asTable();
      var tablebc = table.getBorderColor();
      var tablebw = table.getBorderWidth();
      var tablecw = table.getColumnWidth(1)
      var style = {};
      style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
      DocumentApp.HorizontalAlignment.CENTER;
      style[DocumentApp.Attribute.BORDER_COLOR] = tablebc;
      style[DocumentApp.Attribute.BORDER_WIDTH] = tablebw;
      style[DocumentApp.Attribute.WIDTH] = tablecw;
      var row = table.appendTableRow().appendTableCell();
        row.setAttributes(style);
    var answer = gtx.alert('Do you want do add another row to the table?', gtx.ButtonSet.YES_NO);
     }
     }

Upvotes: 2

Views: 308

Answers (1)

Rune
Rune

Reputation: 179

Just played around with this issue too because I wanted to uniform all tables in a document.

It appears to me that setting the DocumentApp.Attribute.BORDER_WIDTH does not have any effect neither on table or cell (using getCell).

Apparently its a bug.

Upvotes: 1

Related Questions