如何通过Apps Script用HTML样式设置Google Form描述格式
答案:不能直接使用HTML代码,需用富文本API实现格式化
Google Forms的Apps Script服务(FormApp)不支持直接解析HTML标签来格式化标题、描述或问题文本。你代码里的<b>、<i>这类标签会被当作普通字符串显示,不会转换成对应的加粗、斜体样式。
要实现文本格式化,需要使用FormApp提供的RichTextValueBuilder类来构建富文本内容,以下是修改后的示例代码:
function editForm() { var formURL = sheetRespon.getFormUrl(); var form = FormApp.openByUrl(formURL); form.setTitle("FORMULIR JURNAL PKL SMK INDONESIA"); // 构建带格式的富文本描述 var richDescription = FormApp.createRichTextValue() .setText("This is sample ") // 添加加粗文本 .startText() .setBold(true) .setText("deskripsi") .endText() .setText(" formulir with ") // 添加斜体文本 .startText() .setItalic(true) .setText("italic text") .endText() .setText(" and ") // 添加加粗+斜体文本 .startText() .setBold(true) .setItalic(true) .setText("Bold and italic text") .endText() .build(); form.setDescription(richDescription); }
额外格式实现说明:
- 换行:直接在文本中加入
\n转义字符即可,比如.setText("第一行文本\n第二行文本") - 超链接:在
startText()后调用setLinkUrl("目标URL"),示例:.startText() .setLinkUrl("https://example.com") .setText("点击跳转示例链接") .endText()
所有格式化操作都需要通过RichTextValueBuilder的链式方法完成,无法直接使用HTML语法。
内容的提问来源于stack exchange,提问作者PSAB Plalangan
相关产品推荐
相关产品推荐

