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

无法修改getSheetData_函数,如何从谷歌表格取最后一行数据发WhatsApp?

修改Google Apps Script函数仅获取表格最后一行数据

修改后的代码实现

const getSheetData_ = () => {
  const [header, ...rows] = SpreadsheetApp.getActiveSheet().getDataRange().getDisplayValues();
  const data = [];
  // 仅处理表格最后一行数据
  if (rows.length > 0) {
    const lastRow = rows[rows.length - 1];
    const recipient = {};
    header.forEach((title, column) => {
      recipient[title] = lastRow[column];
    });
    data.push(recipient);
  }
  return data;
};

const main = () => {
  const data = getSheetData_();
  data.forEach((recipient) => {
    const status = sendMessage_({
      recipient_number: recipient["Phone Number"].replace(/[^\d]/g, ""),
      customer_name: recipient["Customer Name"]
    });
  });
};

关键修改说明

  • 新增if (rows.length > 0)判断,避免表格仅含表头时触发错误
  • 直接提取rows数组的最后一个元素rows[rows.length - 1]作为目标行
  • 仅将最后一行数据构造成recipient对象并加入返回数组

修改完成后,当表格变更触发器触发时,脚本只会读取最新添加的最后一行数据,并发送对应的WhatsApp消息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 11:24:28