【问题标题】:Set "Left Indent" for a table in Google Docs using Google Apps Script使用 Google Apps 脚本为 Google Docs 中的表格设置“左缩进”
【发布时间】:2018-01-31 13:14:20
【问题描述】:

我们可以通过右键单击表格并选择“表格属性”来手动为 Google Docs 中的表格设置左缩进:

我已尝试通过设置INDENT_FIRST_LINEINDENT_START 属性来使用Google Apps 脚本实现相同的目的,但对表格没有影响:

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

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

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

请对此问题加注星标,以便 Google 团队优先处理:

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

【问题讨论】:

    标签: google-apps-script google-docs-api


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2012-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-29
        • 1970-01-01
        • 2019-10-23
        • 1970-01-01
        相关资源
        最近更新 更多