Google Docs Apps Script for finding and replacing text within different bracket types

I found this helpful script for basic text replacement in Google Docs but I need to modify it for more complex operations. My English isn’t perfect so please bear with me.

function processDocument() {
  const document = DocumentApp.getActiveDocument();
  
  const substitutions = [
    {find: 'PM', replace: 'pm'},
    {find: '\\--', replace: '-'},
    {find: ':30', replace: ''},
  ]
  
  substitutions.forEach(sub => document.replaceText(sub.find, sub.replace));
}

I need help with three specific tasks that I used to do with Word macros:

First task: Find text between quotation marks and replace the quotes with guillemets (« »). The content inside should stay unchanged.
Example: “some text here” becomes «some text here»

Second task: Locate words inside lists and make them italic automatically.

Third task: Find text in parentheses and change only the first letter to uppercase while making the rest lowercase.
Example: (ABC) should become (Abc)

I tried converting my old VBA macro but Google Apps Script works differently. Can someone help me write these functions? I would really appreciate any guidance on how to implement these text transformations.

Working with list formatting in Google Docs through Apps Script is way trickier than other tasks. The document structure treats list items differently than regular paragraphs, so you’ve got to check for list elements specifically. For italicizing words in lists, iterate through list items and apply formatting to the text elements. Check if element.getType() === DocumentApp.ElementType.LIST_ITEM then access the text within those elements. What caught me off guard switching from VBA - Google Apps Script processes document structure instead of raw text. You work with the element hierarchy: paragraphs, list items, text runs, etc. FlyingLeaf’s findText approach works great for simple text replacement, but for formatting changes like italics you need methods like setItalic(true) on specific text ranges. For the parentheses case conversion, adapt the manual loop approach but use string manipulation functions to handle capitalization before inserting replacement text.

Hey emmat83! For the parentheses thing, try regex capture groups: document.replaceText('\(([A-Za-z]+)\)', function(match, p1) { return '(' + p1.charAt(0).toUpperCase() + p1.slice(1).toLowerCase() + ')'; }); Apps Script might not support callback functions in replaceText tho, so u’d probably need findText() and loop through the matches manually.

The main issue is Google Apps Script’s replaceText method doesn’t support callback functions like Alex mentioned. I hit the same problems migrating from VBA. You’ll need findText() with a loop for complex replacements.

For quotation marks, this works better:

function replaceQuotes() {
  const body = DocumentApp.getActiveDocument().getBody();
  let searchResult = body.findText('"([^"]+)"');
  
  while (searchResult) {
    const element = searchResult.getElement();
    const startOffset = searchResult.getStartOffset();
    const endOffset = searchResult.getEndOffsetInclusive();
    
    const foundText = element.asText().getText().substring(startOffset, endOffset + 1);
    const innerText = foundText.slice(1, -1);
    const replacement = '«' + innerText + '»';
    
    element.asText().deleteText(startOffset, endOffset);
    element.asText().insertText(startOffset, replacement);
    
    searchResult = body.findText('"([^"]+)"', searchResult);
  }
}

This gives you more control and works reliably with Google Docs formatting.