Google Script Error: You do not have permission to call openById - google-apps-script

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('xyz#gmail.com','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?
create a google form.
write above scripts on script editor and add a trigger with submission of the form.
fill and submit the form then you will get above error into mail from 'apps-scripts-notifications#google.com'
Please help.

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.

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.

Related

Unable to create an Installble trigger on a spreadsheet

I've created a WebApp which takes a spreadsheet id, a trello's list id, trello api key and a user's trello token as input.
Upon getting the input, the Google app script creates an installable trigger on the spreadsheet such that the trigger will get executed whenever someone submits the google form linked with the spreadsheet and creates a trello card in the specified trello list.
Initially I tested this script by creating a google form (and hence a google sheet) and by giving the script the required input mentioned above. This worked completely fine, the script was successfully able to create a trigger on the specified spreadsheet.
The problem was that the google form and the google app script project were both linked to my Google account and hence there was no trouble for me to access my own resources programmatically. As soon as I tried to create a trigger on a sheet that was of some other user I was not allowed to do so.
I had published this script as webapp with execute app permission as Me and anonymous access to the app.
I used Postman to make the api calls to the script and when submitting the input required to create the trigger I'm getting an HTML that says this:
I also tried to change the execution permissions to this:
But then got this response:
I'm assuming that I'll need to get permission from the user to be able to set a trigger on his/her resource but I'm not sure how to achieve that any suggestions would be appreciated.
Just for the sake of completeness, here is the code to set up a trigger on a spreadsheet.
function getTrigger(data){
return ScriptApp.newTrigger("createTrelloCard").forSpreadsheet(data["spreadsheet_key"])
.onFormSubmit()
.create();
}

create Google Sheet database accessed by different users

I've built a spreadsheet as a form by google script with one button which updates a database(a different sheet) according to the form - btn_pressed().
I want to authorize any google account I give access to - to use the form (activate the google script code), and update the database and send mail (part of the btn_pressed() process).
should I publish the script as a web app? or there's another way to do what I'm looking for
I understand that you want to automatically authorise the script for other users when they come to run the script?
I recently learnt you can only do this with certain triggers such as onOpen() out onEdit(), there is a way round this though. Please see the following link as i asked the question on here myself!
Remove authorisation required for script on Google sheets
I trust that helps

Is there any reason a Google Apps Script wouldn't run for specific people?

So here's my problem: I've made a Google Apps Script that I've put on an HTML page to make a form. The script then pushes the data to a Google Sheets document that is accessible to anyone, even anonymous. However, the only person the script won't run for is the script administrator. I've tried with many different computers, and mine was the only one which didn't run.
I've also cleared my cache, and checked to see if the source code showed the correct script destination, and sure enough, it did. Is there anything that I've been doing wrong?
Thanks for the help, guys.
If you are using a script to make a form, it may be better to just use a Google Form and then link it to a spreadsheet. Then if you want to add a script into the spreadsheet, you can go in tools and then scripts, put in script code, and put appropriate triggers for when you want the script to run. Google Form submissions can be anonymous as well and you'll be able to set the privacy of that spreadsheet to anyone with link. You can choose in the form settings to show the option to view submissions after submitting form.

Google Apps Script keeps running after trigger removed (multiple users)

I have a fairly simple Google Apps Script that runs every time an answer to a Google Form is submitted (by using "on form submit" trigger). The script does some simple manipulations and updates a Google SpreadSheet. Recently I needed to change that script to run manually and not on form submit, so I deleted the trigger. However the script still kept running on every form submit!
There are other users who might have the same trigger setup, since the spreadsheet and the code are shared. Is there a way to completely delete all the triggers for a particular script? In the meantime I just renamed the script and that has solved the issue, however this is a bad solution.
Search on stackoverflow did nothing, seemingly similar questions are resolved through clearing "Resources -> All your triggers...", which only does it for your user. Question is how to remove/edit all the triggers for the project, even from other users.
Here's how to stop any orphaned Google Script.
Go to https://security.google.com/settings/security/permissions
Click the script / project name you wish to uninstall
Next click Remove and you are done.

Google Apps Script triggers E-mail Notification

First off, I'd like to apologize if this has already been answered. I certainly hope not.
Anyways, here's the idea of what I'm doing.
Using Google Drive, a user is supposed to be able to fill out a form and submit it.
On the submission of the form, a script is triggered.
The script is supposed to send an e-mail to an account within our domain in order to notify them that a new form has been submitted.
Here's the script I'm using to do that:
function emailnotifacs1() {
var recipient = "user#domain.net";
var subject = "New Form Is In!";
var body = "Check the spreadsheet." +
" Link: spreadsheetLink.com";
MailApp.sendEmail(recipient, subject, body);
}
For some reason, each time I attempt to debug the script, it asks that I re-authorize the script, Literally every time.
And each time the script is automatically ran (on Submit), it doesn't run at all and gives me this e-mail:
You do not have permission to call openById
It's triggering on formEdit.
Any help would be greatly appreciated and returned with free internet cookies.
Thanks,
Alex Lee
You might try to trigger emailnotifacs1() with "On form submit" instead
and maybe share more code if that doesn't fix the problem
According to this bug report, to fix it, you have to pre-authorize the function that will be run. To do that, you can just go into your script editor and run the function.
However, if your function is meant to be run on a trigger, you won't be able to run your function straight from the script editor (e.g. my function is only supposed to be run onSubmit and it does things to the form that's been submitted, so if I run it from the script editor I'll get errors because the form doesn't exist).
So I wrapped the code in my triggered function in a try-catch loop, ran it from the script editor menu, and voila, my code started working.
Also, in your case, you may want to check that the trigger is properly set up to call emailnotifacs1 on form submit. And emailnotifacs1 should be accepting an event parameter - see here for more info.
NOTE: Every time you make changes to the function, you'll need to re-authorize it by running it from the script editor!