使用 Apps 脚本创建的 Google 文档中出现意外的空行

问题描述 投票:0回答:1

我正在开发一个 Google Apps 脚本,该脚本根据 Google 电子表格中的数据生成一个 Word 文件。该脚本创建一个 Google 文档,对其进行格式化,并插入各种信息。但是,我遇到了一个问题,即无论我插入的文本或元素如何,或者即使我注释掉与该部分相关的部分代码,生成的文档中名称段落后面都会出现意外的空行。

function create() {
  var fileName = SpreadsheetApp.getActiveSpreadsheet().getName();
  var name = fileName.replace(/\.[^/.]+$/, "");
  var formattedName = "CV - " + name;

  var doc = DocumentApp.create(formattedName);
  var docBody = doc.getBody();

  // Setting document margins
  docBody.setAttributes({
    [DocumentApp.Attribute.MARGIN_TOP]: 72,
    [DocumentApp.Attribute.MARGIN_BOTTOM]: 72,
    [DocumentApp.Attribute.MARGIN_LEFT]: 72,
    [DocumentApp.Attribute.MARGIN_RIGHT]: 72
  });

  // Inserting name paragraph
  var nameParagraph = docBody.insertParagraph(0, name);
  nameParagraph.setHeading(DocumentApp.ParagraphHeading.HEADING1);

  // The issue: An unexpected blank line appears here in the document

  // Further content insertion...
}

// Additional relevant functions like createTitle(), addFormattedParagraph(), etc.

javascript google-apps-script google-docs google-docs-api
1个回答
0
投票

我认为当你创建一个文档时,它总是至少有一个子文档。在这种情况下,你可能可以使用类似的东西:

body.getChild(1).asParagraph().removeFromParent()
© www.soinside.com 2019 - 2024. All rights reserved.