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

如何通过Google Script从本地桌面/文件夹添加可编辑Excel附件?

解决Google Script无法直接添加本地Excel作为邮件附件的问题

我懂你的困扰——Google Apps Script是云端运行的脚本,本身没有权限访问你本地电脑的文件系统,这就是为啥你之前只能从Google Drive拉取Excel,或者只能添加PDF(估计是用了上传后自动转格式的临时方案)。要实现直接从桌面/本地文件夹添加可编辑Excel附件,得换个思路,下面给你两个可行的方案:


方案1:用Web App搭建「本地文件上传+邮件发送」界面

这个方案不用手动把文件传到Drive,而是通过网页临时上传本地Excel,脚本直接用上传的Blob对象发邮件,附件会保持可编辑的Excel格式。

步骤1:编写Google Script代码

新建一个Google Apps Script项目,替换默认代码为:

// 生成上传页面
function doGet() {
  return HtmlService.createHtmlOutputFromFile('UploadForm');
}

// 处理文件上传和邮件发送
function sendEmailWithAttachment(emailAddress, subject, body, copy, file) {
  try {
    // 将上传的文件转为Blob(云端临时存储,不会保存到Drive)
    var blob = file.getBlob();
    // 发送邮件
    GmailApp.sendEmail(emailAddress, subject, body, {
      cc: copy,
      attachments: [blob]
    });
    return "邮件发送成功!";
  } catch (e) {
    return "发送失败:" + e.toString();
  }
}

步骤2:创建上传页面HTML文件

点击左侧菜单栏的「文件」→「新建」→「HTML文件」,命名为UploadForm,粘贴以下内容:

<!DOCTYPE html>
<html>
  <body>
    <h3>上传Excel并发送邮件</h3>
    <label>收件人邮箱:</label><input type="email" id="email" required><br><br>
    <label>邮件主题:</label><input type="text" id="subject" required><br><br>
    <label>邮件正文:</label><textarea id="body" rows="5" required></textarea><br><br>
    <label>抄送邮箱:</label><input type="email" id="copy"><br><br>
    <label>上传Excel文件:</label><input type="file" id="excelFile" accept=".xlsx,.xls" required><br><br>
    <button onclick="sendEmail()">发送邮件</button>
    <div id="status"></div>

    <script>
      function sendEmail() {
        var email = document.getElementById('email').value;
        var subject = document.getElementById('subject').value;
        var body = document.getElementById('body').value;
        var copy = document.getElementById('copy').value;
        var file = document.getElementById('excelFile').files[0];

        google.script.run.withSuccessHandler(function(response) {
          document.getElementById('status').innerHTML = response;
        }).withFailureHandler(function(error) {
          document.getElementById('status').innerHTML = "出错了:" + error;
        }).sendEmailWithAttachment(email, subject, body, copy, file);
      }
    </script>
  </body>
</html>

步骤3:部署为Web App

  1. 点击右上角「部署」→「新部署」
  2. 类型选择「Web应用」
  3. 执行权限选「我自己」(如果需要其他人用可以调整)
  4. 访问权限选「任何人」(仅自己用的话选「只有我」)
  5. 点击部署,复制生成的Web链接

使用方式

打开这个Web链接,填写邮件信息,选择桌面的Excel文件,点击发送即可——文件会临时传到云端处理,不会保存到你的Drive里,附件是完整的可编辑Excel格式。


方案2:用本地脚本完全绕开Google生态(更直接)

如果不想依赖Google的云端工具,可以用本地脚本语言(比如Python)直接读取本地Excel并发送邮件,完全不用碰Google Drive或Google Script。

Python示例代码

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders

# 配置你的邮箱信息
sender_email = "你的Gmail邮箱@gmail.com"
sender_password = "你的Gmail App密码"  # 需开启两步验证后生成App密码
recipient_email = "收件人邮箱@xxx.com"
cc_email = "抄送邮箱@xxx.com"
subject = "你的邮件主题"
body = "你的邮件正文内容"
excel_file_path = "C:/Users/你的用户名/Desktop/目标文件.xlsx"  # 本地Excel文件路径

# 构建邮件对象
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = recipient_email
msg['Cc'] = cc_email
msg['Subject'] = subject

# 添加邮件正文
msg.attach(MIMEText(body, 'plain'))

# 添加Excel附件
with open(excel_file_path, "rb") as attachment:
    part = MIMEBase('application', 'octet-stream')
    part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f"attachment; filename= {excel_file_path.split('/')[-1]}")
msg.attach(part)

# 发送邮件
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
    server.login(sender_email, sender_password)
    server.send_message(msg)
print("邮件发送成功!")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:54:15