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

如何在Node.js版Google Cloud Run函数中接收Google Sheets的POST数据?

解决Google Sheet发送数据到Cloud Run的接收问题

第一步:修复Google Sheet端的POST请求代码

你当前的Sheet代码直接把数组row作为payload传递,但UrlFetchApp不会自动将数组序列化为JSON字符串,导致发送格式不符合contentType: "application/json"的要求,这是Cloud Run端无法正确解析数据的核心原因。

修改后的Sheet代码:

const url = "<my function URL>";
// 将数组转为JSON字符串
const payload = JSON.stringify(row);
const params = {
  method: "post",
  contentType: "application/json",
  payload: payload,
};
const res = UrlFetchApp.fetch(url, params);
console.log(res.getContentText());

第二步:修改Cloud Run函数接收并处理数据

Cloud Run的HTTP函数中,req.body会自动解析JSON格式的请求体,你可以直接将其赋值给变量,之后进行HTML表格生成等后续操作。

修改后的Cloud Run代码:

const functions = require('@google-cloud/functions-framework');

functions.http('helloHttp', (req, res) => {
  // 接收Sheet发送的行数据数组
  const receivedRow = req.body;
  
  // 示例:将数组转为HTML表格行
  const tableRow = `<tr>${receivedRow.map(cell => `<td>${cell}</td>`).join('')}</tr>`;
  
  // 这里可添加发送到CMS的逻辑
  console.log('接收到的行数据:', receivedRow);
  console.log('生成的HTML表格行:', tableRow);
  
  res.send(`已接收数据:${JSON.stringify(receivedRow)}`);
});

验证方式

  1. 在Sheet中触发编辑事件发送请求
  2. 查看Cloud Run日志,此时能看到完整的接收到的行数据数组内容
  3. Sheet的日志中可查看函数返回的响应数据

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 08:42:05