3
votes

I am trying to send a mail with attaching a file available in my google drive using following google scripts

var file=DriveApp.getFileById('1qZVK0UZ1jLbDdj10FXZqeAVEodvxEy2Bs');
MailApp.sendEmail('[email protected]','subject','body',{attachments[file.getAs(MimeType.PDF)]});

when I run above script manually it runs fine, email gets sent. But when I run it using form Submission trigger it gives error "You do not have permission to call openById"

steps that will reproduce the problem?

  1. create a google form.
  2. write above scripts on script editor and add a trigger with submission of the form.
  3. fill and submit the form then you will get above error into mail from '[email protected]'

Please help.

2

2 Answers

2
votes

I found this when creating the script on the Form itself.

To solve the problem first have you form responses sent to a spreadsheet.

From you form go to Tools > Script editor... copy you script and delete any triggers you have set up there, you won't be needing this script anymore.

Now click on View responses button in your form to open the spreadsheet.

  • Go to Tools > Script editor... (for the spreadsheet).
  • You should start with a blank project.
  • Delete anything in there and paste in your script.
  • Save and name you project.

Now set up your triggers as normal. You will find the third drop down list of the new trigger has the option On form submit.

Once your script is authorised any time the form is submitted the event will trigger and will have access to the DriveApp.

Once you've tested and got it working I would go back to the form and delete the script project in there. I do that to keep things tidy and save confusion if I come to alter the script later and have forgotten which one is actually live.

I don't know if there is a limitation with Google Forms that limits their access to Drive or something.

2
votes

I ran into this same issue, but felt it was a little silly to have to use the FormResponses Sheet to get the submission event (I mean there's a trigger on the Form Apps Scripts for a reason, right?).

All I had to do was to Remove and Re-add the trigger for From form --> On form submit. When I added it, challenged me for OAuth permissions to view and use my google drive.

Immediately after this, I was able to run on a Forms Apps Script Submission trigger again.