Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why UrlFetchApp fails to be called in an onOpen trigger?

I am trying to make an HTTP POST request when Google Spreadsheet loads.

I have gotten my HTTP POST request working as a function in a menu dropdown but I can't get it to work with onOpen or onInstall.

Here is the code:

    function onOpen(){
      var url = "http://example.com/collect";
      var options = {
        "method": "post",
        "payload": {
          "v":"1",             
          "data1":"hello", 
          "data2":"5553",   
          "data3":"test"
        }
      }
      Logger.log('worked');
      UrlFetchApp.fetch(url, options);
    }

Is it possible to make an HTTP POST request when a spreadsheet loads? If so, do you see anything wrong with my code?

like image 973
NicoM Avatar asked Feb 15 '14 08:02

NicoM


1 Answers

If you read the documentation about permissions and simple triggers, you will learn that simple triggers can't do anything that requires authorization because they run under the authority of the "user at the keyboard" and do it silently.

Change the function name and create an installable onOpen trigger that will be executed as yourself.

like image 176
Serge insas Avatar answered Nov 15 '22 07:11

Serge insas