2

I'm developing an add on for Google Sheets. While doing so I encountered following exception

Exception: The add-on attempted an action that is not permitted in Test as add-on mode. To use this action, you must deploy the add-on.

Here is the gist of code for which I'm getting an exception

function initiateCall()
{
  try
  {
    var sp = SpreadsheetApp.getActiveSpreadsheet();
    var sheet_obj = sp.getSheetByName(SHEET_NAME);
    var last_row = sheet_obj.getLastRow();
    var result = "failure";

    if(last_row >= 2)
    {
      Logger.log("Creating");
      var trigger_id = ScriptApp.newTrigger('triggerToInitiateCall').timeBased().after(100).create();
      Logger.log("Created");
      setProperty("trigger_id", trigger_id.getUniqueId());

      result = "success";
    }//if
    else
      result = "validation_failure";

    return result;
  }//try
  catch(e)
  {
    Logger.log("Exception=> "+e+" At Line Number "+e.lineNumber)
  }//catch
}//initiateCall

Note: - Script is running in AuthMode.NONE mode - Script is standalone script.

The reason behind using triggers is to make asynchornous UrlFetch.

So I have two questions to ask

  1. Is it possible to make Asynchronous UrlFetch from Google App Script?
  2. If not how to create time driven triggers in Google Sheet Add On (alternate way to make kind of an async UrlFecth)?

Please help. Any help is appreciated.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Parag Jadhav
  • 1,853
  • 2
  • 24
  • 41

1 Answers1

2

Google Apps Script is synchronous. Add-ons can use time based triggers, but they can only run once an hour.

You have a run time of 6 minutes in the script, if possible just wait for the URLFetch to return. It is synchronous, and it will wait for the return value of the call.

If you really need a asynchronous call, you can't wait for the URLFetch to return, use a trigger that runs once an hour, or create a sidebar and execute the API call in the context of the sidebar, there you have asynchronous calls.

Jasper Duizendstra
  • 2,587
  • 1
  • 21
  • 32