Google Workspace 自动化,支持 Gmail、日历、Drive、表格,可通过服务账户或 OAuth 完成读取、写入、发送并管理整个 Google 套件。
Google 工作空间 : 自动ate Gmail、 Google 日历、 Google 驱动器和来自命令行的 Google 工作表是一项面向实际任务的技能,主要用于读取并发送电子邮件, 创建日历事件, 管理 Drive 文件, 并读写工作表。它将相关步骤、工具调用和结果整理方式集中到统一流程中,帮助使用者更快完成目标并减少重复操作。
实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。
执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
通过命令行自动化操作 Gmail、Google Calendar、Google Drive 和 Google Sheets。支持读取与发送邮件、创建日历事件、管理 Drive 文件,以及读写 Sheets 数据 —— 全部基于 Google API Python 客户端,并使用服务账号认证。
export GOOGLE_SERVICE_ACCOUNT_JSON='{"type":"service_account","project_id":"...","private_key_id":"...","private_key":"-----BEGIN RSA PRIVATE KEY-----n...","client_email":"...@....iam.gserviceaccount.com","client_id":"...","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token"}'
创建路径:Google Cloud Console → IAM → 服务账号 → 创建 → 添加 JSON 密钥。
随后启用以下 API:Gmail API、Calendar API、Drive API、Sheets API。
对于 Gmail / Calendar / Drive:需在 Google Workspace 管理控制台中启用全域委托(domain-wide delegation),并添加相应权限范围(scopes)。
export GOOGLE_CREDENTIALS_FILE="/path/to/credentials.json" export GOOGLE_TOKEN_FILE="/path/to/token.json" # 首次运行时自动生成
# ── GMAIL ─────────────────────────────────────────────── # 读取收件箱(最新 10 封邮件) python3 scripts/google_workspace.py gmail-inbox python3 scripts/google_workspace.py gmail-inbox --limit 25 # 搜索邮件 python3 scripts/google_workspace.py gmail-search --query "from:boss@company.com subject:urgent" python3 scripts/google_workspace.py gmail-search --query "is:unread" --limit 20 # 发送邮件 python3 scripts/google_workspace.py gmail-send --to "client@example.com" --subject "Meeting Tomorrow" --body "Hi, just confirming our meeting at 2pm." python3 scripts/google_workspace.py gmail-send --to "a@x.com" --subject "Report" --body "See attached." --attachment "/path/to/report.pdf" # 为邮件添加标签 python3 scripts/google_workspace.py gmail-label --message-id "18abc123..." --label "Important" # 创建草稿 python3 scripts/google_workspace.py gmail-draft --to "client@example.com" --subject "Draft Subject" --body "Draft content here." # ── CALENDAR ──────────────────────────────────────────── # 列出即将发生的日程事件 python3 scripts/google_workspace.py cal-list python3 scripts/google_workspace.py cal-list --days 14 --limit 20 # 创建日程事件 python3 scripts/google_workspace.py cal-create --title "Team Standup" --start "2024-03-15T09:00:00" --end "2024-03-15T09:30:00" --timezone "Australia/Brisbane" python3 scripts/google_workspace.py cal-create --title "All Day Event" --start "2024-03-20" --all-day # 更新日程事件 python3 scripts/google_workspace.py cal-update --event-id "abc123..." --title "Updated Title" --start "2024-03-15T10:00:00" --end "2024-03-15T10:30:00" # 删除日程事件 python3 scripts/google_workspace.py cal-delete --event-id "abc123..." # ── DRIVE ─────────────────────────────────────────────── # 列出文件 python3 scripts/google_workspace.py drive-list python3 scripts/google_workspace.py drive-list --query "name contains 'report'" --limit 20 # 上传文件 python3 scripts/google_workspace.py drive-upload --file ./report.pdf python3 scripts/google_workspace.py drive-upload --file ./report.pdf --folder-id "1BxiMVs0XRA5..." # 下载文件 python3 scripts/google_workspace.py drive-download --file-id "1BxiMVs0XRA5..." --output ./downloaded.pdf # 分享文件 python3 scripts/google_workspace.py drive-share --file-id "1BxiMVs0XRA5..." --email "colleague@company.com" --role writer python3 scripts/google_workspace.py drive-share --file-id "1BxiMVs0XRA5..." --anyone-link # ── SHEETS ────────────────────────────────────────────── # 读取指定区域数据 python3 scripts/google_workspace.py sheets-read --spreadsheet-id "1BxiMVs0XRA5..." --range "Sheet1!A1:D10" # 向指定区域写入数据 python3 scripts/google_workspace.py sheets-write --spreadsheet-id "1BxiMVs0XRA5..." --range "Sheet1!A1" --values '[["Name","Score"],["Alice",95],["Bob",87]]' # 追加一行数据 python3 scripts/google_workspace.py sheets-append --spreadsheet-id "1BxiMVs0XRA5..." --range "Sheet1!A:D" --values '["John","Doe","john@example.com",42]'
google-api-python-client(执行 pip install google-api-python-client google-auth google-auth-httplib2 安装)GOOGLE_SERVICE_ACCOUNT_JSON 环境变量(或 OAuth 凭据)