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

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.

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)

Triggering a macro when a specific sheet is selected - Google Sheets

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.

Standalone Script for Google Sheets

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.

onOpen trigger for view-only users

The built-in onOpen trigger in Google Apps Script only runs when a user with permission to edit opens a spreadsheet or document. I am developing a spreadsheet where only developers have edit privileges and all other users must have view-only privileges.
How do I create an onOpen trigger that fires when view-only users open my spreadsheet as well as those with edit privileges?
You can't, because it's not supported. Google Apps Script, embedded in either Google Sheets, Docs, Forms, and as an add-on, runs only for editors.
The alternative is to Publish as a webapp, so the viewers open that instead of the sheet directly, or open the webapp (on another tab, not inside the sheet) from a link in a sheet cell.
From the "Restrictions" section of trigger documentation on triggers - both simple and installable:
They do not run if a file is opened in read-only (view or comment) mode.**

How to create a Google Sheets Script that runs as soon as the user opens a spreadsheet?

How do I create a script that runs as soon as the user opens a spreadsheet?
Specifically I need a script that when the spreadsheet is opened asks the name and sex of the user, and depending on the gender it adds the name to a different sheet of the spreadsheet.
This is simple but I've gone through the tutorials on their webpage and couldn't do it. I know how to program but I am new to Google Apps Scripts.
Also is this something done better in Google Forms or in Google Sheets?
How do I create a script that runs as soon as the user opens a spreadsheet?
An onOpen() trigger function.
Specifically I need a script that when the spreadsheet is opened asks the name and sex of the user, and depending on the gender it adds the name to a different sheet of the spreadsheet.
The onOpen() should call a function that uses Browser.inputBox() to get the user's input, then writes it to the sheet via Range.setValues().
Note that the user will need to have edit privileges for this to work.
Also is this something done better in google forms or in google spreadsheets?
If you want the UI to show up in a spreadsheet, then the script must be contained in a spreadsheet.
Alternatively, if you don't want the user to see the spreadsheet, you could use the Forms service to collect their input, with no need for any programming.
Nothing too hard to get that :
read this doc on how to build an alert in a spreadsheet and make it show up using an installable trigger onOpen
all you have to do is put these together.
For script lauched at opening you need write function:
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
...
};
Google Spreadsheet don't have a dialog window or something similar.
But there are several ways:
You can use show method to show your html or UiApp input dialogbox: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#show(Object)
you can use one sheet for input "sex" and hide others sheets. After click button you can show all sheets. Here is SpreadSheet API: https://developers.google.com/apps-script/reference/spreadsheet/
you can use information from profile logged person and read his/her sex :)
you can use Form for input "sex" and switch context (by script) to spreadsheet but this is more complex solution. Here is Form API: https://developers.google.com/apps-script/reference/forms/
Yea, I forget about inputBox() :)
To create a script that runs as soon as the user open the spreadsheet, you should create a bounded to a spreadsheet script and use a trigger:
Create a new or open an existing spreadsheet.
Click on Tools > Script editor...
Click the close button on the welcome dialog.
Start writing your script
To make the script run on open, you could use a simple trigger by using onOpen as the function name or you could use an installable trigger.
For further details see https://developers.google.com/apps-script/guides/sheets