简体   繁体   中英

How to set a certain number of spaces or indents before a Paragraph in Google Docs using Google Apps Script

I have a 20 line script, and I want to make sure that each paragraph is indented exactly once.

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

The code I have tried doesn't work. But my expected results are that for every paragraph in the for loop in myFunction() , there are exactly 4 spaces before the first word in each paragraph.


Here is a sample: https://docs.google.com/document/d/1sMztzhOehzheRdqumC6PLnvk4qJgUCSE0irjTZ0FjTQ/edit?usp=sharing

If the user uses Autoformat, but already has the paragraphs indented... 使用剪切工具带有缩进段落的占位符文本 占位符文本缩进两次

Update

I have investigated use of the Paragraph.setIndentFirstLine() method. When I set it to four, it sets it to 1 space. Now I realize this is because points and spaces are not the same thing. What number do I need to multiply by to get four spaces in points?

Let us consider a few basic identing operations: manual and by script. The following image shows how to indent current paragraph (cursor stays inside this one).

当前段落的手动缩进

Please note, the units are centimetres. Also note, that the paragraph does not include leading spaces or tabs, we have no need of them.

Suppose we would like to get the indent values in the script and apply them to the next paragraph. Look at the code below:

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

If you run and look at the log, you will see something like this: [92.69291338582678, 64.34645669291339, 14.173228346456694] . No surprise, we have typographic points instead of centimetres . (1cm=28.3465pt) So we can measure and modify any paragraph indent values precisely.

Addition

For some reasons you might want to control spaces number at the beginning of the paragraph. It is also possible by scripting, but it has no effect on the paragraph's "left" or "right" indents.

Sample code below is for similar task: count leading spaces number of the 5-th paragraph and make the same number of spaces at the beginning of the next one.

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

We have used methods deleteText() and insertText() of the class Text for proper corrections and findText() to locate the spaces if any. Note, the last method argument is a string, representing a regular expression. It matches "all leading spaces", if they exist. See more details about regular expression syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM