# Google Apps Script 接入 Server酱：Gmail/Sheets/Calendar 事件推送到微信（2026 教程）

> 用 Google Apps Script 的 UrlFetchApp 调用 Server酱 API，将 Gmail 邮件、Sheets 编辑、Calendar 日程等事件实时推送到微信。

- 网页版：https://sct.ftqq.com/docs/integrations/google-apps-script/
- 更新日期：2026-07-17

# Google Apps Script + Server酱：把 Google Workspace 事件推送到微信

> 效果：Gmail 收到特定邮件、Sheets 数据变更或 Calendar 日程提醒时，自动通过 Server酱 推送消息到微信。

## 前置条件

- **Server酱 SendKey**：登录 sct.ftqq.com，在「SendKey」页面复制。详见 [30 秒获取 SendKey](https://sct.ftqq.com/docs/getting-started/sendkey/)。
- **Google 账号**：需有访问 Gmail / Google Sheets / Google Calendar 等 Workspace 服务的权限。
- Server酱 支持推送到微信服务号/企业微信/钉钉/飞书等多个通道，详见 [/getting-started/channels/](https://sct.ftqq.com/docs/getting-started/channels/)。

## 配置步骤

### 1. 将 SendKey 存入 Script Properties

打开 Apps Script 编辑器，在编辑器中运行一次以下函数，将 SendKey 写入脚本属性：

```javascript
function setupSendKey() {
  PropertiesService.getScriptProperties().setProperty('SERVERCHAN_SENDKEY', '你的SENDKEY');
}
```

运行后即完成写入。

### 2. 编写推送函数

在同一个脚本项目中新建函数，用 UrlFetchApp.fetch() 调用 Server酱 API：

```javascript
function sendServerChan(title, desp) {
  var sendKey = PropertiesService.getScriptProperties().getProperty('SERVERCHAN_SENDKEY');
  var url = 'https://sctapi.ftqq.com/' + sendKey + '.send';
  UrlFetchApp.fetch(url, {
    method: 'post',
    payload: {
      title: title,
      desp: desp
    },
    muteHttpExceptions: true
  });
}
```

### 3. 配置触发器

可根据需要配置时间驱动触发器或事件触发器（如 onEdit），在触发函数中调用 sendServerChan 推送消息。在 Apps Script 编辑器中添加触发器。

## 完整示例

以下示例展示如何调用 sendServerChan 发送一条测试消息：

```javascript
function testNotification() {
  sendServerChan('测试标题', '这是一条来自 Google Apps Script 的测试消息。');
}
```

配置时间驱动触发器，定时运行 `testNotification` 即可。

> 如果使用 Server酱³（SendKey 以 sctp 开头），API 地址改为 `https://你的UID.push.ft07.com/send/你的SENDKEY.send`，其中 UID 是 sctp 后面的数字。

## 常见问题

**SendKey 应该硬编码在脚本里吗？**

不建议。推荐使用 PropertiesService.getScriptProperties() 将 SendKey 存储为脚本属性，避免在代码中暴露密钥。

**消息没有收到怎么办？**

先检查 SendKey 是否正确写入 Script Properties，再确认 UrlFetchApp.fetch 返回的响应 JSON 中 code 是否为 0。更多排查步骤见 [/getting-started/faq/](https://sct.ftqq.com/docs/getting-started/faq/)。

**每天能发多少条？**

免费额度为每天 5 条，每分钟上限 50 条。订阅会员目前特价最低 3 元/月，一天最多可发 1000 条（[价格详情](https://sct.ftqq.com/subscribe)）。

**触发器执行了但没收到推送？**

检查 Apps Script 执行记录，确认 UrlFetchApp.fetch 是否报错。Gmail 场景需注意首次运行授权，未授权的脚本不会执行网络请求。

## 相关集成

- [/integrations/python/](https://sct.ftqq.com/docs/integrations/python/) —— Python 脚本推送
- [/integrations/qinglong/](https://sct.ftqq.com/docs/integrations/qinglong/) —— 青龙面板任务通知
- [/integrations/uptime-kuma/](https://sct.ftqq.com/docs/integrations/uptime-kuma/) —— Uptime Kuma 宕机告警


## 懒人方案：把这段话复制给你的 AI 助手

把下面这段文字原样粘贴给 Claude Code、Cursor 或任何 AI 编程助手，它就会替你完成 Server酱 的接入：

```text
请帮我把 Server酱 微信通知接入到我当前的项目/工具中：
1. API：POST https://sctapi.ftqq.com/{SendKey}.send，参数 title（必填，不能含换行）、desp（正文，支持 Markdown，可选）；成功时返回 JSON 的 code 为 0。
2. 如果我的 SendKey 以 sctp 开头，则改用 https://{uid}.push.ft07.com/send/{SendKey}.send，uid 是 SendKey 中 sctp 与 t 之间的数字。
3. SendKey 从环境变量 SERVERCHAN_SENDKEY 读取；如果没有，提醒我到 https://sct.ftqq.com 免费获取（每天 5 条额度）。
4. 安全要求：不要把 SendKey 硬编码进代码、不要打印到日志、不要提交到 git。
5. 频率约定：任务结束发 1 条结果摘要即可，批量任务合并成一条，不要每步都推。
请根据我当前的项目和场景直接写好代码/配置并验证。
```

