使用 Google Apps 脚本在 Google 文档中为 table 设置 "Left Indent"

Set "Left Indent" for a table in Google Docs using Google Apps Script

我们可以通过右键单击 table 并选择 "Table Properties":

在 Google 文档中手动设置 table 的左缩进

我尝试通过设置 INDENT_FIRST_LINEINDENT_START 属性使用 Google Apps 脚本实现相同的效果,但对 table 没有影响:

var style = {};
style[DocumentApp.Attribute.INDENT_FIRST_LINE] = 72;
style[DocumentApp.Attribute.INDENT_START] = 72; 

body.insertTable(elementIndex, table).setAttributes(style);

如何使用 Google Apps 脚本为 table 设置相同的 属性?有没有其他方法可以达到同样的效果?

请给这个问题加注星标,以便 Google 团队优先处理:

https://issuetracker.google.com/issues/36764951

因为 Apps 脚本不提供此功能。我最终做了以下事情:

  • 创建一个包含两列的外部 table。将边框宽度设置为零
  • 第一列的宽度是要求缩进的长度
  • 第二列的宽度为实际table
  • 的宽度
  • 将 table 插入第二列。生成的文档看起来好像 table 已经缩进了

以下是上述算法的示例脚本:

var insertTableWithIndentation = function(body, 
                                        document, 
                                        originalTable, 
                                        elementIndex, 
                                        nestingLevel) {

  var docWidth = (document.getPageWidth() - 
                  document.getMarginLeft() - 
                  document.getMarginRight());

  var table = body.insertTable(elementIndex);
  var attrs = table.getAttributes();
  attrs['BORDER_WIDTH'] = 0;
  table.setAttributes(attrs);

  var row = table.appendTableRow();
  var column1Width = (nestingLevel+1) * 36;
  row.appendTableCell().setWidth(column1Width);

  var cell = row.appendTableCell();
  var column2Width = docWidth - column1Width;
  cell.setPaddingTop(0)
      .setPaddingBottom(0)
      .setPaddingLeft(0)
      .setPaddingRight(0)
      .setWidth(column2Width);

  cell.insertTable(0, originalTable);
}

我来这里是为了寻找如何设置信封。以下对我有用: 附加段落后设置边距是关键。

const env = DocumentApp.create('Envelope');
  let body = env.getBody();
  body.setPageHeight(420);
  body.setPageWidth(600);
  for(let i = 1; i< 10; i += 1){
    body.appendParagraph('');
  }
  body.appendParagraph('Name');
  body.appendParagraph('Address');
  body.appendParagraph('Address');
  body.appendParagraph('City, State, Zip');
  body.setMarginLeft(300);

另一种解决方案是使用 setIndentFirstLine,例如:

  par = body.appendParagraph('City, State, Zip');
  par.setIndentFirstLine(20);