如何创建可嵌入Google Sheets的Gmail邮件直连链接及自研脚本方案?
解决方案
一、生成可直接打开Gmail邮件的Sheets链接
手动快速获取(无需脚本)
打开目标邮件后,地址栏URL里会有一串类似18a1b2c3d4e5f678的唯一ID(位于#inbox/之后),用这个ID构造链接:
https://mail.google.com/mail/u/0/#inbox/[邮件ID]
复制该链接到Sheets单元格,Sheets会自动识别为可点击超链接;也可以用HYPERLINK公式让链接更直观:
=HYPERLINK("https://mail.google.com/mail/u/0/#inbox/18a1b2c3d4e5f678", "查看任务邮件")
批量处理脚本(Google Apps Script)
如果需要批量处理选中的邮件,可通过以下脚本一键复制可直接粘贴到Sheets的链接:
- 打开Google Apps Script(Gmail右上角齿轮→查看所有设置→高级→启用Google Apps Script,或直接访问script.google.com)
- 创建新项目,替换默认代码为:
function copyGmailLinksToClipboard() { const threads = GmailApp.getSelection().getThreads(); let links = []; threads.forEach(thread => { const messages = thread.getMessages(); const messageId = messages[messages.length - 1].getId(); const link = `=HYPERLINK("https://mail.google.com/mail/u/0/#inbox/${messageId}", "${thread.getFirstMessageSubject()}")`; links.push(link); }); const clipboardText = links.join("\n"); const htmlOutput = HtmlService.createHtmlOutput(` <script> navigator.clipboard.writeText('${clipboardText.replace(/'/g, "\\'")}'); window.close(); </script> `); SpreadsheetApp.getUi().showModalDialog(htmlOutput, "链接已复制"); }
- 保存脚本后,在Gmail选中目标邮件,点击右上角扩展程序→运行该脚本,授权后即可复制所有邮件的Sheets超链接公式,直接粘贴到Sheets即可。
二、自研脚本生成Gmail邮件PDF副本
用Google Apps Script可一键生成邮件PDF并保存到Drive,甚至关联到Sheets:
基础脚本示例
function saveSelectedEmailsAsPDF() { const threads = GmailApp.getSelection().getThreads(); const folder = DriveApp.getFolderById("你的Drive文件夹ID"); // 替换为你的目标文件夹ID threads.forEach(thread => { const messages = thread.getMessages(); const latestMessage = messages[messages.length - 1]; const subject = latestMessage.getSubject().replace(/[\/\\:*?"<>|]/g, "_"); // 处理文件名非法字符 const htmlBody = latestMessage.getBody(); const blob = Utilities.newBlob(htmlBody, "text/html", `${subject}.pdf`) .getAs("application/pdf"); const file = folder.createFile(blob); // 如需同步到Sheets,可在此添加写入单元格的逻辑 console.log(`PDF已保存:${file.getUrl()}`); }); SpreadsheetApp.getUi().alert("PDF生成完成"); }
优化方向
- 可添加附件嵌入PDF的逻辑,需额外处理附件的读取与整合
- 若要同步到Sheets,可在脚本中获取当前活动行,将PDF链接写入对应单元格
- 可为脚本添加Gmail侧边栏,直接在邮箱界面操作,无需打开脚本编辑器
内容的提问来源于stack exchange,提问作者RoosterMagic22
相关产品推荐
相关产品推荐

