如何在Apps脚本的表格中设置水平对齐 [英] How to Set Horizontal Alignment On A Table In Apps Script

查看:111
本文介绍了如何在Apps脚本的表格中设置水平对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到使用Google Apps脚本在Google文档中水平对齐表格的方法。我已经彻底检查了所有的文件,并且还盲目地尝试了几种方法。



尝试一:

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

var tableStyle = {};
tableStyle [DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.Horizo​​ntalAlignment.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.Horizo​​ntalAlignment.RIGHT);

Google Docs用户界面支持从表格属性菜单选项更改此属性。



有关如何使用Google Apps Script对齐表的任何想法? 尽管你可以将属性设置为完整的表格,但我只能通过它获取每个单元格的内容,然后添加属性。



在以下代码我查找文档中的表格,转到每个单元格,然后应用格式。我知道这不应该是这样,但不能找到一个更简单的方法来做到这一点。



希望它有帮助。

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

var style = {};
样式[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.Horizo​​ntalAlignment.RIGHT;

var body = doc.getBody(); (body).getChild(i).getType()=='

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

为(var j = 0; j {
$ b body.getChild(i).asTable()。getCell(j,k).getChild(0).setAttributes(style);
}
}
}
}
}


I am not able to find a way to horizontally align a table in a Google Doc using Google Apps Script. I have thoroughly checked all of the documentation, and also blindly tried several approaches.

Attempt One:

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

Attempt Two:

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

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

The Google Docs UI supports changing this attribute from the "Table Properties" menu option.

Any thoughts on how to align a table using Google Apps Script?

解决方案

I also though you could do it setting the attributes to the complete table, but i could only do it getting the content of each cell and then add the attribute.

In the following code I look for the tables in the document, go to each cell and then apply the formatting. I know this shouldn't be like that but couldn't find a easier way to do it.

Hope it helps.

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

这篇关于如何在Apps脚本的表格中设置水平对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆