2017-06-07 93 views
0

我有一些代碼用於Google應用腳本宏。這使用DocumentApp TableRow和Table Cell數據類型。Google Apps Script TableCell不接受屬性

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

回答

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

希望這有助於。