如何在Google Docs中自动格式化动态勾选清单
实现Google Docs自动添加勾选框并调整缩进的脚本方案
操作步骤(全程无需懂代码)
- 打开你的目标Google Docs文档,点击顶部菜单栏的「扩展程序」→「Apps 脚本」
- 删除脚本编辑器里的默认代码,粘贴下面的完整脚本
- 点击编辑器顶部的「保存」按钮,给脚本起个名字(比如「清单格式化工具」)
- 回到Docs页面刷新,顶部会多出一个「清单工具」菜单,点击里面的「格式化勾选清单」即可自动完成操作
完整脚本代码
function onOpen() { // 在Docs顶部创建自定义菜单 DocumentApp.getUi() .createMenu('清单工具') .addItem('格式化勾选清单', 'formatChecklist') .addToUi(); } function formatChecklist() { const doc = DocumentApp.getActiveDocument(); const body = doc.getBody(); // 获取文档中链接的表格(默认取第一个表格,若不是第一个,把0改成对应索引,从0开始计数) const table = body.getTables()[0]; if (!table) { DocumentApp.getUi().alert('未找到表格,请检查文档内容!'); return; } // 遍历对应Sheets的C3-F12区域:表格第3行到第12行(索引2到11),C到F列(索引2到5) for (let rowIndex = 2; rowIndex <= 11; rowIndex++) { const row = table.getRow(rowIndex); if (!row) continue; for (let cellIndex = 2; cellIndex <= 5; cellIndex++) { const cell = row.getCell(cellIndex); if (!cell) continue; const cellText = cell.getText().trim(); if (cellText === '') continue; // 跳过空单元格 // 重置单元格内容,添加带勾选框的段落 cell.clear(); const checklistPara = cell.appendParagraph(cellText); // 设置为可点击的勾选框样式 checklistPara.setListStyle(DocumentApp.ListPreset.GRAY_CIRCLE_CHECKBOX); // 将缩进减少75%(保留原缩进的25%) const originalIndent = checklistPara.getIndentFirstLine(); checklistPara.setIndentFirstLine(originalIndent * 0.25); checklistPara.setIndentStart(originalIndent * 0.25); } } DocumentApp.getUi().alert('清单格式化完成!'); }
注意事项
- 如果你的链接表格不是文档中的第一个,修改脚本里
body.getTables()[0]的数字:第二个表格写1,第三个写2,以此类推 - 第一次运行脚本会触发权限申请,按照页面提示完成授权即可(这是Google官方的安全流程,放心操作)
- 缩进比例是按原缩进的25%设置的,若需要调整,把
0.25改成你想要的比例即可
内容的提问来源于stack exchange,提问作者Danny Linton
相关产品推荐
相关产品推荐

