Google Spreadsheet trigger programmatically added is not - google-apps-script

I want to process values submitted from a Google Form when they get saved in the associated spreadsheet. I have an app script associated with the form (as it adds an addon menu there and is suppose to be used from FormApp), and this one install the trigger on destination spreadsheet as following.
ScriptApp.newTrigger('onFormSubmit').
forSpreadsheet(destId).onFormSubmit().create();
function onFormSubmit(e) { Logger.log("--> submit"); }
I also have added an email notification (Immediate) on the trigger.
The trigger appears in project triggers. Calling the onFormSubmit function from editor, or on other event, is working and debug statement is written in the log.
When the form is POSTed, I don't either see anything in the log, nor receive any mail notification.
What's wrong there?

Related

Fetch the data that was submitted in the google form (using OnFormSubmit trigger) with apps script

I created an apps script project for my google form, and added a trigger which has the following settigs -
and my Code.gs file has this code, which is supposed to execute when a new submission arrives -
function sendemail(e) {
GmailApp.sendEmail("sampleemail#gmail.com","Hello there","This is a sample sentence.");
}
When I tested this trigger by submitting a new form, I received the test email successfully. Now instead of a static email (sampleemail#gmail.com) being used as the recipient of the email, I want it to be the value my user entered in the Email field in the form submission which triggered this function. So I want my code to look like -
function sendemail(e) {
GmailApp.sendEmail("<email from form response>","Hello <name from the form>","This is a sample sentence.");
}
So if the trigger data is arriving in the e parameter in sendemail function, how do I get the data which was submitted? When I debugged JSON.stringify(e), I get {"authMode":"FULL","response":{},"source":{},"triggerUid":"12647267"}.
Is there any way I get the data that was submitted in the form in the function? Do I need to change any settings on my form or trigger to get the data? I've just started with Google Apps Script and am a bit confused on how should I do this... Any advice is appreciated! Thanks :)
Solution by #Cooper in the comments
Try using the onFormSubmit trigger for the linked Spreadsheet. It;s event object contains namedValues and values.
Thanks #Cooper, it works perfectly now. Cheers!

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();
}

Add button to trigger script along top bar in google sheets

I have written the script below to hid all rows that have a specific box checked. I set a trigger using the clock and every 10 minutes but instead seems to run every time I check a box and it screws up the view every time I check it. I would like to change it to a manual trigger that is a button along the top bar that someone manually clicks. Can anyone help me edit it?
function onEdit(e) {
var s = SpreadsheetApp.getActive().getSheetByName('2 Week Snapshot');
s.showRows(1, s.getMaxRows());
s.getRange('C:C')
.getValues()
.forEach( function (r, i) {
if (r[0] == 1)
s.hideRows(i + 1);
});
}
As mentioned by I'-'I, the name given to your function causes your script to run when an edit is made to the sheet. From the Apps Script documentation on Simple Triggers:
To use a simple trigger, simply create a function that uses one of these reserved function names:
- onOpen(e) runs when a user opens a spreadsheet, document, presentation, or form that the user 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.
The e parameter in the function names above is an event object that is passed to the function. The object contains information about the context that caused the trigger to fire, but using it is optional.
Renaming your function to avoid binding this simple trigger will prevent edits of your sheet from triggering your function.
Documentation to look at:
Guide to Menus
Guide to Triggers

onEdit() function does not triggered when change was made by automatic script

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:(

Google App Scripts - Create Google form using CreateForm() function and run MarkForm() function on form submit

Aim
I am currently using Google App Scripts to create a form that one submitted sends an email to the respondent with a detailed summary of their results.
Methods so Far
To achieve this, I have written two functions: one function to create the form and a second function to mark and email the summary of the results to the respondent.
function CreateForm(){
//Working code to create form here
}
function MarkForm(){
//Working code to create and email detailed summary of results
}
In order to make this code work correctly, I have to:
Create the form using the CreateForm() function
Open the new form and paste the MarkForm() function into the script editor.
Set a trigger OnFormSubmit to run the function MarkForm()
Question
Is it possible to do the three steps above using one function?
You can use the "On Form Submit" installable trigger to run a function when a form response comes in. Reading the Google Triggers guide in the Apps Script Documentation will help.
Do the following:
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. (MarkForm())
Under Events, select From Spreadsheet.
From the next drop-down list, select On form submit.
Click Save.
Now, when a submission comes in, the form response will be marked automatically. This trigger is an installable trigger, which means it needs to be set up through the menu rather than calling a function like onEdit().