如何在Google文档表格单元格中对齐文字

Kar*_*her 4 google-docs google-apps-script

我通过脚本创建了一个google文档,并希望通过以下代码将表插入其中:

  var row1 = "some city"
  var row2 = "some text"
  var rowsData = [[row1, row2]];
  var table = body.appendTable(rowsData);
  table.setBorderWidth(0);
  style[DocumentApp.Attribute.BOLD] = true;
  style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
  table.setAttributes(style);
Run Code Online (Sandbox Code Playgroud)

我期待单元格中的所有文本都是粗体和居中的.但是我看到只应用了粗体属性.我试过添加一些脚本

 var cell1 = table.getCell(0, 0);
  var cell2 = table.getCell(0, 1);
  cell1.setAttributes(style);
  cell1.editAsText().setAttributes(style);
Run Code Online (Sandbox Code Playgroud)

但没有效果.

请告诉我如何正确地在单元格中居文本!

use*_*325 12

文本对齐不能应用于"table-cell"项,它只能在"paragraph"项上提示.要从"table-cell"获取"段落",您需要执行以下操作:

var cellStyle = {};
cellStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
tabel.getRow(0).getCell(1).getChild(0).asParagraph().setAttributes(cellStyle)
Run Code Online (Sandbox Code Playgroud)

(感谢Stefan van Aalst 的回答)


小智 0

我相信您想使用 .setAlignment 而不是 .setAttribute 或 style[]。

cell1.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
Run Code Online (Sandbox Code Playgroud)