[英]get last name and phone number from appScript and google sheet
I have these two functions that take data from a google sheet and then send a whatsapp message.我有这两个函数,它们从谷歌表格中获取数据,然后发送一条 whatsapp 消息。 I can't modify the first function (getSheetData_) to take only the data from the last row.我无法修改第一个 function (getSheetData_) 以仅获取最后一行的数据。 I only have two columns (name and phone).我只有两列(姓名和电话)。 I have a trigger that launches the script on every change.我有一个触发器,可以在每次更改时启动脚本。
source: https://www.labnol.org/whatsapp-api-google-sheets-220520来源: https://www.labnol.org/whatsapp-api-google-sheets-220520
const getSheetData_ = () => {
const [header, ...rows] = SpreadsheetApp.getActiveSheet().getDataRange().getDisplayValues();
const data = [];
rows.forEach((row) => {
const recipient = { };
header.forEach((title, column) => {
recipient[title] = row[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"]
});
});
};
Solution:解决方案:
const getSheetData_ = () => { var sheet = SpreadsheetApp.getActiveSheet(); var lastRow = sheet.getRange(sheet.getLastRow(),1,1,2); const header = ['Customer Name', 'Phone Number']; const rows = lastRow.getDisplayValues(); const data = []; rows.forEach((row) => { const recipient = { }; header.forEach((title, column) => { recipient[title] = row[column]; }); data.push(recipient); }); return data; };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.