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

Google Apps Script开发Gmail插件:无法设置文本字体家族的问题

在Gmail插件中设置文本字体家族的正确方法

你遇到的错误是因为CardService.newTextParagraph()返回的对象并没有setFontFamily方法,这个方法属于富文本构建器RichTextValueBuilder。要给Gmail插件的文本设置字体,需要通过富文本对象来实现,具体步骤如下:

  1. 使用CardService.newRichTextValueBuilder()创建富文本构建器
  2. 调用setText()设置文本内容,再调用setFontFamily()指定字体
  3. 构建富文本值后,传入TextParagraph.setText()方法

修正后的代码示例:

function showCardWithFontFamily() {
  // 创建卡片
  var card = CardService.newCardBuilder();

  // 创建卡片区块
  var section = CardService.newCardSection();

  // 创建富文本并设置字体
  var richText = CardService.newRichTextValueBuilder()
    .setText("Hello, this is a text with custom font family.")
    .setFontFamily(CardService.FontFamily.ARIAL) // 使用枚举值或直接传字符串"Arial"
    .build();

  // 创建文本段落并传入富文本
  var textParagraph = CardService.newTextParagraph()
    .setText(richText);

  // 将文本段落添加到区块
  section.addWidget(textParagraph);

  // 将区块添加到卡片
  card.addSection(section);

  // 构建并返回卡片
  return card.build();
}

注意事项:

  • Gmail插件仅支持Google Workspace预定义的字体,你可以使用CardService.FontFamily枚举值(如ARIAL、TIMES_NEW_ROMAN、COURIER_NEW等),或直接传入对应的字体名称字符串
  • 并非所有系统字体都能生效,优先使用枚举中提供的字体类型

内容的提问来源于stack exchange,提问作者Avula Vijay Kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 22:57:09