如何使用Google Apps脚本在Google文档中的段落之前设置一定数量的空格或缩进

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

我有一个20行的脚本,我想确保每个段落缩进一次。

该附加组件称为Autoformat,它是我使用Google Apps脚本制作的Google Docs插件,用于格式化用户的文档或MLA格式的文章。一世

function myFunction() {
  /*
  This function turns the document's format into standard MLA.
  */

  var body = DocumentApp.getActiveDocument().getBody();
  body.setFontSize(12); // Set the font size of the contents of the documents to 9
  body.setForegroundColor('#000000');
  body.setFontFamily("Times New Roman");

  // Loops through paragraphs in body and sets each to double spaced
  var paragraphs = body.getParagraphs();
  for (var i = 3; i < paragraphs.length; i++) { // Starts at 3 to exclude first 4 developer-made paragraphs
      var paragraph = paragraphs[i];
      paragraph.setLineSpacing(2);
      // Left align the first cell.
      paragraph.setAlignment(DocumentApp.HorizontalAlignment.LEFT);
      // One indent
      paragraph.editAsText().insertText(0, "\t"); // Adds one tab every time
  }
  var bodyText = body.editAsText();
  bodyText.insertText(0, 'February 3, 1976\nMrs. Smith\nYour Name Here\nSocial Studies\n');
  bodyText.setBold(false);
}

我试过的代码不起作用。但是我的预期结果是,对于myFunction()中for循环中的每个段落,在每个段落中的第一个单词之前恰好有4个空格。


这是一个例子:https://docs.google.com/document/d/1sMztzhOehzheRdqumC6PLnvk4qJgUCSE0irjTZ0FjTQ/edit?usp=sharing

如果用户使用Autoformat,但已经缩进了段落... Placeholder text with indented paragraph using snipping tool Placeholder text indented twice

更新

我已经研究过使用Paragraph.setIndentFirstLine()方法。当我将其设置为4时,它将其设置为1个空格。现在我意识到这是因为点和空格不是一回事。我需要乘以多少才能得到四个空格?

google-apps-script paragraph
1个回答
1
投票

让我们考虑一些基本的识别操作:手动和脚本。下图显示了如何缩进当前段落(光标停留在此段落内)。

Manual indenting of the current paragraph

请注意,单位为厘米。另请注意,该段落不包括前导空格或制表符,我们不需要它们。

假设我们想在脚本中获取缩进值并将它们应用于下一段。看下面的代码:

function myFunction() {
  var ps = DocumentApp.getActiveDocument().getBody().getParagraphs();
  // We work with the 5-th and 6-th paragraphs indeed
  var iFirst = ps[5].getIndentFirstLine();
  var iStart = ps[5].getIndentStart();
  var iEnd = ps[5].getIndentEnd();
  Logger.log([iFirst, iStart, iEnd]);
  ps[6].setIndentFirstLine(iFirst);
  ps[6].setIndentStart(iStart);
  ps[6].setIndentEnd(iEnd);
}

如果你运行并查看日志,你会看到如下内容:[92.69291338582678, 64.34645669291339, 14.173228346456694]。毫不奇怪,我们有印刷点而不是厘米。 (1cm = 28.3465pt)因此我们可以精确地测量和修改任何段落缩进值。

加成

出于某些原因,您可能希望控制段落开头的空格数。也可以通过脚本编写,但它对段落的“左”或“右”缩进没有影响。

下面的示例代码用于类似的任务:计算第5段的前导空格数,并在下一个段的开头创建相同数量的空格。

function mySpaces() {
  var ps = DocumentApp.getActiveDocument().getBody().getParagraphs();
  // We work with the 5-th and 6-th paragraphs indeed
  var spacesCount = getLeadingSpacesCount(ps[5]);
  Logger.log(spacesCount);
  var diff = getLeadingSpacesCount(ps[6]) - spacesCount;
  if (diff > 0) {
    ps[6].editAsText().deleteText(0, diff - 1);
  } else if (diff < 0) {
    var s = Array(1 - diff).join(' ');
    ps[6].editAsText().insertText(0, s);
  }
}


function getLeadingSpacesCount(p) {
  var found = p.findText("^ +");
  return found ? found.getEndOffsetInclusive() + 1 : 0;
}

我们使用类Text的方法deleteText()和insertText()进行适当的修正,并使用findText()来定位空格(如果有的话)。注意,最后一个方法参数是一个字符串,表示正则表达式。如果它们存在,它匹配“所有前导空格”。有关正则表达式语法,请参阅more details

© www.soinside.com 2019 - 2024. All rights reserved.