Triggering a macro when a specific sheet is selected - Google Sheets - google-apps-script

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.

Related

Can I use Google Apps Script to activate my spreadsheet?

I have an active spreadsheet, I want to select a cell in this spreadsheet and then bring me to another Spreadsheet. I used app.openByUrl to open the spreadsheet and then get my wanted sheet by name and then activate the cell by getRange, the script is finished but it didn't take me from the active spreadsheet to the other spreadsheet, the cursor remains in the cell of the active Spreadsheet. I use the following code. Does anyone has an idea?
app.openByUrl("https://docs.google.com/spreadsheets/d/1S9Kc6foLZ_bXqs4/edit#gid=502179704").getSheetByName("Verification Candidates").getRange('D4:F4').activate();
It's not possible to activate an external spreadsheet by solely using Google Apps Script as
Class Spreadsheet doesn't include like similar to Class Range activate
The Class Spreadsheet "open" methods open the spreadsheet on the server side, not on client side.
You might be able to do what you are looking by using other tools (i.e. a complex bookmarklet)

OnRow add event and update cell in a sheet

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

How to copy a Google sheet and keep it's project triggers and scripts?

I currently have a Google Sheet that I'm using as a master template. That is, I'm making a copy of this template for every request. I want to add a Google App Script (which onEdit POSTs to my server when the sheeting is completed) to my master template that will be duplicated and run for every copy of this template.
I've tried doing this from an admin account, however, the scripts don't seem to 'stick' with any of the templates. Is this possible?
Installable triggers can be attached to any spreadsheet (respecting sharing permissions) from any project. You can add a new trigger to your master sheets project for each copy that is made. In the second example here they suggest using SpreadsheetApp.openById() you could also use SpreadsheetApp.openByURL() or the Spreadsheet returned by Spreadsheet#copy() depending on how you are duplicating your spreadsheet.
Personally, what I do in 2022 is including something like this on the beginning:
function scriptSetup() {
createOnEditFunction();
}
function createOnEditFunction() {
const ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('onEdit')
.forSpreadsheet(ss)
.onOpen()
.create();
}
function onEdit(e) {
//your stuff here
}
This way, the scriptSetup() is located on the first line so when copying a spreadsheet I would ask them within Sheets to click on Extensions > Apps Script > Run.
As per Jared Pinkham's link referencing Developer documentation, there are a few things Apps Script can use as triggers other than onEdit.
There are several installable triggers for Google Workspace
applications:
An installable open trigger runs when a user opens a spreadsheet,
document, or form that they have permission to edit.
An installable edit trigger runs when a user modifies a value in a
spreadsheet.
An installable change trigger runs when a user modifies the structure
of a spreadsheet itself—for example, by adding a new sheet or removing
a column.
An installable form submit trigger runs when a user responds to a
form. There are two versions of the form-submit trigger, one for
Google Forms itself and one for Sheets if the form submits to a
spreadsheet.
An installable calendar event trigger runs when a user's calendar
events are updated—created, edited, or deleted.
You can refer to the following link for spreadsheet triggers.

How to open a google spreadsheet by script in a browser tab

I have set of spreadsheets which need to be opened in individual tabs in browser. Each sheet has got script to run on onopen trigger. Is there a way to open each spreadsheet in individual browser tabs from another mastersheet script?
No, you don't have the ability to open links from a Spreadsheet's script, the user must click the link.
You can converge all sheets into one with scripts or importRange tough.

Spreadsheet event trigger on Google Apps script doesn't fire when the sheet is edited by another script

I wrote a small script bound to a Google spreadsheet that reads an email-address from a cell in the last row and sends an email to it.
The values are collected by a WebApp (not by Google form).
I tried all kind of triggers. The time driven triggers work, but the spreadsheet triggers don't work. I tried all of them. If I change manually some cell in the spreadsheet the onEdit trigger is working, but it doesn't fire up when the sheet was changed by another script.
Google's dev says : The onEdit() trigger runs automatically when a user changes the value of any cell in a spreadsheet. but it doesn't precise the change could be from script or not.
You'll find here a comment from HDCerberus :
I'm not at my PC to write a full response, but at a glance it could be the OnEdit trigger, which ONLY works when the sheet is edited manually, and NOT by a script. Are you expecting it to run when the sheet is edited manually or by a script?
Not every change triggers onEdit(). Please take a look on answer from topic Detect user inserting row or column in a google spreadsheet and reacting in a script - you will find registered bugs and one of them says that values written by scripts do not trigger onEdit().