You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Apps Script通过JSON设置Google Docs富文本:样式失效排查

Why Your Bold, Italic, and Strikethrough Styles Aren't Applying

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 20:52:55