1

I've been trying to write a script that will output two lines to a google doc. The lines need to spaced closer together than the default. In the editor I can click the line spacing icon and select "Custom Spacing" then set it to a value like 0.5 to make the lines closer together. I've been trying to achieve the same thing in app scripts, but every time the line spacing value will default to 1. It's almost like my value is being rounded up at some point.

This code reproduces my issue:

  let docBody = DocumentApp.openByUrl("URL OF BLANK GOOGLE DOC").getBody()
  let p1 = docBody.appendParagraph("")
  p1.setAlignment(DocumentApp.HorizontalAlignment.CENTER)
  p1.setLineSpacing(0.5)

  let line1 = p1.appendText(`Line 1\r`)
  line1.setFontSize(30)

  let line2 = p1.appendText("line 2")
  line2.setFontFamily("Roboto Mono")
  line2.setFontSize(10)
  console.log(p1.getLineSpacing())

p1.getLineSpacing returns 0.5 and there are no errors, but in the document the line spacing is always set to 1. If I change 0.5 to something above 1 (like 1.43), it will set the property like it's supposed to. I can also go into the document and change the line spacing normally, but I can't seem to set it from the script.

Is there a reason it can't be set this way?

1

1 Answer 1

1

Issue and workaround:

It seems that in the current stage, the method of setLineSpacing of Google Document service (DocumentApp) cannot use the custom line space like 0.5. I'm not sure whether this is the current specification or a bug.

But, fortunately, when Google Docs API is used, this custom line space can be used. In this answer, I would like to propose using Google Docs API for setting the custom line space.

When Docs API is used, your script can be modified as follows.

Modified script:

This script uses Docs API. So, please enable Docs API at Advanced Google services.

function sample() {
  const doc = DocumentApp.openByUrl("URL OF BLANK GOOGLE DOC"); // Added
  let docBody = doc.getBody(); // Modified
  let p1 = docBody.appendParagraph("");
  p1.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
  // p1.setLineSpacing(0.5); // Removed

  let line1 = p1.appendText(`Line 1\r`);
  line1.setFontSize(30);

  let line2 = p1.appendText("line 2");
  line2.setFontFamily("Roboto Mono");
  line2.setFontSize(10);
  console.log(p1.getLineSpacing());

  // I added the below script.
  doc.saveAndClose();
  const id = doc.getId();
  const obj = Docs.Documents.get(id).body.content.pop();
  const requests = [{ updateParagraphStyle: { paragraphStyle: { lineSpacing: 50 }, range: { startIndex: obj.startIndex, endIndex: obj.endIndex }, fields: "lineSpacing" } }];
  Docs.Documents.batchUpdate({ requests }, id);
}
  • When this script is run, you can see that the line space is set as 0.5 on Google Document.

References:

  • UpdateParagraphStyleRequest

  • ParagraphStyle

    • From this official document, it says as follows.

      lineSpacing: The amount of space between lines, as a percentage of normal, where normal is represented as 100.0. If unset, the value is inherited from the parent.

3
  • I contacted Google workspace support with this issue and their response was that they also don't know if this is intentional or a bug, but the workaround posted looks like it does the trick. I also needed to switch from the default app scripts managed GCP project to a standard GCP project in the settings before I could enable the advanced service. I realize this is a pretty specific issue, but if anyone else runs into it, this seems to be the official solution.
    – demnusoic
    Jan 20 at 1:17
  • @demnusoic Thank you for replying. When the values of line space shown in the UI are used, it seems that setLineSpacing can be used. But, when the values of line space not shown in the UI are used (custom line space), it seems that setLineSpacing cannot be used. From this situation, I thought that this might be the current specification as my guess. I would like to expect this will be resolved in the future update. And, I'm glad your issue was resolved with a workaround. Thank you, too.
    – Tanaike
    Jan 20 at 1:23
  • @demnusoic When I tested more, again, it seems that in the current stage, setLineSpacing cannot be used with numbers less than 1 while the numbers more than 1. In order to use numbers less than 1, Docs API is used as a current workaround.
    – Tanaike
    Jan 20 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.