How can I set paragraph line spacing below 1 in Google Docs using Apps Script?

I’m struggling to make two lines closer together in a Google Doc using Apps Script. In the editor, I can use custom spacing and set it to 0.5, but my script always defaults to 1.

Here’s a snippet that shows my problem:

function setCustomSpacing() {
  let doc = DocumentApp.getActiveDocument().getBody();
  let para = doc.appendParagraph('');
  para.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
  para.setLineSpacing(0.5);

  para.appendText('First line\n').setFontSize(24);
  para.appendText('Second line').setFontSize(12).setFontFamily('Arial');

  console.log(para.getLineSpacing());
}

The log shows 0.5, but the document always displays spacing of 1. Values above 1 work fine. I can manually change it in the doc, but not via script.

Any ideas why this happens or how to fix it? Is there a minimum spacing limit I’m unaware of?

hey, have u tried using the Google Docs API instead? it lets u set spacing below 1. u need to enable it in ur Google Cloud project first, then use the batchUpdate method. it’s a bit more complex but gives u more control. might be worth a shot if ur really need that tight spacing!

As someone who’s worked extensively with Google Docs and Apps Script, I can tell you that the setLineSpacing method in DocumentApp is indeed limited. It won’t let you go below 1, which is frustrating when you need tighter spacing.

I’ve found a workaround though. Use the Google Docs API instead. It’s more powerful and flexible. You’ll need to enable it in your Google Cloud project first, then use the batchUpdate method to set custom line spacing.

Here’s a quick example of how it might look:

const requests = [{
  updateParagraphStyle: {
    paragraphStyle: { lineSpacing: 50 },
    range: { startIndex: para.getStartOffset(), endIndex: para.getEndOffsetInclusive() },
    fields: 'lineSpacing'
  }
}];
Docs.Documents.batchUpdate({ requests }, docId);

This sets the spacing to 50% of normal. It’s a bit more complex to set up, but it gives you the control you need. Just be mindful of API quotas and make sure you’ve got the right authentication set up. Hope this helps!

I’ve encountered this issue before. Unfortunately, the DocumentApp service in Apps Script has limitations when it comes to line spacing. It doesn’t support values below 1, which is why your script isn’t working as expected.

However, there’s a workaround using the Google Docs API. You’ll need to enable it in your Google Cloud project first. Then, you can use the batchUpdate method to set custom line spacing values below 1.

Here’s a basic example of how you might structure the request:

const requests = [{
  updateParagraphStyle: {
    paragraphStyle: { lineSpacing: 50 }, // 50% of normal spacing
    range: { startIndex: paragraph.getStartOffset(), endIndex: paragraph.getEndOffsetInclusive() },
    fields: 'lineSpacing'
  }
}];
Docs.Documents.batchUpdate({ requests }, docId);

This approach gives you more control over formatting, including tighter line spacing. Just be mindful of API quotas and ensure proper authentication when implementing this solution.