【问题标题】:Google Apps Script - setting Line spacing and Heading1 in a table cellGoogle Apps 脚本 - 在表格单元格中设置行距和 Heading1
【发布时间】:2022-09-23 20:46:25
【问题描述】:

感谢@Tanaike 为我之前的问题提供了答案。我还有另外两个问题。

1 - 当设置间距为 1 但在 Google Doc 中检查时,间距为 1.15。

heading1Style[DocumentApp.Attribute.LINE_SPACING] = 1;

2 - 我尝试将 DocumentApp.ParagraphHeading.Heading1 设置为以下,但它不起作用。

tableRow1.getCell(0, 1).editAsText().setAttributes(heading1Style);

以下是整个代码。

function insertHeading1() {
  var heading1Style = {};
  heading1Style[DocumentApp.Attribute.FONT_FAMILY] = \'Calibri\';
  heading1Style[DocumentApp.Attribute.FONT_SIZE] = 18;
  heading1Style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.LEFT;
  heading1Style[DocumentApp.Attribute.LINE_SPACING] = 1;
  heading1Style[DocumentApp.Attribute.BOLD] = true;

  var cells = [
    [\' \', \'HEADING 1, CALIBRI, 18PT, BOLD, LEFT ALIGNED, ALL CAPS, SINGLE SPACING, NO SPACE BEFORE AND AFTER PARAGRAPH\'],
  ];

  var tableRow1 = body.appendTable(cells);
  tableRow1.setColumnWidth(0, 10);
  tableRow1.getCell(0, 0).setBackgroundColor(\'#1c3a69\');
  tableRow1.getCell(0, 1).editAsText().setAttributes(heading1Style);
}

    标签: google-apps-script


    【解决方案1】:
    • 您正在将声明的样式属性设置为TableCell object。但是,您不能直接在 Google Docs 中更改 TableCell 对象的行间距。
    • 您可以改为在该单元格上添加 Paragragph 对象,然后修改 lineSpacing。为此,您可以调用第二个表格单元格的 TableCell 对象的方法insertParagraph(childIndex, text)

    我稍微修改了示例代码:

    
    function insertHeading1() {
      var doc = DocumentApp.getActiveDocument();
      var body = doc.getBody();
    
      var heading1Style = {};
      heading1Style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
      heading1Style[DocumentApp.Attribute.FONT_SIZE] = 18;
      heading1Style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.LEFT;
      // heading1Style[DocumentApp.Attribute.LINE_SPACING] = 1;
      heading1Style[DocumentApp.Attribute.BOLD] = true;
    
      var cells = [
        ['', ''],
      ];
    
      var tableRow1 = body.appendTable(cells);
      tableRow1.setColumnWidth(0, 10);
      tableRow1.getCell(0, 0).setBackgroundColor('#1c3a69');
      tableRow1.getCell(0, 1).setAttributes(heading1Style);
      tableRow1.getCell(0, 1)
          .insertParagraph(0, 'HEADING 1, CALIBRI, 18PT, BOLD, LEFT ALIGNED, ALL CAPS, SINGLE SPACING, NO SPACE BEFORE AND AFTER PARAGRAPH')
          .setLineSpacing(1);
      tableRow1.getCell(0, 1).removeChild(tableRow1.getCell(0, 1).getChild(1)); // deletes the additional paragraph created by appendTable()
    }
    

    【讨论】:

    • 谢谢@Gustavo。您的修改有效,我也解决了标题问题。这里的事情是在单元格中插入 para 之后,光标“进入”下一行无论如何要删除它?标题 1、CALIBRI、18PT、粗体、左对齐、全部大写、单行距、段落前后没有空格 <enter>
    • @user1787756 编辑了我的答案,并修复了删除附加行。如果对您有帮助,请记住接受答案。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多