如何通过Google Apps Script完整复制含评论的Google Drive文件?
用Google Apps Script复制Google Drive文件(含评论)
默认的DriveApp.makeCopy()方法只会复制文件内容,不会包含评论和回复。要实现完整复制,需要分两步:复制文件本身,再根据文件类型单独复制评论及回复。
完整实现代码
主函数:复制文件并调用对应评论复制逻辑
function copyFileWithComments(sourceFileId, destinationFolderId) { // 复制文件本体 const sourceFile = DriveApp.getFileById(sourceFileId); const copiedFile = sourceFile.makeCopy(`${sourceFile.getName()} - 副本`, DriveApp.getFolderById(destinationFolderId)); const copiedFileId = copiedFile.getId(); const mimeType = sourceFile.getMimeType(); // 根据文件类型处理评论复制 switch(mimeType) { case MimeType.GOOGLE_DOCS: copyDocComments(sourceFileId, copiedFileId); break; case MimeType.GOOGLE_SLIDES: copySlideComments(sourceFileId, copiedFileId); break; case MimeType.GOOGLE_SHEETS: // 若不需要复制回复,用copySheetComments;需要回复则用copySheetCommentsWithReplies copySheetCommentsWithReplies(sourceFileId, copiedFileId); break; default: throw new Error("仅支持Google文档、演示文稿、电子表格类型"); } return copiedFileId; }
文档(Docs)评论复制
function copyDocComments(sourceId, targetId) { const sourceDoc = DocumentApp.openById(sourceId); const targetDoc = DocumentApp.openById(targetId); const sourceComments = sourceDoc.getComments(); sourceComments.forEach(comment => { // 匹配目标文档中对应的元素位置 const sourceElement = comment.getElement(); const elementIndex = sourceDoc.getBody().getChildIndex(sourceElement); const targetElement = targetDoc.getBody().getChild(elementIndex); if (targetElement) { // 添加主评论 const newComment = targetElement.addComment(comment.getAuthor(), comment.getContent()); // 添加所有回复 comment.getReplies().forEach(reply => { newComment.addReply(reply.getAuthor(), reply.getContent()); }); } }); }
演示文稿(Slides)评论复制
function copySlideComments(sourceId, targetId) { const sourceSlides = SlidesApp.openById(sourceId).getSlides(); const targetSlides = SlidesApp.openById(targetId).getSlides(); sourceSlides.forEach((sourceSlide, slideIndex) => { const targetSlide = targetSlides[slideIndex]; if (!targetSlide) return; const comments = sourceSlide.getComments(); comments.forEach(comment => { // 添加主评论到对应幻灯片 const newComment = targetSlide.addComment(comment.getAuthor(), comment.getContent()); // 添加回复 comment.getReplies().forEach(reply => { newComment.addReply(reply.getAuthor(), reply.getContent()); }); }); }); }
电子表格(Sheets)评论复制(含回复)
注意:默认Sheets服务无法获取评论回复,需启用高级Google服务中的Sheets API:
- 在App Script编辑器中点击「资源」→「高级Google服务」
- 找到「Google Sheets API」并开启
function copySheetCommentsWithReplies(sourceId, targetId) { // 获取原文件所有评论(含回复) const sourceComments = Sheets.Spreadsheets.Comments.list(sourceId); if (!sourceComments.comments) return; sourceComments.comments.forEach(comment => { // 构建评论请求体,保留原内容、作者、状态及回复 const requestBody = { comment: { content: comment.content, resolved: comment.resolved, replies: comment.replies.map(reply => ({ content: reply.content, author: reply.author })) }, range: comment.range }; // 添加评论到目标表格 Sheets.Spreadsheets.Comments.create(requestBody, targetId); }); }
关键注意事项
- 权限要求:脚本需要获取「查看、编辑、创建和删除你Google云端硬盘中的文件」及对应文档服务的权限
- 元素匹配:Docs的评论复制依赖元素在文档中的位置索引,若原文档和副本的结构不一致(比如复制后自动调整了格式),可能导致部分评论无法匹配
- Sheets回复限制:只有启用高级Sheets API才能复制评论回复,默认服务仅支持主评论
内容的提问来源于stack exchange,提问作者Aleksandr Pomozov
相关产品推荐
相关产品推荐

