如何通过Apps Script实现Google Forms富文本格式设置
Google Forms Apps Script 富文本标题设置方案
Google Forms已新增标题和描述的富文本格式支持,但直接通过setTitle传入HTML或伪RTF格式字符串无法生效——该方法仅能解析换行符\n,不识别其他格式标记。
要实现富文本格式,需通过FormApp.newRichTextValue()构建富文本对象,再传递给标题设置方法,以下是具体示例:
基础加粗标题示例
// 替换为你的表单ID const form = FormApp.openById("YOUR_FORM_ID"); const questionHeader = form.addSectionHeaderItem(); const question = "需要加粗的标题文本"; // 构建富文本内容,给整个标题设置加粗 const richText = FormApp.newRichTextValue() .setText(question) .setBold(0, question.length) .build(); // 应用富文本标题 questionHeader.setTitle(richText);
混合格式示例(部分加粗+换行)
如果需要给文本的部分内容设置格式,或结合换行,可以这样写:
const form = FormApp.openById("YOUR_FORM_ID"); const questionHeader = form.addSectionHeaderItem(); const text = "重点内容:加粗显示\n这是第二行普通文本"; const richText = FormApp.newRichTextValue() .setText(text) .setBold(5, 9) // 给"加粗显示"这四个字设置加粗 .build(); questionHeader.setTitle(richText);
支持的其他格式
除了加粗,还可以通过以下方法设置其他格式:
setItalic(startIndex, endIndex):斜体setUnderline(startIndex, endIndex):下划线setForegroundColor(startIndex, endIndex, color):字体颜色(支持十六进制色值,如#FF0000)setBackgroundColor(startIndex, endIndex, color):背景颜色
内容的提问来源于stack exchange,提问作者Steven Wong
相关产品推荐
相关产品推荐

