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

如何用Google脚本清除Google Docs自动编号列表的条目文本?

解决方法

Google Docs的自动编号列表是结构化元素,不是纯文本里的编号字符,所以用replaceText没法直接匹配处理。正确的做法是直接操作列表项对象,清空其文本内容但保留列表格式:

  • 遍历文档中所有的列表项(ListItem)
  • 对每个列表项执行清空文本的操作,编号会因为列表结构的保留而自动维持

以下是可行的脚本代码:

function clearNumberedListText() {
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  const allListItems = body.getListItems();

  allListItems.forEach(listItem => {
    // 清空列表项的文本,保留编号格式
    listItem.setText('');
  });
}

补充说明

如果只想处理自动编号列表(排除项目符号列表),可以增加判断条件,只清空编号类型的列表项:

function clearOnlyNumberedLists() {
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  const allListItems = body.getListItems();

  allListItems.forEach(listItem => {
    const listType = listItem.getList().getListProperties().getNestingLevel(0).getGlyphType();
    // 仅处理自动编号(NUMBER 类型)的列表项
    if (listType === DocumentApp.GlyphType.NUMBER) {
      listItem.setText('');
    }
  });
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 17:47:11