You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Google Apps Script自动将指定邮件同步到Google Sites板块?

实现Google Site邮件同步动态板块的方案

一、你提出的GAS+Google Doc嵌入方案完全可行

这个方案能满足你的需求,核心逻辑是用Google Apps Script(GAS)自动抓取符合条件的邮件,提取内容后写入Google Doc,再把Doc嵌入Google Site实现动态更新。具体操作步骤如下:

  • 步骤1:准备Google Doc
    新建一个Google Doc,调整好你想要的基础格式(比如标题样式、段落间距),确保自己拥有编辑权限。
  • 步骤2:编写GAS脚本处理邮件
    打开GAS编辑器(通过Google Doc的「工具」>「脚本编辑器」进入),编写脚本完成邮件检索与内容同步:
    1. 筛选目标邮件:用GmailApp.search()方法设置筛选规则,比如包含特定关键词、指定发件人等,示例代码:
      function syncTargetEmails() {
        // 筛选包含"特定内容"的未读邮件,可根据需求修改规则
        const mailThreads = GmailApp.search('"特定内容" is:unread');
        mailThreads.forEach(thread => {
          const messages = thread.getMessages();
          messages.forEach(msg => {
            // 提取邮件纯文本内容
            const mailContent = msg.getPlainBody();
            // 处理PDF附件
            const attachments = msg.getAttachments();
            attachments.forEach(att => {
              if (att.getContentType() === 'application/pdf') {
                // 将PDF存到Drive并生成链接插入Doc
                const savedFile = DriveApp.createFile(att);
                const pdfLink = savedFile.getUrl();
                addContentToDoc(`PDF附件:<a href="${pdfLink}" target="_blank">点击查看</a>`);
              }
            });
            // 把邮件文本内容插入Doc
            addContentToDoc(mailContent);
            // 标记邮件为已读,避免重复处理
            msg.markRead();
          });
        });
      }
      
    2. 写入内容到Google Doc:用DocumentApp操作Doc,示例代码:
      function addContentToDoc(content) {
        const docId = '你的Google Doc ID'; // 从Doc的URL中提取
        const doc = DocumentApp.openById(docId);
        const body = doc.getBody();
        // 添加分隔线、同步时间和内容,让排版更清晰
        body.appendHorizontalRule();
        body.appendParagraph(`【同步时间:${new Date().toLocaleString()}】`);
        body.appendParagraph(content);
        doc.saveAndClose();
      }
      
  • 步骤3:设置脚本自动触发
    在GAS编辑器的「编辑」>「当前项目的触发器」中,添加时间驱动触发器,比如每小时运行一次syncTargetEmails函数,实现自动同步。
  • 步骤4:嵌入Doc到Google Site
    打开你的Google Site编辑页面,选择「插入」>「从云端硬盘」,找到刚才创建的Doc,选择嵌入方式,保存后就能在站点看到动态更新的内容。

二、其他可行的OOTB方案

1. Google Sheets替代Google Doc

如果你希望结构化展示邮件内容(比如按时间、发件人排序),可以把邮件数据写入Google Sheets,再将Sheets嵌入Google Site。脚本逻辑和上面类似,只是把DocumentApp换成SpreadsheetApp操作表格即可。

2. GAS Web App直接嵌入

编写GAS Web App,直接返回格式化后的邮件HTML内容,然后嵌入Google Site。这种方式更灵活,能自定义展示样式,无需依赖Doc或Sheets:

  • 编写Web App脚本:
    function doGet() {
      const mailThreads = GmailApp.search('"特定内容"');
      let htmlContent = '<div style="padding:16px; max-width:800px; margin:0 auto;">';
      mailThreads.forEach(thread => {
        const latestMsg = thread.getMessages()[0]; // 取线程里的最新邮件
        htmlContent += `<h3 style="margin-top:24px;">${latestMsg.getSubject()}</h3>`;
        htmlContent += `<p style="color:#333; line-height:1.6;">${latestMsg.getPlainBody()}</p>`;
        // 处理PDF附件
        const attachments = latestMsg.getAttachments();
        attachments.forEach(att => {
          if (att.getContentType() === 'application/pdf') {
            const savedFile = DriveApp.createFile(att);
            htmlContent += `<p><a href="${savedFile.getUrl()}" target="_blank" style="color:#1a73e8;">查看PDF附件</a></p>`;
          }
        });
        htmlContent += '<hr style="margin:16px 0; border:none; border-top:1px solid #eee;">';
      });
      htmlContent += '</div>';
      return HtmlService.createHtmlOutput(htmlContent);
    }
    
  • 发布Web App(设置权限为「任何人,甚至匿名」),然后在Google Site中插入「嵌入」>「URL」,填入Web App的发布链接即可。

3. Gmail过滤器+Drive自动保存(无代码方案)

如果只需要同步PDF附件,完全不用写代码:设置Gmail过滤器,把包含特定内容且带PDF附件的邮件,自动将附件保存到Google Drive的指定文件夹,然后在Google Site中嵌入该文件夹(「插入」>「从云端硬盘」>「文件夹」),站点会自动展示文件夹里的PDF文件,实现动态更新。但这个方案仅支持PDF,无法同步邮件文本内容。

内容的提问来源于stack exchange,提问作者Kinkikowboi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 12:50:05