如何使用Google Apps Script编写函数清空Google文档全部内容?
Google Docs 文档清空功能实现代码
你可以直接调用 Google Apps Script 内置的Body.clear()方法实现正文全清,修改后的完整purge函数如下:
function purge() { // 获取当前激活的文档实例 const currentDoc = DocumentApp.getActiveDocument(); // 获取文档正文对象后调用清空方法 currentDoc.getBody().clear(); DocumentApp.getUi().alert('文档已全部清空完成'); }
可选扩展:同时清空页眉页脚
如果需要把页眉、页脚的内容也一起清空,可以使用扩展版本:
function purge() { const currentDoc = DocumentApp.getActiveDocument(); // 清空正文 currentDoc.getBody().clear(); // 遍历清空所有页眉 currentDoc.getHeaders().forEach(header => { if (header) header.clear(); }); // 遍历清空所有页脚 currentDoc.getFooters().forEach(footer => { if (footer) footer.clear(); }); DocumentApp.getUi().alert('文档全部内容(含页眉页脚)已清空完成'); }
你原来的代码里调用purge()没有传参,所以这里的purge函数不需要设置形参,直接适配你现有的调用逻辑即可。
内容的提问来源于stack exchange,提问作者ausomecodes
相关产品推荐
相关产品推荐

