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

如何用Google App Script复制Google文档标题文本

解决Google App Script获取Google文档标题文本的问题

原代码存在的问题

  • findElement(DocumentApp.ElementType.PARAGRAPH)仅获取第一个段落,但未校验其标题格式;且searchResult.getElement()的参数用法错误,不能直接传入DocumentApp.ParagraphHeading.TITLE枚举值。
  • 缺少日志输出语句,无法确认代码执行结果。

修正后的代码

function autoFill() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  // 获取文档第一个段落
  var firstParagraph = body.getParagraphs()[0];
  // 检查该段落是否为Title格式
  if (firstParagraph.getHeading() === DocumentApp.ParagraphHeading.TITLE) {
    var titleText = firstParagraph.getText();
    // 输出标题到日志,方便验证
    Logger.log("文档标题:" + titleText);
    // 此处可添加后续表格搜索逻辑
  } else {
    Logger.log("第一个段落不是Title格式");
  }
}

验证方法

执行代码后,在脚本编辑器顶部点击「查看」→「日志」,即可看到输出的标题文本或提示信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 17:15:49