【问题标题】:Google Apps Script TableCell not accepting AttributesGoogle Apps 脚本 TableCell 不接受属性
【发布时间】:2017-06-07 19:32:52
【问题描述】:

我有一些用于谷歌应用脚​​本宏的代码。这使用 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);

【问题讨论】:

    标签: google-apps-script macros


    【解决方案1】:

    您可以参考这个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();
    }
    

    希望这会有所帮助。

    【讨论】:

    • 为什么 cellStyle 必须同时应用于 td 和 para?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    相关资源
    最近更新 更多