I would like to catch an event once a row is added to a google sheet (via google forms) using google script api , is it possible ?
My Spreadsheet has multiple sheets but i am only interested on the rows added to a specific sheet.
Thank you
Short answer
If you are using the built-in feature to send responses from a Google Form to a Google spreadsheet, use the on form submit event.
Explanation
The Google Sheets API doesn't have event triggers, only Google Apps Script. By the other bear in mind that the installable trigger on change is triggered only by changes made by the "user on keyboard".
References
https://developers.google.com/apps-script/guides/triggers/
https://developers.google.com/apps-script/guides/triggers/events
Related
How can I trigger macro doAction when anybody clicks on sheet targetSheet?
Update:
onSelectionEvent works on sheet change.
Currently, There is no trigger for sheet selection available on Google apps script. Therefore you cannot do that.
I am looking to locally use a Google Apps Script with one of my Google Sheets (Attached to a form) that will be accessible to all emails that have permission to access the sheet, but I don't plan on publishing this script to the public. Do I still need to publish in order to use this script, or will I be fine just adding an onSubmit function for my Google Form as a trigger for my script to run in my Google Sheet? And can I use installable triggers if I plan on operating this script as a standalone only attached to this one Google Sheet?
Do I still need to publish in order to use this script
No: If you own the spreadsheet (or have editor permissions on it), your script can remain private. A script does not need to be bound to a Spreadsheet except if it will use:
Spreadsheet UI (getUi(), custom menus, dialogs and sidebars)
"Active" items (getActiveSpreadsheet(), getActiveSheet(), getActiveRange(), getActiveCell())
...will I be fine just adding an onSubmit function for my Google form as a trigger for my script to run in my Google Sheet?
Yes: Your standalone script can have installable triggers associated with Google Apps items such as spreadsheets, documents and forms.
And can I use installable triggers if I plan on operating this script as a standalone only attached to this one Google Sheet?
Yes: There's nothing special about the 1:1 relationship; the same trigger could be installed for multiple spreadsheets.
Is there a method or way of identifying the last edited sheet in a Google Spreadsheet using Google Apps Script?
You would have checked the official documentation and found that no such method exists.
To accomplish this, you would need to write a proactive script to:
Use an onEdit() trigger function to identify the occurrence and source of edits.
Record the details in a non-volatile store; perhaps another spreadsheet.
Respond to queries about the last edited sheet.
I am writing a Google Spreadsheet add-on that will pull data from web pages (calling URLFetch with an hourly trigger) and write the data to a spreadsheet.
However, I only want this add-on to only write to the same spreadsheet even if the user is opening the add-on in a different sheet.
Is it possible to force the user to work with that add-on only in a particular sheet? TIA.
Yes it is possible, when calling the SpreadsheetApp service you can call the sheet you want by name or the one you need from the list of sheets.
SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
SpreadsheetApp.getActiveSpreadsheet().getSheetName()
Then you would write the information in that sheet.
hope it helps.
Phrased another way, is there a way to:
Create a new Google Spreadsheet (from the Google Drive API)
And then create a Google Apps Script associated with that Google Spreadsheet programmatically
And then programmatically activate the "On Edit" and "On Form Submit Triggers" to call various methods in the script? (this is the part I cannot figure out)
-OR- (as #JacobFlatter suggested)
Manually create a spreadsheet with the desired script
Use the Google Drive API to programmatically copy the spreadsheet (which copies with script with it as well)
Somehow programmatically activate the "On Edit" and "On Form Submit Triggers" (which DO NOT copy over from copying the spreadsheet, this is the part I cannot figure out)
Possible path to follow:
Create a container bound script with an onOpen() trigger within a spreadsheet.
Copy the existing Spreadsheet (which will copy the script as well) programmatically.
Open the new Spreadsheet programmatically (unsure if this will initiate the trigger described above).
This assumes a few things, but seems like it is worth investigating. I'm curious if this works. Good Luck.
Unfortunately, no. You cannot do meta programming with Apps Script.
Based on the answer to this question, I wrote a simple function to try meta programming
function meta(){
var mime = 'application/vnd.google-apps.script';
DocsList.createFile('MetaScript','function test(){}', mime);
}
It throws up an error saying Invalid mime type