How to send SMS based on cell contents in Google Sheets

淺唱寂寞╮ 提交于 2019-12-11 06:23:26

问题


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] + "! 😊\n\nThanks...;
    var leadText = "Hey " + data[9] + "! 👋\n\nThanks so...;
    var followText = "Hey " + data[9] + "! 😊 \n\nAre...;
    var confirmText = "Hey " + data[9] + "! ⏰ \n\nYour appointment...;

    if (e.value == "Appt. Set (send text)") {
      object.subject = "Appt. Confirmation";
      object.body = apptText; //
      SpreadsheetApp.getActiveSheet().getRange(range.getRow(),2,1,1).setValue('Appt. Set Text sent on ' + date);
    } else if (e.value == "Lead (send 1st text)") {
      object.subject = "Lead";
      object.body = leadText; //
      SpreadsheetApp.getActiveSheet().getRange(range.getRow(),2,1,1).setValue('Lead 1st Text sent on ' + date);
    } else if (e.value == "3rd Text") {
      object.subject = "Follow Up";
      object.body = followText; 
      SpreadsheetApp.getActiveSheet().getRange(range.getRow(),2,1,1).setValue('3rd Text sent on ' + date);
    }else if (e.value == "Day of Confirm (send text)") {
      object.subject = "Can't wait to meet you!";
      object.body = confirmText;  
      SpreadsheetApp.getActiveSheet().getRange(range.getRow(),2,1,1).setValue('Confirmation Text sent on ' + date);
    }
    if (object.subject) sendSms(data[5], twilioNumber);
  }
}

来源:https://stackoverflow.com/questions/56669749/how-to-send-sms-based-on-cell-contents-in-google-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!