Google Sheets built-in "On form submit" trigger

Feels like I am on the one-yard line but just do not know enough yet about GAS syntax, etc.

Here is the call as you instructed:

   var to = '14086567070';
   var body = 'sendSMS test';
   sendSms(to,body);

And here is the sendSms function in a separate script:

```function sendSms(to, body) {
  var messages_url = "https://api.twilio.com/2010-04-........";

  var payload = {
    "To": to,
    "Body" : body,
    "From" : "14104313008"
  };

  var options = {
    "method" : "post",
    "payload" : payload
  };

  options.headers = { 
    "Authorization" : "Basic " + Utilities.base64Encode("AC291d5ff9701......"
  };

  UrlFetchApp.fetch(messages_url, options);
}

Twilio keeps kicking it back. The "to" and "body" parameters do not seem to be passing.  Appreciate the help and once this hurdle is crossed, I will go through the full GAS tutorials and training.  Thank you.

I’ve not used Twilio before, but a quick glance at their API Docs suggests that all parameters should be lower-cased. So what happens if you use:

var payload = {
    "to": to,
    "body" : body,
    "from" : "14104313008"
  };

?

I was able to get the parameters to pass between the scripts. Everything works. Agree on Rock Star status for Darren. Thank you.

3 Likes

@gp9293 Can you please share code … mine is not working with if(e.changeType == ‘INSERT_ROW’), BUT without it its working fine …
My whole code :
function onChange(e){
if(e.changeType == ‘INSERT_ROW’){
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“Sheet1”);
sh.getRange(“B10”).setValue(100)
}
else {
return;
}
}

var sheet = SpreadsheetApp.getActive();
ScriptApp.newTrigger(“onChange”)
.forSpreadsheet(sheet)
.onChange()
.create();

Glide can’t directly call a function with a parameter. You need to set up an onChange trigger in the google sheet that calls the function. That is the only trigger that will work other than a time based trigger. Unfortunately any change in the sheet or via glide will execute the onChange trigger, so you would have to change your logic to determine if a row is new or not. Maybe set a flag in a column if the new row has been processed by the script and then skip any rows that have the value set. The function can be named anything you want, but cannot have any parameters like (e).

What are you trying to achieve with your script? Is is something that could be done in Glide instead? Looks like you are just setting a single value in a single cell?

1 Like

As @Jeff_Hager says, make sure you are running the trigger.

1 Like

umm, that’s not true.
There is an event object that can be accessed when an onChange event has fired.
You can use it to discover (for example) the changeType and the source (the latter isn’t documented, but it is available - I use it regularly to discover which sheet and/or range triggered the event)

In this case…

That’s only ever going to be true if the change resulted in a new row being added to the bottom of the sheet.

1 Like

Really? I thought the triggers didn’t pass an object. I thought that an object was only passed if you are working directly within the sheet. Might have to play around with that again.

Simple test…

Add the following as an onChange trigger:

function on_sheet_change(event) {
  var sheetname = event.source.getActiveSheet().getName();
  var change = event.changeType;
  console.log([sheetname, change]);
}

Then do something in the app that causes a new row to be added, and check the logs. This is what I see:

Jan 25, 2021, 8:36:54 PM Debug [ 'Sheet3', 'EDIT' ]

@Jeff_Hager @Darren_Murphy The above code I sent is placed in Script Editor in Google sheet itself and not sent through glide. Yes I am placing a value in a cell through script. But I want this value on a single cell and not after that …
I tried onEdit as well that executed on each edit in a sheet but I want only when New Row is added via Glide app … I tried set column Action, but it is not working and somebody already reported a bug for that (Add row & Set column) Add row places a row in Glide table, but did not add row in Google sheet…

@Nitesh_Aggarwal this :arrow_up: is most likely the reason why your script is not executing. (Or more correctly, it probably is executing, but it isn’t getting past that conditional)

Yes … row is adding at bottom of sheet and all the rows below it are Empty (not existed)

@Darren_Murphy @Jeff_Hager My simple use case is … I have Blank sheet (with no rows) and 2 columns ( A & B) … I just want to set value of A via form (user input) and add Default value of 100 to column B (without asking to user) … and I need this only for the first row
And yes obviously when form is submitted I need only 1 row to be created in Google sheet as well as in Glide Sheet …

A B
User Input No user input value & Default Value = 100
User Input No userinput value & No default Value
User Input No userinput value & No default Value
User Input No userinput value & No default Value

Just so we are clear, please consider the following:

Screen Shot 2021-01-25 at 10.13.21 PM

  • If Glide adds a row at rows 4, 5, 6, 7, 8 or 9, the changeType will be EDIT
  • If Glide creates a new row (10), the changeType will be INSERT ROW

Um, why do you need a script for this?
Why not use a Set Columns action when the user submits the form?
If you only need it for the first row, you can create a rollup that counts the number of rows, and only do the Set Columns action if that count = 1.

Set column is not working :frowning:

copy demo app and see yoursef … or try it on your own sheet … Thanks !

Okay, I see.

Alright, getting back to your script, here is a version that will work:

function on_change(event) {
  var sheet = event.source.getActiveSheet();
  var sheetname = sheet.getName();
  if (sheetname == 'Sheet1') {
    sheet.getRange(2,2).setValue(100);
  }
}

Note that will reset that cell to 100 every time there is a change to that sheet. I’m not sure if that’s exactly what you want.

Thanks ! Though I want it to be executed only for the first time only … but it still works !
Thanks a lot.

Okay, so use this:

function on_change(event) {
  var sheet = event.source.getActiveSheet();
  var sheetname = sheet.getName();
  if (sheetname == 'Sheet1') {
    var check = sheet.getRange(2, 2).getValue();
    if (!check) {
      sheet.getRange(2, 2).setValue(100);
    }
  }
}

That will only set the value in B2 if it is empty.

1 Like

Thanks A lot … you are Genius !!! :slight_smile: :v: :100: