0

This is my first post. I have learned almost everything I know about Apps Script from these forums, so thank you in advance.

I am trying to create a prefilled Google Form based on previous submissions. Data is passed to the Form as a string.


function filledFormGenerate(str){

  var dataarr = str.split('♠');

  var form = FormApp.openById("FORMID");
  var formResponse = form.createResponse();
  var items = form.getItems()
  
  var choices = items[0].asMultipleChoiceItem().getChoices()
  var formitem = items[0].asMultipleChoiceItem()
  var response = formitem.createResponse(choices[0].getValue());
  formResponse.withItemResponse(response);
  
  response = items[7].asTextItem().createResponse(dataarr[3].toString())
  formResponse.withItemResponse(response);
  
    
  var url = formResponse.toPrefilledUrl();
  var html = '<html><body><a href="'+url+'" target="blank" onclick="google.script.host.close()">'+'Follow this link to submit your next form.'+'</a></body></html>';
  var relinkui = HtmlService.createHtmlOutput(html)
  var ui = SpreadsheetApp.getUi();
    ui.showModelessDialog(relinkui, "Relinking Form")
}

With my own account, this is fully successful, but other users get the following Error on trying to access the form URL:

Exception: No item with the given ID could be found. Possibly because you have not edited this item or you do not have permission to access it.

Short of making the form editable by all users, is there a set of permissions to make this happen?

Thanks

1 Answers1

0

You need to implement your code in such a way that it runs on your behalf

If it is bound to a trigger (e.g. onOpen or onEdit) - it needs to be an installable instead of a simple trigger.

Explanation:

  • Simple triggers runs on behalf of the user to performs an action that fires this trigger.
  • If the user does not have edit access to the form called by FORMID he will get an authorization error
  • On the contrary, if you use installable triggers, those triggers will run on your behalf, even if another user performs the action that fires them
  • Since you have access to to your form, execution the the script content on your behalf will be successful.

How to set up an installable trigger?

  • Rename your current trigger function from a key word like onOpen or onEdit to something different
  • Go on Edit>Current project's triggers> "+" and bind a trigger to your renamed function, see here for more details
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • Thank you, this explains a lot. Unfortunately, I need this to be user-driven, so none of the triggers cover the type of deployment I'm looking for. I think I will be exploring the webapp route. – deadneck Dec 16 '20 at 20:01
  • By "user-driven", do you mean that you need to know the name of the user who fired the trigger? This is possible even if the trigger is authorized as "you" - see [here](https://stackoverflow.com/a/61653281/11599789) – ziganotschka Dec 16 '20 at 20:40
  • No, I need the Form to be launched by the user. Triggering on edit isn't viable, because this will not be a common task. I have completed the project now using a web app. Thank you for your help. – deadneck Dec 16 '20 at 23:00
  • I see, sorry that I misunderstood your issue! – ziganotschka Dec 17 '20 at 07:00