Creating a custom formatting script for Google Docs

I’m working on a Google Docs script to apply custom paragraph formatting. I need help setting up these specific styles:

  • 4 cm indent
  • Arial font, size 10
  • Single line spacing
  • 1.5 spacing before citations
  • Justified alignment

I found a script online but I’m not sure how to modify it for my needs. Here’s what I have so far:

function applyCustomStyle() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  
  body.setAttributes({
    [DocumentApp.Attribute.FONT_FAMILY]: 'Arial',
    [DocumentApp.Attribute.FONT_SIZE]: 10,
    [DocumentApp.Attribute.LINE_SPACING]: 1,
    [DocumentApp.Attribute.INDENT_FIRST_LINE]: 151, // 4 cm in points
    [DocumentApp.Attribute.HORIZONTAL_ALIGNMENT]: DocumentApp.HorizontalAlignment.JUSTIFY
  });
}

This applies some of the formatting, but I’m not sure how to add the spacing before citations. Can anyone help me improve this script or suggest a better approach? It’s for an important school project. Thanks!

Your script is on the right track, but it needs a few tweaks to fully meet your requirements. Here’s a suggestion to improve it:

function applyCustomStyle() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  
  body.setAttributes({
    [DocumentApp.Attribute.FONT_FAMILY]: 'Arial',
    [DocumentApp.Attribute.FONT_SIZE]: 10,
    [DocumentApp.Attribute.LINE_SPACING]: 1,
    [DocumentApp.Attribute.INDENT_START]: 151,
    [DocumentApp.Attribute.HORIZONTAL_ALIGNMENT]: DocumentApp.HorizontalAlignment.JUSTIFY
  });
  
  var paragraphs = body.getParagraphs();
  for (var i = 0; i < paragraphs.length; i++) {
    if (isCitation(paragraphs[i].getText())) {
      paragraphs[i].setSpacingBefore(22.5);
    }
  }
}

function isCitation(text) {
  return /^\(.*?\)$/.test(text.trim());
}

This script applies all your specified formatting and adds the 1.5 spacing before citations. The isCitation function checks for parentheses at the start and end of a paragraph to identify citations. You may need to adjust this based on your specific citation format. Remember to test the script on a copy of your document first.

I’ve tackled similar custom formatting challenges in Google Docs before. Your script is a good start, but you’re right that it’s missing a few key elements. For the spacing before citations, you’ll want to use a separate function to identify citation paragraphs and apply the 1.5 spacing specifically to those.

Here’s a modified version of your script that should work:

function applyCustomStyle() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  
  body.setAttributes({
    [DocumentApp.Attribute.FONT_FAMILY]: 'Arial',
    [DocumentApp.Attribute.FONT_SIZE]: 10,
    [DocumentApp.Attribute.LINE_SPACING]: 1,
    [DocumentApp.Attribute.INDENT_FIRST_LINE]: 151,
    [DocumentApp.Attribute.HORIZONTAL_ALIGNMENT]: DocumentApp.HorizontalAlignment.JUSTIFY
  });
  
  applyCitationSpacing(body);
}

function applyCitationSpacing(body) {
  var paragraphs = body.getParagraphs();
  for (var i = 0; i < paragraphs.length; i++) {
    if (paragraphs[i].getText().match(/^\[.*\]$/)) {
      paragraphs[i].setSpacingBefore(22.5); // 1.5 line spacing
    }
  }
}

This should cover all your requirements. The citation spacing function assumes citations are in square brackets at the start of a paragraph. Adjust the regex if needed. Hope this helps with your project!

hey tom, looks like ur script is good. to add spacing for citations, loop through paragraphs and if the text fits a citation pattern, use setSpacingBefore(22.5). run it after applying the main formatting. hope it helps, cheers!