如何在保持样式的表格中插入行?

时间:2019-02-13 12:09:42

标签: google-apps-script google-docs

代码工作正常,但是新行与表的样式不同(新行只是空白)。如何添加与原始表格样式(颜色,边框,宽度,高度...)相同的行?

  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);
     }
     }

尝试读取和设置属性时的其他代码。

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);
     }
     }

1 个答案:

答案 0 :(得分:1)

因为我想统一文档中的所有表,所以也只是解决了这个问题。

在我看来 设置DocumentApp.Attribute.BORDER_WIDTH对表或单元格(使用getCell)都没有任何影响。

显然是个错误。