如何通过脚本让Google Sheets评论者接收所有评论通知?
解决方案
完全可以通过Google Apps Script实现这个需求,脚本能自动抓取表格所有评论,提取你需要的信息,并以和所有者收到的官方邮件类似的格式,批量发送给所有拥有Commenter权限的协作者。
实现步骤
- 提取表格所有评论的关键信息:评论者姓名、文件名、单元格位置、评论内容、评论ID、文件ID
- 筛选出所有拥有Commenter权限的协作者邮箱
- 模仿官方邮件的HTML格式构建内容,包含回复和跳转至评论位置的链接(用
fileId和commentId构造) - 批量发送HTML格式邮件给所有目标协作者
完整脚本示例
function sendAllCommentsToCommenters() { const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); const fileId = spreadsheet.getId(); const fileName = spreadsheet.getName(); // 需先在脚本编辑器的「服务」中添加Drive API(v2版本) const comments = Drive.Comments.list(fileId, {fields: 'items(author(displayName,email),content,context,id)'}).items; const commenters = Drive.Permissions.list(fileId, {fields: 'items(emailAddress,role)'}).items .filter(perm => perm.role === 'commenter') .map(perm => perm.emailAddress); if (!comments.length) { Logger.log("无评论可发送"); return; } // 构建单条评论的HTML模块 const getCommentHtml = (comment) => { const cellLoc = comment.context.type === 'cell' ? comment.context.value : '无关联单元格'; const replyUrl = `https://docs.google.com/spreadsheets/d/${fileId}/edit#comment=${comment.id}&mode=reply`; const openUrl = `https://docs.google.com/spreadsheets/d/${fileId}/edit#comment=${comment.id}`; return ` <div style="font-family: Arial, sans-serif; margin: 16px 0;"> <p><strong>${comment.author.displayName}</strong> 在文件 <strong>${fileName}</strong> 中发表了评论:</p> <p><strong>单元格:</strong>${cellLoc}</p> <p><strong>内容:</strong>${comment.content}</p> <p> <a href="${replyUrl}" style="padding: 8px 16px; background: #1a73e8; color: #fff; text-decoration: none; border-radius: 4px;">回复评论</a> <a href="${openUrl}" style="margin-left: 10px; padding: 8px 16px; background: #f1f3f4; color: #202124; text-decoration: none; border-radius: 4px;">查看评论位置</a> </p> </div> `; }; // 给每个评论者发送全评论汇总 commenters.forEach(email => { const subject = `${fileName} 所有评论汇总`; let body = '<h3>以下是该文件的全部评论:</h3><hr style="border: 1px solid #eee;">'; comments.forEach(comment => { body += getCommentHtml(comment) + '<hr style="border: 1px solid #eee;">'; }); MailApp.sendEmail({ to: email, subject: subject, htmlBody: body }); }); Logger.log("邮件发送完成"); }
使用指南
- 打开目标Google Sheets,点击「扩展程序」→「Apps脚本」进入编辑器
- 点击编辑器左侧「服务」→ 添加「Drive API」(选择v2版本)
- 粘贴上述代码,保存后点击运行。首次运行需完成权限授权,按提示操作即可
- 如需定期自动发送,可设置时间触发器(编辑器左侧「触发器」→ 添加触发器)
注意事项
- 脚本需要访问Drive评论、权限列表以及发送邮件的权限,授权时需确认
- 邮件样式可根据官方邮件调整,让格式更贴近原生通知
- 若评论数量极大,可拆分邮件或优化内容展示,避免邮件过长
内容的提问来源于stack exchange,提问作者dek
相关产品推荐
相关产品推荐

