2015-07-20 63 views
1

我不能找到一種方法,水平對齊表格中使用谷歌Apps腳本在谷歌文檔。我已經徹底檢查了所有的文檔,並且還盲目嘗試了幾種方法。如何設置水平定在桌子上應用腳本

嘗試一:

var cells = [ 
    ['Company', rowData[3]], 
    ['Title', rowData[4]], 
]; 

var tableStyle = {}; 
tableStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.RIGHT; 

var mentorTable = body.appendTable(cells); 

var myTable = body.appendTable(cells); 
myTable.setAttributes(tableStyle); 

嘗試二:

var cells = [ 
    ['Company', rowData[3]], 
    ['Title', rowData[4]], 
]; 

var mentorTable = body.appendTable(cells); 
myTable.setAlignment(DocumentApp.HorizontalAlignment.RIGHT); 

的谷歌文檔的用戶界面支持更改從 「表格屬性」 菜單選項,這個屬性。

如何對齊使用谷歌Apps腳本在表有什麼想法?

+0

爲表通過Google Apps腳本中心功能請求添加到谷歌跟蹤器,可以在「星」吧:https://issuetracker.google.com/issues/36765133 – Kos

回答

0

我也儘管你可以做到這一點的屬性設置爲完整表,但我只能做到這一點使每個單元格的內容,然後添加屬性。

在下面的代碼我查找文檔中的表格,轉到每個單元格,然後應用格式。我知道這不應該是這樣,但無法找到一個更簡單的方法來做到這一點。

希望它有幫助。

function myFunction() { 
    var doc = DocumentApp.getActiveDocument(); 

    var style = {}; 
    style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.RIGHT; 

    var body = doc.getBody(); 

    for(var i = 0 ; i < body.getNumChildren(); i++) 
    { 
    if(body.getChild(i).getType() == 'TABLE') 
    { 
     var table = body.getChild(i).asTable(); 
     var rows = table.getNumRows(); 
     var cols = table.getChild(0).asTableRow().getNumChildren(); 

     for(var j =0 ; j< rows; j++) 
     { 
     for(var k =0; k<cols; k++) 
     { 
      body.getChild(i).asTable().getCell(j,k).getChild(0).setAttributes(style); 
     }  
     } 
    } 
    } 
} 
+1

謝謝,赫拉爾多。這似乎是右對齊表中的每個單元格。我想實際上將表格本身與文檔中的右側對齊。 – SeanMaday

相關問題