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

如何基于日期列触发Google Sheets向Webhook发送JSON行数据

可以实现,具体方案如下:

核心逻辑

利用Google Apps Script编写自定义脚本,搭配时间驱动触发器(或数据变更触发器),自动检查表格中日期列与当日日期是否匹配,将符合条件的行数据转换为指定JSON格式后,通过UrlFetchApp发送到目标Webhook URL。


具体操作步骤

1. 编写Apps Script代码

打开目标Google Sheet,点击「扩展程序」→「Apps Script」,替换默认代码为以下内容(需根据你的表格结构调整列索引和字段映射):

function checkDateAndSendWebhook() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const dataRange = sheet.getDataRange();
  const values = dataRange.getValues();
  const today = new Date();
  // 格式化今日日期为YYYY-MM-DD,需和表格中日期格式统一
  const formattedToday = Utilities.formatDate(today, Session.getScriptTimeZone(), "yyyy-MM-dd");

  // 跳过表头,从第二行开始遍历
  for (let i = 1; i < values.length; i++) {
    const row = values[i];
    const targetDate = row[0]; // 假设日期在第1列,根据实际修改列索引
    const formattedTargetDate = Utilities.formatDate(targetDate, Session.getScriptTimeZone(), "yyyy-MM-dd");

    // 日期匹配时执行发送逻辑
    if (formattedToday === formattedTargetDate) {
      // 构造符合Webhook要求的JSON结构
      const payload = {
        "data": {
          "header_image_url": row[1] // 假设图片链接在第2列
        },
        "recipients": [
          {
            "whatsapp_number": row[2], // 假设手机号在第3列
            "attributes": {
              "first_name": row[3], // 假设名字在第4列
              "last_name": row[4] // 假设姓氏在第5列
            },
            "lists": ["Default"],
            "tags": ["new lead", "notification sent"]
          }
        ]
      };

      const requestOptions = {
        method: "post",
        contentType: "application/json",
        payload: JSON.stringify(payload)
      };

      // 发送请求并处理结果
      try {
        const response = UrlFetchApp.fetch("你的Webhook目标URL", requestOptions);
        // 在第6列标记发送状态,避免重复触发
        sheet.getRange(i+1, 6).setValue("已发送");
        console.log("请求成功:" + response.getContentText());
      } catch (error) {
        sheet.getRange(i+1, 6).setValue("发送失败");
        console.error("请求出错:" + error.toString());
      }
    }
  }
}

2. 设置时间驱动触发器

  • 在Apps Script界面点击左侧「触发器」图标
  • 点击「添加触发器」,配置参数:
    • 选择运行函数:checkDateAndSendWebhook
    • 事件源:「时间驱动」
    • 时间类型:「每天」
    • 执行时间:设置为每日固定时段(比如凌晨),时区需与表格一致
  • 保存触发器,脚本会按设定时间自动执行

3. 测试与优化

  • 手动运行一次checkDateAndSendWebhook函数,验证日期匹配和请求发送逻辑
  • 根据表格实际列顺序、数据格式,调整代码中的列索引和字段映射
  • 若需避免重复发送,可通过状态列(如示例中的第6列)过滤已处理的行

注意事项

  • 确保表格中的日期是可识别的日期类型,而非纯文本格式
  • 注意Google Apps Script的UrlFetchApp配额限制,避免短时间内大量请求
  • 若Webhook需要身份认证,需在requestOptions中添加对应请求头(如Authorization)
  • 若需在数据更新时实时检查,可将触发器改为「 onChange 」类型

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 08:10:28