Google Apps脚本TableCell不接受属性

时间:2017-06-07 19:32:53

标签: google-apps-script macros

我有一些我用于谷歌应用程序脚本宏的代码。这使用DocumentApp TableRow和Table Cell数据类型。

//row is TableRow datatype
function appendRow(row){
    var style = {};
    style[DocumentApp.Attribute.FONT_SIZE] = '8';
    style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.RIGHT;
    row.appendTableCell(SOME_TEXT).setAttributes(style);
}

因此,当我运行此函数时,行中的结果单元格仍然是默认左对齐。我错过了什么吗?他们在自己的网站上有这个例子。

https://developers.google.com/apps-script/reference/document/table-cell#setAttributes(Object)

var body = DocumentApp.getActiveDocument().getBody();

// Define a custom paragraph style.
var style = {};
style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
 DocumentApp.HorizontalAlignment.RIGHT;
style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
style[DocumentApp.Attribute.FONT_SIZE] = 18;
style[DocumentApp.Attribute.BOLD] = true;

// Append a plain paragraph.
var par = body.appendParagraph('A paragraph with custom style.');

// Apply the custom style.
par.setAttributes(style);

1 个答案:

答案 0 :(得分:0)

您可以参考此sample tutorial。这是一个示例代码,用于添加一个右对齐的单元格文本表。

function addTableInDocument() {

  var headerStyle = {};
  headerStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = '#336600';
  headerStyle[DocumentApp.Attribute.BOLD] = true;
  headerStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#FFFFFF';

  //Style for the cells other than header row
  var cellStyle = {};
  cellStyle[DocumentApp.Attribute.BOLD] = false;
  cellStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';
  cellStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.RIGHT;

  var doc = DocumentApp.getActiveDocument();

  //get the body section of document
  var body = doc.getBody();

  //Add a table in document
  var table = body.appendTable();

  //Create 5 rows and 4 columns
  for(var i=0; i<2; i++){
    var tr = table.appendTableRow();

    //add 4 cells in each row
    for(var j=0; j<2; j++){
      var td = tr.appendTableCell('Cell '+i+j);

      //if it is header cell, apply the header style else cellStyle
      if(i == 0) td.setAttributes(headerStyle);
      else td.setAttributes(cellStyle);

      //Apply the para style to each paragraph in cell
      var paraInCell = td.getChild(0).asParagraph();
      paraInCell.setAttributes(cellStyle);
    }
  }

  doc.saveAndClose();
}

enter image description here

希望这有帮助。