0
votes

I'm trying to have my standalone script a common trigger(onFormSubmit trigger) for multiple forms and determine the which form triggered the script.

function myFunction() {
  ScriptApp.newTrigger('onFormSubmit').forForm('FormID_1').onFormSubmit().create();
  ScriptApp.newTrigger('onFormSubmit').forForm('FormID_2').onFormSubmit().create();
  ScriptApp.newTrigger('onFormSubmit').forForm('FormID_3').onFormSubmit().create();
}

function onFormSubmit(e){
  Logger.log(e);
}

However the parameter e only has {authMode=FULL, response=FormResponse, triggerUid=......}, no FormId.

How can I know which form triggered the script?

1

1 Answers

1
votes

Please think of this as one of several answers. From your question, it is found that you can retrieve triggerUid from the parameter e. So how about saving triggerUid using PropertiesService? triggerUid is ID for each trigger. By using this ID, the form which is submitted can be retrieved.

When you use this sample script, at first, please run myFunction(), and submit using form. You can see the result at Stackdriver (View -> Stackdriver Logging). The form ID of form which was used can be retrieved.

The sample script is as follows.

Sample script :

function myFunction() {
  var FormID_1 = 'FormID_1';
  var FormID_2 = 'FormID_2';
  var FormID_3 = 'FormID_3';
  var form1 = ScriptApp.newTrigger('onFormSubmit').forForm(FormID_1).onFormSubmit().create();
  var form2 = ScriptApp.newTrigger('onFormSubmit').forForm(FormID_2).onFormSubmit().create();
  var form3 = ScriptApp.newTrigger('onFormSubmit').forForm(FormID_3).onFormSubmit().create();
  var forms = {};
  forms[FormID_1] = form1.getUniqueId();
  forms[FormID_2] = form2.getUniqueId();
  forms[FormID_3] = form3.getUniqueId();
  PropertiesService.getScriptProperties().setProperties(forms);
}

function onFormSubmit(e) {
  var forms = PropertiesService.getScriptProperties().getProperties();
  for (v in forms) {
    if (forms[v] == e.triggerUid) {
      console.log(v) // v is the form name which was submitted. You can see this at Stackdriver.
    }
  }
}

Reference :

If I misunderstand your question, I'm sorry.

Edit :

How about this? In this case, each form has each function. And each function calls onFormSubmit(e).

function myFunction() {
  ScriptApp.newTrigger('form1').forForm('FormID_1').onFormSubmit().create();
  ScriptApp.newTrigger('form2').forForm('FormID_2').onFormSubmit().create();
  ScriptApp.newTrigger('form3').forForm('FormID_3').onFormSubmit().create();
}

function form1(e){
  onFormSubmit(e)
}

function form2(e){
  onFormSubmit(e)
}

function form3(e){
  onFormSubmit(e)
}

function onFormSubmit(e){
  Logger.log(e);
}