Home > Software design >  Why can't I set custom line spacing to less than zero on a paragraph in google docs using app s
Why can't I set custom line spacing to less than zero on a paragraph in google docs using app s

Time:01-20

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?

CodePudding user response:

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.

  • Related