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

如何用Google Apps Script识别Google Docs中的行内代码?

解决Google Apps Script识别Markdown插入的行内代码问题
  • Google Docs里用Markdown插入的行内代码,并不是手动设置了字体和颜色,而是被标记为内置代码文本样式,所以用getForegroundColor()这类方法拿不到属性值。
  • 正确的识别方式是通过getAttributes()获取文本的样式属性,其中对应Markdown行内代码的专属属性是CODE,值为true。
  • 示例代码:
function findMarkdownInlineCode() {
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  const targetStr = "你要找的函数名";
  const match = body.findText(targetStr);

  if (match) {
    const text = match.getElement().asText();
    const start = match.getStartOffset();
    const end = match.getEndOffsetInclusive();
    const attrs = text.getAttributes(start, end);

    if (attrs[DocumentApp.Attribute.CODE]) {
      Logger.log("这是Markdown插入的行内代码");
      // 这里可以添加你的处理逻辑
    } else {
      Logger.log("找到的文本不是代码样式");
    }
  }
}
  • 补充:这种内置样式的字体和颜色是Google Docs默认配置,不会以普通字体/颜色属性的形式暴露,只能通过CODE属性识别。

内容的提问来源于stack exchange,提问作者RankkaApina

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 04:32:14