Google Apps Script通过JSON设置Google Docs富文本:样式失效排查
Hey there! Let's break down why those three styles aren't working in your Google Apps Script function—you're really close, just a small property access mix-up is causing the issue.
The Root Cause
Looking at your setParagraphToRichText function, when applying bold, italic, and strikethrough, you're referencing properties directly on the run object:
.setBold(run.startIndex, run.endIndex, run.isBold) .setItalic(run.startIndex, run.endIndex, run.isItalic) .setStrikethrough(run.startIndex, run.endIndex, run.isStrikethrough)
But if you check your test JSON (obj), those boolean style flags aren't at the top level of each run—they're nested inside the textStyle sub-object! For example, your "cat" run has "isBold": true under textStyle, not directly on the run itself.
Since run.isBold (and the others) don't exist, they return undefined, which Google Apps Script interprets as false when setting styles. That's why those three effects aren't showing up.
The Fix
Update those three lines to access the properties from the textStyle object instead:
.setBold(run.startIndex, run.endIndex, run.textStyle.isBold) .setItalic(run.startIndex, run.endIndex, run.textStyle.isItalic) .setStrikethrough(run.startIndex, run.endIndex, run.textStyle.isStrikethrough)
A quick side note: I noticed your JSON includes both foregroundColour (nice touch for your dialect!) and foregroundColor in each textStyle. Your function uses foregroundColour, which works as long as that key is consistent in your JSON, but standardizing on one name could prevent future bugs.
Wrap-Up
It's easy to miss these nested property details when working with complex structures—glad you already realized it was a small human error rather than a Google service issue!
内容的提问来源于stack exchange,提问作者bugmagnet

