0

So I have an installed trigger:

function CreateRebateExtTrig() {

var ExtFrm = FormApp.openById("ID");
  ScriptApp.newTrigger('ExtSubmit')
  .forForm(ExtFrm)
  .onFormSubmit()
  .create()
}

For a form on submit. I am trying to get the answer to a specific question filled in on the form to replace the incorrect version forms carried over (it removed leading 0's but form responses show it with 0's)

I have another form set up on the same google sheet but a different sheet using the same process. exactly the same, same variables 'range' and 'row'

function ExtSubmit(e) {

 var range = e.range;
 var ss = range.getSheet();
 var row = range.getRowIndex();
 var formResponse = e.response;
 var itemResponses = formResponse.getItemResponses();

   for (var i=0; i<itemResponses.length; i++) {
    switch (itemResponses[i].getItem().getTitle()) {
     case "dummy text for stack overflow":
      var IDCF2 = itemResponses[i].getResponse();
     console.log({message: IDCF2})
    break;
    }
  }

Before I added the 'row' and 'range' the e was working up to console.log IDCF2 returning the correct value with the leading 0's. As soon as I put the e.range and range.getSheet in, it broke :( Not sure why. If anyone can help me out I'd appreciate it. Suggestions/code/sources.

My current errors are:

enter image description here

enter image description here

Cheers,

Tea

Rubén
  • 34,714
  • 9
  • 70
  • 166
RichTea
  • 115
  • 9
  • The other form also uses e.(insert whatever here) if that would affect it? I'd think not though considering they're on separate script files? – RichTea Sep 18 '18 at 14:29
  • 2
    The event object for the *form submission* event has different properties, depending on if the trigger is for a Form, or for a Sheet https://developers.google.com/apps-script/guides/triggers/events#form-submit_4 – tehhowch Sep 18 '18 at 15:00
  • aaah so e.range etc doesn't exist coming from the trigger .ForForm? That sucks.. The only reason I did that was because I have two forms linked to one Google Sheet and no matter which form was submitted it was running both scripts. @tehhowch – RichTea Sep 18 '18 at 15:08
  • @tehhowch Do you know if there is another way to link a trigger to a specific form keeping .ForSpreadsheet rather than having to use .forForm? – RichTea Sep 18 '18 at 15:10
  • 1
    You can absolutely access a particular response destination from a form, or determine the form for a given sheet. You just need to use the right methods. Review https://stackoverflow.com/a/51484165/9337071 and https://stackoverflow.com/a/51483712/9337071 and documentation – tehhowch Sep 18 '18 at 16:18
  • @I'-'I innovative yet obvious.. should have thought to try something like that, I'll give it a try – RichTea Sep 19 '18 at 08:42
  • @tehhowch that first link has me eye, I think that code refers to inside the form, whereas I'm running mine from the sheet, but I'm going to give it a pop now. thanks for all these sources! – RichTea Sep 19 '18 at 08:48
  • Yeah don't worry I will, I can't code that fast haha, I was trying tehhowch's links out but I can't get the first one to work so I'll try yours. – RichTea Sep 19 '18 at 09:27

1 Answers1

0
function ExtSubmit(e) {

  if(e.range.getSheet().getName()==="Form Responses 3"){

    //rest of code below

I used I'-'I's answer in the comments to solve my issue, I just used the same code above on the other script file but with 'Form Responses 2' and it works flawlessly,

Thanking you sir!

RichTea
  • 115
  • 9