2

I wrote a script that gets a rows data from a spreadsheet and loops through them, calling a function to send an SMS if the rows' data meets certain conditions (having a phone number and not having already been sent for example).

However after adding about 600 rows, the script execution time exceeds it's limit, that seems to be 5 minutes according to my research. I'm using JavaScript objects to read data and a for loop to iterate through the rows.

Can anyone tel me if it is possible to make it faster? I'm very new to programming but this seems such a light task for all this computing power that I can't understand why it takes so long

Thanks in advance!

Here's the code of the function I'm using:

// Will send SMS on the currently active sheet
function sendSms() {

  // Use the send sms menu to trigger reconcile
  var user = ScriptProperties.getProperty(PROPERTY_USER_RECONCILE);
  if (user == null)
    reconcileUser();

  // The sheets
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Registo");
  var settingsSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Settings");

  // Fetch values for each row in the Range.
  var startRow = 2;
  var apiKey = settingsSheet.getRange("B2").getValue();
  var apiSecret = settingsSheet.getRange("B3").getValue();
  var prefix = settingsSheet.getRange("B4").getValue();
  var numRows = sheet.getMaxRows() - 1;
  var numCols = 16;
  var statusColNum = 15;  // IMPT: To keep track status in col 15
  var dataRange = sheet.getRange(startRow, 1, numRows, numCols);

  // Make sure there is API key and secret
  if (apiKey == "" || apiSecret == "") {
    Browser.msgBox("You MUST fill in your API key and secret in Settings sheet first!");
    return;
  }

  // Create one JavaScript object per row of data.
  var objects = getRowsData(sheet, dataRange);

  var totalSent = 0;

  for (var i = 0; i < objects.length; ++i) {
    // Get a row object
    var rowData = objects[i];
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var templateSheet = ss.getSheetByName("SMS Modelo");
    var template = templateSheet.getRange("A1").getValue();

    // jump loop iteration if conditions not satisied
    if (rowData.resolv == "x" || rowData.contactoUtente == null || rowData.contactoUtente == "" || rowData.reserv == null || rowData.reserv == "" || rowData.cont == "x" || rowData.sms !== null) continue;
      var message = fillInTemplateFromObject(template, rowData);
      var senderName = "Farm Cunha"
      var mobile = rowData.contactoUtente;
      // Send via Nexmo API
      var response = nexmoSendSms(apiKey, apiSecret,"+351" + mobile, message, senderName);
      if (response.getResponseCode() == 200) {
        var object = JSON.parse(response.getContentText());
        if (object.messages[0]['status'] == "0") {
          // Set to QUEUE status - We assumed SENT, as we don't handle delivery status.
          //sheet.getRange(startRow + i, statusColNum).setValue(STATUS_QUEUE);
          sheet.getRange(startRow + i, statusColNum).setValue(STATUS_SENT);
          // Set the reference id
          sheet.getRange(startRow + i, 19).setValue(object.messages[0]['message-id']);
          // sheet.getRange(startRow + i, statusColNum+3).setValue(new Date()); linha pode ser activada para fazer timestamp do envio
          totalSent++;
          }
        else {
          // If status is not 0, then it is an error.
          // Set status to the error text
          sheet.getRange(startRow + i, statusColNum).setValue(object.messages[0]['error-text']);
        }
      }
      else {
        // Non 200 OK response
        sheet.getRange(startRow + i, statusColNum).setValue("Error Response Code: " + response.getResponseCode);
      }
      SpreadsheetApp.flush();
      // Need a wait. Need to throttle else will have "Route Busy" error.
      Utilities.sleep(2000);
  }

  // Update total sent
  var lastTotalSent = parseInt(ScriptProperties.getProperty(PROPERTY_SMS_SENT_FOR_RECONCILE));
  if (isNaN(lastTotalSent)) lastTotalSent = 0;
  ScriptProperties.setProperty(PROPERTY_SMS_SENT_FOR_RECONCILE, (lastTotalSent + totalSent).toString());
  Logger.log("Last sent: " + lastTotalSent + "  now sent: " + totalSent);
  reconcileApp();
}
user2149992
  • 83
  • 1
  • 6

2 Answers2

5

You have a few things in your loop that are too time consuming : spreadsheet readings and API calls + 2 seconds sleep !.

I would obviously advise you to take these out of the loop (specially the template sheet reading that is always the same!). A possible solution would be to check the conditions from the row objects and to save the valid entries in an array... THEN iterate in this array to call the API.

If this is still too long then proceed by small batches, saving the end position of the partial iteration in scriptproperties and using a timer trigger that will continue the process every 5 minutes until it is completed (and kill the trigger at the end).

There are a few example of this kind of "mechanics" on this forum, one recent example I suggested is here (it's more like a draft but the idea is there)

Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Thanks very much Serge. I think saving the valid entries in an array is the option that will work better for me. I think the other options wouldn't work very well. That's because of the very specific way this spreadsheet is going to be used by non tech savvy people. Also I'm not sure how I would implement them as I'm new to programming :) – user2149992 Mar 11 '13 at 20:12
  • The 2 second sleep seems to be a requirement from the API... Not sure though. Will test. – user2149992 Mar 11 '13 at 20:17
4

Ok, I've solved it by taking these 3 lines out of the loop as Serge (thanks) had told me to:

 var ss = SpreadsheetApp.getActiveSpreadsheet();
    var templateSheet = ss.getSheetByName("SMS Modelo");
    var template = templateSheet.getRange("A1").getValue();

It's so simple that I don't know how I was not seeing that.

This simple change made the script much faster. For example, going through 600 rows would take more than 5 minutes. Now, more than 5000 rows only take seconds.

user2149992
  • 83
  • 1
  • 6