谷歌应用程序脚本,从电子表格数据发送短信

时间:2014-08-15 22:15:39

标签: javascript google-apps-script

我想知道是否有人已经编写了一个脚本,只需从一个单元格中取一个电话号码并发送短信。就像它是一个电子邮件地址一样。使用运营商电子邮件扩展程序不是我正在寻找的解决方案,但如果有人已经想出如何以编程方式找出运营商扩展,将其添加到电话号码,那么这也将起作用。

示例我用于发送电子邮件

     function inventory() {
  var sheet = SpreadsheetApp.getActiveSheet()
  var row = sheet.getActiveRange().getRowIndex()
  var firstName = sheet.getRange(row, getColIndexByName("First Name")).getValue();
  var lastName = sheet.getRange(row, getColIndexByName("Last Name")).getValue();
  //var phone = sheet.getRange(row, getColIndexByName("Phone Number")).getValue();
  var email = sheet.getRange(row, getColIndexByName("Email Address")).getValue();
  var inventoryLinks = sheet.getRange(row, getColIndexByName("Inventory Links")).getValue();
  //var userEmail = 'put email here';
  var subject = "Your Inventory List";                 
  var body = "Hello"
             + "\n\n" + "Please follow the supplied links below to see the listings that fit your profile."
             + "\n\n" + sheet.getRange(row, getColIndexByName("Inventory Links")).getValue();

  MailApp.sendEmail(email, subject, body, {name:"Inventory List"});
}

function getColIndexByName(columnName) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var numColumns = sheet.getLastColumn();
  var row = sheet.getRange(1, 1, 1, numColumns).getValues();
  for (i in row[0]) {
    var name = row[0][i];
    if (name == columnName) {
      return parseInt(i) + 1;
    }
  }
  return -1;
}

所以我想用电话号码替换电子邮件地址做同样的事情

这就是我最终的结果。效果很好!

function sendSMS() {
// Get account SID and auth token here:
//   https://www.twilio.com/user/account
//  Also, you have to buy a paid account to send SMS to any phone number.  Otherwise you can only  send to verified numbers
//  So this script allows me to put my cursor in the row I want to send an SMS.  

var sheet = SpreadsheetApp.getActiveSheet();
var row = sheet.getActiveRange().getRowIndex();
var phone = sheet.getRange(row, getColIndexByName("Phone")).getValue();
var inventoryLinks = sheet.getRange(row, getColIndexByName("Inventory Links")).getValue();
var sentSMS = sheet.getRange(row, getColIndexByName("Sent SMS")).getValue();
var comments = sheet.getRange(row, getColIndexByName("Comments")).getValue();
var activeUser = Session.getActiveUser().getEmail();
var extphone = "+1" + phone;
var body = "Click on the link/s to see current inventory.  " + inventoryLinks;
var accountSid = "get it from your Twilio account";
var authToken = "get it from your Twilio account";
var url = "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/SMS/Messages.json";
var options = {
method: "post",
headers: {
  Authorization: "Basic " + Utilities.base64Encode(accountSid + ":" + authToken)
},
payload: {
  // From is one of your Twilio phone numbers
  From: "Put you Twilio number here",
  To: extphone,
Body: body
}
};

sheet.getRange(row, getColIndexByName("Sent SMS")).setValue('Sent SMS').setComment(new Date() +        activeUser);
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
}

0 个答案:

没有答案