【问题标题】:How to set a certain number of spaces or indents before a Paragraph in Google Docs using Google Apps Script如何使用 Google Apps 脚本在 Google 文档中的段落前设置一定数量的空格或缩进
【发布时间】:2019-04-11 21:05:06
【问题描述】:

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

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,但段落已经缩进...

更新

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

【问题讨论】:

  • 感谢您的回复。我无法理解But not just the top paragraph (actual paragraph not Mrs. Smith...) every paragraph, hence the for loop looping through the paragraphs.。我为我糟糕的英语水平道歉。
  • 为什么不使用Paragraph类的标准方法getIndentStart()getIndentFirstLine()
  • @АлександрЕрмолин 现在我再次对其进行了测试,这非常有效。当我第一次尝试时它不起作用,但由于某种原因现在它起作用了。
  • @АлександрЕрмолин 再三考虑,这并不完美:(

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


【解决方案1】:

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

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

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

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]。毫不奇怪,我们使用印刷的 points 而不是 cmitetres。 (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 了解正则表达式语法。

【讨论】:

  • 菜单步骤:“格式--对齐&缩进--缩进选项...”
猜你喜欢
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
  • 2022-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多