电子表格脚本中的错误

时间:2018-11-30 20:32:09

标签: javascript

我在Google中有一个简单的电子表格,可以用来简化向客户发送短信的过程。

我编写了一个脚本,该脚本与我的Twilio帐户进行接口以发送消息。

该脚本大约50%的时间正常工作。如果没有,我会收到一个错误消息,“即使有电话号码,也需要”。

有什么想法吗?

    
var CURRENT_SHEET = SpreadsheetApp.getActiveSheet();

function sendSMS(toNumber, name, dress, row, message) {

  var accountSID = "XXXXXXXXXX";
  var authToken = "XXXXXXXXXX";

  var url = "https://api.twilio.com/2010-04-01/Accounts/" + accountSID + "/Messages.json";
  var options = {
    method: "POST",
    headers: {
      Authorization: "Basic " + Utilities.base64Encode(accountSID + ":" + authToken)
    },
    payload: {
      "From" : "XXXXXXXXXX",
      "To"   : toNumber,
      "Body" : "Hi "+name+",\n"+message+"\n"+ dress[0][0]+"\n"+dress[0][1]+"\n"+dress[0][2]
    },
    muteHttpExceptions: true
  };

  var response = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());

  if (response.hasOwnProperty("sid")) {
    Logger.log("Message sent successfully.");
     var date = dateTime().time;
     var time = dateTime().date;
     CURRENT_SHEET.getRange(row, 9).setValue('Sent');
     CURRENT_SHEET.getRange(row, 10).setValue(date);
     CURRENT_SHEET.getRange(row, 11).setValue(time);
     sentMessage("Message Sent.");
  }

  Utilities.sleep(1000);

}


function validateData() {

  var currentRow = CURRENT_SHEET.getActiveCell().getRow();
  var status = CURRENT_SHEET.getRange(currentRow, 9).getValue();

  status = status.toLowerCase();

  if(status != 'sent') {
       var phoneNumber = CURRENT_SHEET.getRange(currentRow, 3).getValue();
       if(!phoneNumber) {
          sentMessage("Phone number required.");
          return;
       }
       var name = CURRENT_SHEET.getRange(currentRow, 1).getValue();
       var dresses = CURRENT_SHEET.getRange(currentRow, 6, 1, 3).getValues();
       var message = CURRENT_SHEET.getRange(currentRow, 5).getValue();
       sendSMS(phoneNumber, name, dresses, currentRow, message);
   }
  else
  {
    sentMessage("Message Already Sent.");
  }

}

function sentMessage(message) {
   SpreadsheetApp.getActiveSpreadsheet()
     .toast(message);
}


function dateTime() {
  var date = new Date(); 
  var userTimeZone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
  var timeOnly =  Utilities.formatDate(date, userTimeZone, "yyyy-MM-dd");
  var dateOnly  = Utilities.formatDate(date, userTimeZone, "HH:mm:ss z "); 
  return ({time:timeOnly, date:dateOnly}) 
}

0 个答案:

没有答案