onOpen trigger for view-only users - google-apps-script

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.**

Related

Google doc generated by app script has unexpected owner

Beginner question – I have an app script in a Google Sheet (response sheet from a Google Form) generating Google Docs. It is unexpectedly making me the owner of those docs. The business owner is the owner of the form, sheet, doc template, and shared Google Drive folder where the docs are. She has given me edit access to all. She also is shown as the owner of the app script “project” in the sheet. The app script creates a copy of the template doc in the shared folder, and populates it with data from the form. I am the owner of these new documents, why is that? I am only editor of all the components involved. Can I fix it so that she is the default owner of the new docs?
The newly created documents will be owned by the account that runs the script. This is clear enough when you run the script manually, but it also happens when you run it through an installable trigger. The trigger owner will own the files created while the function runs.
Check who owns the 'formSubmit' trigger.
(From your script editor, view your triggers. The left most column will show who the trigger is owned by.) Since your name is appearing as owner, the trigger is most likely still owned by you.

Google sheets button to open marketplace with specific addon page?

Is there a way to write an apps script function that opens up google workspace marketplace with a specific addon page? I've seen some addons do this.
Like I want to create a button drawing on my sheet and assign an apps script function to it that opens up the marketplace on that sheet with a single click.
The "trick" is done by creating a spreadsheet from a template having an add-on.
When a user that have not installed yet the add-on tries to run the add-on, the Marketplace listing will be shown, otherwise the function assigned to the button will be ran.
Broad steps
Preparation
Create the add-on
Create the spreadsheet to be used as a template
Install / activate the add-on on the spreadsheet
Add the button and assign on of the the add-on functions.
Submit the spreadsheet to the template gallery
Distribution
Use the template to create a new spreadsheet
Share the new spreadsheet to the user(s)
Use
The user open the new spreadsheet
The user clicks the button
Resources
Create a file from a template
You can't open a Dialog like the mentioned
Google Spreadsheet cannot contain a button that opens an iframe since it's outside docs.google.com
As a workaround you can open the add-on link in another tab rather than in a dialog on the same Spreadsheet page. There are several posts related despite they are not the same in your case but pretty much what you are looking for:
Google Apps Script to open a URL
How do I open a web browser using google apps script?

Sharing Google Sheets with script

I am using Google Sheets as an online database for six users. It has buttons that run scripts mainly to hide/unhide sheets. People I share it with, tell me they require permission or authorization when they activate the button to run the script. I don't know how to grant permission. Can anyone help, please?
the script is attached to your spreadsheet and at some point, you authorized the script for your spreadsheet. now if you share that spreadsheet then all your people will need to do the same - authorize the script for a given spreadsheet for themselves (in other words, they need to allow running those scripts because all non-authorized scripts are by default disabled).
prompt them to enter script editor and press run button (triangle-shaped). a window will pop up with the question if they want to authorize those scripts. when they do they can use those scripts too.
if you are advanced script user you can set up an installable trigger for your spreadsheet to ease this process

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.

Spreadsheet Editable only with HTML Service gui?

I have a Google App spreadsheet that I would like to make available to anyone with the link. It uses a custom gui to edit the cells of the spreadsheet. The custom gui is a html file launched using the HtmlService class in the Google Apps Script file.
How can I adjust the settings of the sheet so that anyone can open the spreadsheet and use the custom gui to make changes to the spreadsheet, but not make changes through any other means?
Here are the results I've had under various scenarios.
The gui works as expected but the sheet can be modified without it:
Anyone with link can edit
Gui launched via custom menu (App script runs as user)
The gui works as expected but the sheet can still be modified without it:
Anyone with link can edit
Gui launched via installable trigger (App script runs as trigger owner, which is also the document owner)
The gui cannot edit the sheet and neither can users otherwise:
Anyone with link can comment
Gui launched via installable trigger (App script runs as trigger owner, which is also the document owner)
My thought was that since the installable trigger is running as a user with permissions to change the sheet, that would work. Does that trigger's permissions not extend to a HTML Service gui that it opens?
Its not related to htmlServices. Simply any gui that you launch by any means from the spreadsheet will run as the user using the spreadsheet.
You need to publish the service (to run as the script owner) and use that gui through its service url, NOT launching it from the spreadsheet (menu etc).