0
votes

Is there a way to trigger a spreadsheet’s onFormSubmit in script? That is, instead of using a google form to trigger onFormSubmit? I attempted the below sendHttpPost code posting to a spreadsheet Web App:

function sendHttpPostSelf() {
var payload = {
      "name" : "name",
      "comment" : "comment",
    };
  var options = {
      "method" : "post",
      "payload" : payload
    };
var url="https://script.google.com/macros/s/AKfycbzlVoiDQMbLe4yliErDoNub6A0m3tijSfPAUMEBENgIikQnLQ_H/exec";
var resp=UrlFetchApp.fetch(url,options);
Logger.log(resp.getContentText());
} 

The code correctly triggers doPost() in the spreadsheet Web App. But, the code does not trigger the spreadsheet’s onFormSubmit. Alternately, is there any way to script an onInsert trigger when inserting into a spreadsheet?

3

3 Answers

0
votes

onFormSubmit trigger will only fire when a Google Form submits. It's pretty tricky/unsupported to get an Apps Script code to trigger that.

If you just want the ability to detect new rows, you can use the onEdit trigger to and check the range to see what got inserted.

https://developers.google.com/apps-script/understanding_events

0
votes

A list of things-that-don't-trigger-onEdit, along with their issue tracking ids, were provided in a previous answer. There is example code there that demonstrates detection of a new row... but the caveat is that you need to do some other type of edit to trigger it!

So, bad news - despite Arun's answer, neither inserting rows nor a script writing values will trigger onEdit.

0
votes

please tell me if I'm wrong but of what I understood, you want to submit values not passing through a google form, but with a form you builded yourself and you want to submit with a doPost action.
If so, here is what you can do:
1/ insert the data from the doPost in your spreadsheet with a appenrow()
2/ call the function that is usually called with onFormSubmit inside the doPost() (you need to transfert to the onFormSubmit function the good arguments (as if it was called with a real formSubmit)

here is a sample function:

function onformSubmit(e){ 
  var answers = e.values;
  if (typeof(answers) == "undefined") answers = e;
  MailApp.sendEmail("youremailadress", "form first answer", answers[1]);

}
function doPost(e){
  MailApp.sendEmail("youremailadress","reponse doPost",e.parameter.name);
  SpreadsheetApp.openById("spreadsheet id").getSheetByName("spreadsheet name").appendRow([new Date(),e.parameter.name,e.parameter.comment]);
  var toe = [new Date(),e.parameter.name,e.parameter.comment];
  onformSubmit(toe);
  return(ContentService.createTextOutput().setContent("okey"));
}
function submitIt(){
  var payload = {
      "name" : "name",
      "comment" : "comment",
    };
  var options = {
      "method" : "post",
      "payload" : payload
    };
var url="https://script.google.com/macros/s/yourscripturl/exec";
var resp=UrlFetchApp.fetch(url,options);
}

When you launch submitIt() it will call the doPost that will write the row in your spreadsheet just like when the form is submited and then it will call the function onformsubmit (that is normally called when the form is submited)