Modifying text within various brackets using Google Docs scripts

I’m trying to create a script for Google Docs that can do some complex text modifications. Here’s what I want to achieve:

  1. Find all text between single quotes and replace the quotes with guillemets (French quotes). For example:
    ‘sample text’ should become «sample text»

  2. Make all words inside a list italic.

  3. Find words in round brackets and capitalize only the first letter. For instance:
    (ABC) should change to (Abc)

I’ve seen some examples online but they don’t quite fit my needs. I’m not great with coding so I’m struggling to adapt them. Here’s a basic script I found:

function modifyText() {
  const doc = DocumentApp.getActiveDocument();
  
  // Replace quotes with guillemets
  doc.replaceText("'(.+?)'", "«$1»");
  
  // Make list items italic (this part needs work)
  const body = doc.getBody();
  const lists = body.getListItems();
  // Code to make list items italic goes here
  
  // Capitalize first letter in brackets
  doc.replaceText("\\(([a-z])([^\\)]+)\\)", function(match, p1, p2) {
    return "(" + p1.toUpperCase() + p2.toLowerCase() + ")";
  });
}

Can someone help me adjust this script to work correctly? Thanks!

I’ve worked with Google Apps Script before, and I think I can help you refine your code. For the list items, you’re on the right track, but you need to iterate through each item and apply the italic formatting. Here’s an improved version:

function modifyText() {
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();

  // Replace quotes with guillemets
  body.replaceText("'([^']+)'", '«$1»');

  // Make list items italic
  const lists = body.getListItems();
  lists.forEach(item => {
    item.editAsText().setItalic(0, item.getText().length, true);
  });

  // Capitalize first letter in brackets
  body.replaceText("\\(([a-z])([^\\)]+)\\)", (match, p1, p2) => 
    "(" + p1.toUpperCase() + p2.toLowerCase() + ")"
  );
}

This should handle all three requirements. Test it out and let me know if you need any further adjustments.

As someone who’s worked extensively with Google Apps Script, I can offer some insights on your text modification script. Your approach is solid, but there are a few tweaks we can make to improve its efficiency and reliability.

For the list items, instead of using setItalic() on the entire text, it’s better to apply it to each run within the list item. This handles cases where the list item might already have mixed formatting. Here’s how you could do it:

lists.forEach(item => {
  const textObj = item.editAsText();
  for (let i = 0; i < textObj.getNumChildren(); i++) {
    const run = textObj.getChild(i);
    run.setItalic(true);
  }
});

This ensures that existing formatting isn’t overwritten unintentionally. Also, for the parentheses capitalization, consider using a case-insensitive regex to catch all instances:

body.replaceText('(?i)\(([a-z])([^\)]+)\)', (match, p1, p2) =>
  '(' + p1.toUpperCase() + p2.toLowerCase() + ')'
);

These modifications should make your script more robust. Let me know if you need any clarification on these changes!

hey benmoore, i can help with ur script. for the list items, try this:

lists.forEach(item => {
let text = item.editAsText();
text.setItalic(0, text.getText().length - 1, true);
});

this should make all text in list items italic. hope it helps!