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!
Related
I wrote a script for a Google Sheet that emails a user some specific info relevant to them alone. It works fine with the onOpen trigger, but I don't want them to have to open the sheet each time.
It would be easier to submit a form request and then receive the email but I'm not sure how to do that. I'm sure this is very basic so apologies for that, I'm new to this.
Basically I would like to form to ask if they want to receive an email with the info, and if they say yes, it triggers the script.
There are two ways to do this. The first is to:
Link the form responses to the spreadsheet
Add the installable trigger on form submit to the spreadsheet on the function you want to run.
Get the necessary information from the event object to see who submitted the form.
The second way is to:
Place a script on form.
Put an onFormSubmit() trigger on the form
Have the form open the spreadsheet - SpreadsheetApp.openById()
Copy paste the spreadsheet code into the form, making adjustments, so it will run (will mostly be how to get the user data)
If you would like this to only occur if the respondent asks, you can place a yes/no question on the form and check the answer. You can also have a questionless form that only collects emails, if you just want them to submit it to trigger an email.
there might be useful to do a template with a form with two buttons, 'Yes' or 'No'. If they hit 'Yes' you can hit an API in your platform to run script.
Maybe you can use a redis or some database to save the preferences of this user.
Hope it helps.
my onEdit() function calling to other function when there is a change in the sheet.
if the user makes the change all works fine.
but if the change was made by google-form (the form fill some cells with the answers it gets) onEdit() does not trigger.
do I miss something?
Yes, you forgot to read the documentation for the simple triggers:
onOpen(e) runs when a user opens a spreadsheet, document, or form that he or she has permission to edit.
onEdit(e) runs when a user changes a value in a spreadsheet.
onInstall(e) runs when a user installs an add-on.
doGet(e) runs when a user visits a web app or a program sends an HTTP GET request to a web app.
doPost(e) runs when a program sends an HTTP POST request to a web app.
and installed triggers:
Even though installable triggers offer more flexibility than simple triggers, they are still subject to several restrictions:
They do not run if a file is opened in read-only (view or comment) mode.
Script executions and API requests do not cause triggers to run. For example, calling FormResponse.submit() to submit a new form response does not cause the form's submit trigger to run.
Installable triggers always run under the account of the person who created them. For example, if you create an installable open trigger, it will run when your colleague opens the document (if your colleague has edit access), but it will run as your account. This means that if you create a trigger to send an email when a document is opened, the email will always be sent from your account, not necessarily the account that opened the document. However, you could create an installable trigger for each account, which would result in one email sent from each account.
A given account cannot see triggers installed from a second account, even though the first account can still activate those triggers.
Consider what would happen if your onEdit(e) was activated by programmatic changes, such as if your onEdit function alters the spreadsheet values...
In your situation, where you want form submission to activate your on edit function, You will need to install a form submission trigger (there is no simple trigger for form submissions).
An example function to receive your form submission trigger:
function giveMeAnInstalledFormSubmitTrigger(formSubmitEventObject) {
if(!formSubmitEventObject) throw new Error("You called this from the Script Editor");
var newEventObject = /* do something with the formSubmitEventObject */;
// Call the on edit function explicitly
onEdit(newEventObject);
}
You can read more about the event objects that triggered functions receive in the Apps Script documentation: https://developers.google.com/apps-script/guides/triggers/events
onEdit() will not be triggered by another script editing a sheet or by a form submission.
You can use onFormSubmit(e) instead depending on what your function does it would be a good idea to use the e parameter of the trigger.
https://developers.google.com/apps-script/guides/triggers/events#form-submit
What i have done is that i created an extra sheet with a cell that is the same where the primarecell is(the cell that changes). Leave it for now. I open the scriptapp and write down the code(the code that should happen if the cell changes). Then i wrte down "if" and take the two value i both cells and make an trigger that goes on every minute. With that said, if The cells are the same the "if" is looping on "true". But if something change on the primary cell, it will be "false" and you use "else". Under"else" you should have what ever function you want when soemthing change in that cell. + You must have a funtion where you insert the new value form the primarycell to the "check cell" if u want this too function as loop. Otherwise the "if" code will loop "false" every minute becuse the both cells are not the same.
My english and explaining is not that good:(
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.
The "fix" (test_onformsubmit) code you gave, I have to manually run it every time there is new data in the spreadsheet. I was wanting it to automatically send the pdf to email when Form is submitted. Is there a way? Because the manual way runs the code exactly like its supposed to, but I want this as an automatic event so I don't have to do anything.
See parent thread of original problem/question
Read Understanding Triggers. This function is an Installable Trigger, so you need to set it up to run when a form is submitted. It's easy - I would have thought a Forms tutorial would have walked through it.
In the Script Editor:
Choose Edit > Current project's triggers. You see a panel with the message No triggers set up. Click here to add one now.
Click the link.
Under Run, select the function you want executed by the trigger. (That's onFormSubmit(), in this case.)
Under Events, select From Spreadsheet.
From the next drop-down list, select On form submit.
Click Save.
From this point on, the function will be triggered whenever a form is submitted to the spreadsheet.
If you plan to share your script, each recipient will need to repeat these steps.
As an aside, you should change the email setting in your script, so it will work for ANYONE.
var email_address = Session.getActiveUser().getEmail();
My UI works for me (I'm the spreadsheet owner and script creator). However, when I share the spreadsheet with other users, the UI does not display fully and they are prompted to open or save the userAppPanel from docs.google.com.
I've complete cut down my script to the following, still not luck:
function showDialog() {
// Create the UI application
var uiApp = UiApp.createApplication();
// Provide the Popup with a title
uiApp.setTitle('Parameter Collection Dialog');
// Create an user instruction label
var lblInstructions = uiApp.createLabel()
.setText('Select a type of Person and Country');
// Add the panel to the application
uiApp.add(lblInstructions);
// Show the application in the spreadsheet
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
spreadSheet.show(uiApp);
}
The UI works when the the script is run from the script editor for every user that tries - however, when I hook it up to an "on open spreadsheet" trigger, the script only works for me - and no-one else.
I'm so close to nailing my requirement and this is a very frustrating last hurdle.
Many thanks
Update: Please see my comment for a solution i.e. use onOpen function rather than a manual onOpen trigger.
I've attempted to recreate the problem and was able to do so easily. This appears to be a bug. I suggest you open an issue in the Issue Tracker and update this thread with the bug number.
Thanks for confirming Srik. However, I have worked out a solution:
I was hooking up the showDialog function to the On Open trigger, manually through the script editor. but, if you change the showDialog() function name to onOpen(), then you don't have to hook up the function to the event manually as onOpen is a built in event, and it now works for all users (providing they are shared users, and logged in).
I will open an issue too, as I believe it should work both ways.
Hope my solution helps others too :)
Unfortunately the workaround with renaming the function onOpen is not always applicable, for instance when you need the function to run under the identity of someone (yourself for example). If you rename it onOpen(), it will run under no identity. I cannot do that because I need to authenticate into a database.
This seems to confirm that the problem is linked to the identity of the person triggering the script.
have you posted an issue ? Could you please share the link to it ?