1

我想知道是否可以使用 Google Apps 脚本在 Google 文档中执行以下任何操作:

  1. 创建一个从右到左的表
  2. 修改表格的方向(从 RTL 到 LTR,反之亦然)
  3. 获取给定表格的方向

Google Apps 脚本为类的方向性提供了一个接口Paragraph:它有一个setLeftToRight()方法和一个LEFT_TO_RIGHT属性。但是,没有TableCell,TableRow或的等价物Table。此外,尝试在从右到左的有向段落之后添加一个表格会产生一个从左到右的有向表格。

相反,使用 Google 文档的图形界面,当光标位于从右到左的表格内时创建表格时,会生成一个从右到左的表格。此外,在任何单元格内设置方向,都会更改该单元格和整个表格的方向(尽管其他单元格保持其原始方向)。

我没有找到解决这个问题的方法,希望能得到一些想法。同时,我向 Google 产品团队提出了问题。以下示例代码也是提交问题的一部分,但在此处给出,以便无需 Google 帐户即可访问:

// run the following function on a document.
// it will append several paragraphs to the document, demonstrating the issue.
function tableDirectionIssueExample() {
  var rtlAttrs = {};
  rtlAttrs[DocumentApp.Attribute.LEFT_TO_RIGHT] = false;
  var ltrAttrs = {};
  ltrAttrs[DocumentApp.Attribute.LEFT_TO_RIGHT] = true;

  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var par;

  par = body.appendParagraph("The next table is in a left-to-right direction:");
  par.setAttributes(ltrAttrs);
  var tableLtr = body.appendTable([["a","b"],["c","d"]]);

  par = body.appendParagraph("The next table should be in a right-to-left direction, as that is what the graphical interface does, but it isn't:");
  par.setAttributes(rtlAttrs);
  var tableRtl = body.appendTable([["a","b"],["c","d"]]);

  body.appendParagraph("Getting the left-to-right attribute of a table results in the value: " + tableLtr.getAttributes()[DocumentApp.Attribute.LEFT_TO_RIGHT]);
  body.appendParagraph("The following table will be created with a default direction, and then we'll attempt setting the left-to-right property to false, but to no avail...");
  var tableExplicitRtl = body.appendTable([["a","b"],["c","d"]]);
  tableExplicitRtl.setAttributes(rtlAttrs);
  tableExplicitRtl.getRow(0).setAttributes(rtlAttrs);
  tableExplicitRtl.getRow(0).getCell(0).setAttributes(rtlAttrs);
  tableExplicitRtl.getRow(0).getCell(0).getChild(0).setAttributes(rtlAttrs); 
  body.appendParagraph("You'll notice the only thing that was succesful is setting the paragraph of the first cell as right-to-left, but that didn't affect the rest of the table. That is again unlike the graphical interface, where hitting a direction button on any cell changes the directionality of the entire table.");

}
4

0 回答 0