I’m working on a Google Apps Script that adds text to a document, but I’m having trouble with formatting. I need to combine three different text parts where only the middle section should be formatted with italics.
My goal: Create text like “Hello there friend” where only the middle word is italicized.
The problem: When I use appendText() to add the final part, it inherits the italic formatting from the previous section. I’ve tried different approaches with insertText() but keep running into issues.
Here’s what I’m working with:
function formatTextExample() {
var document = DocumentApp.getActiveDocument();
var bodyElement = document.getBody();
var firstPart = "Start text ";
var middlePart = "emphasized content";
var lastPart = " end text here";
if (firstPart !== "") {
var partOneLength = firstPart.length;
var partTwoLength = middlePart.length;
var totalOffset = partOneLength + partTwoLength;
if (middlePart.length > 0) {
var addFirst = bodyElement.insertParagraph(0, firstPart);
var addSecond = addFirst.appendText(middlePart);
var makeItalic = addSecond.editAsText().setItalic(true);
// These attempts didn't work:
// var attempt1 = makeItalic.appendText(lastPart); // makes lastPart italic too
// var attempt2 = makeItalic.insertText(partTwoLength, lastPart); // still formats lastPart
}
}
}
How can I add the third text segment without it picking up the italic formatting from the second part?
This happens because setItalic(true) formats the entire text element when you don’t specify a range. You’re trying to format after appending text, but you need to format specific character ranges instead.
Here’s how to fix it: append all your text parts first, then apply italic formatting only to the middle section’s character range. Use setItalic(startOffset, endOffsetExclusive, true) to target just that portion.
var paragraph = bodyElement.insertParagraph(0, firstPart + middlePart + lastPart);
var startPos = firstPart.length;
var endPos = startPos + middlePart.length;
paragraph.editAsText().setItalic(startPos, endPos, true);
This way you’re formatting exactly the character range you want while leaving everything else alone. I use this approach all the time for reports with mixed formatting.
yeah, ur right! when u use setItalic after appending, everything gets italicized. u should reset it after adding that last part. use setItalic(false) to target the range where u don’t want italics. try something like makeItalic.editAsText().setItalic(totalOffset, totalOffset + lastPart.length, false)!
This happens because Google Apps Script carries over text formatting when you call setItalic(true) on a text element. It messes with the formatting context for whatever you do next.
Here’s what worked for me: build your complete string first, then use getText() and setText() together. Don’t try to build it piece by piece - construct the full text by combining all parts, replace the paragraph content, then apply formatting to specific ranges.
var fullText = firstPart + middlePart + lastPart;
var paragraph = bodyElement.insertParagraph(0, fullText);
var textElement = paragraph.editAsText();
textElement.setItalic(firstPart.length, firstPart.length + middlePart.length, true);
This skips the formatting inheritance problem completely since you’re working with the complete text upfront instead of building it step by step. I’ve had way better luck with this approach for complex formatting in Google Docs automation.