This is a follow up to my last question How to loop an onEdit function to send emails from multiple rows in Google Sheets? Now to finish this project, I need to be able to send SMS based on a cell's contents.
I'm using Twilio, and the code from their example https://www.twilio.com/blog/2016/02/send-sms-from-a-google-spreadsheet.html allows me to send texts to ALL numbers in the spreadsheet when I run the function. There is some help at this question Send SMS from Google Sheet however since I'm using Twilio instead of carrier emails I'm still getting stuck.
As of right now, this first block of code allows all texts to be send at the same time I run the function (sendSMS contains all API info and is not shown):
function sendAll() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('HIVE');
var startRow = 2;
var width = 16;
var numRows = sheet.getLastRow() - 1;
var dataRange = sheet.getRange(startRow, 2, numRows, width)
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
try {
response_data = sendSms(row[4], row[12]);
status = "sent";
} catch(err) {
Logger.log(err);
status = "error- Not Sent";
}
sheet.getRange(startRow + Number(i), 2).setValue(status);
}
}
I tried replicating the pattern used to send the emails in my last question by inserting:
if (sheet.getSheetName() == sheetname && range.columnStart == 1) {
var data = sheet.getRange(range.getRow(), 1, 1, 21).getValues()[0];
var object = {
to: data[5] // Column "E"
if (e.value == "Appt. Set (send text)") {
object.subject = "Appt. Confirmation";
object.body = apptText; // variable containing body text
if (object.subject) sendSms(object);
I've updated the code to where it will trigger onEdit, but only for the first IF statement:
function onEditText(e) {
var sheetname = "HIVE";
var sheet = e.range.getSheet();
var range = e.range;
var timezone = "GMT-5";
var timestamp_format = "MMMM dd 'at' HH:mm";
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
var twilioNumber = 1234567890;
if (sheet.getSheetName() == sheetname && range.columnStart == 1) {
var data = sheet.getRange(range.getRow(), 1, 1, 21).getValues()[0];
var object = {
to: data[5] // Column "E"
};
var apptText = "Hey " + data[9] + "!