Change permissions on the Spreadsheet - google-apps-script

I have a script (Script by spreadsheet) who writes a series of data in a spreadsheet, but some parts of the spreadsheet are locked.
The users that are running this script from the spreadsheet are blocked in certain areas, it is possible that Script release these areas and then lock them again or run as adm of the Spreadsheet?
Thank you.

The user would need to have permission to edit sharing on the Spreadsheet, which would not be the case if it was locked.
Is the script an onEdit trigger? If so, you can add a trigger from the script editor that will run as yourself. Just make sure to rename onEdit() to something else so it doesn't try to run as the regular user as well.
If this isn't onEdit, I would suggest making the function they call add a specific value to a specific cell. Then you can have a timed trigger every minute running as yourself that watches this cell and does its work when the cell contains the specific word. Just make sure to clear the cell quickly so it doesn't run twice.

Related

Google Apps Script: How are edit events from a bounded script in one sheet treated on another?

I'll try to be as specific as possible. This questions relates to Google Apps Script, bounded to two sheets.
I have a function that runs on edit through an installable (not simple) trigger. (Installable because it needs to call e.g. GmailApp). Call it Sheet A.
On another sheet, which we'll call Sheet B, is a script which causes an edit to be made in a cell on Sheet A.
When an edit is made manually on Sheet A, the installable on-edit script runs fine.
When the edit is made via the script on Sheet B, the installable on-edit script does not run. In other words, there doesn't seem to be any event associated with edits made by a script.
I am wondering if there is any way to trigger the on-edit script on Sheet A to run when changes are being made through another, external, bounded script.
I've looked through GAS documentation and searched as best I can here, but I cannot find an answer. I've tried to use a simple trigger, but it also does not seem to "pick up" the edit event when performed by another script.
Any help would be greatly appreciated.
Triggers only run on "human" changes. You need to rethink your flow.
If you have a function funA() changing something programmatically in a cell and you need to trigger another function funB(), just call funB() directly from funA() without counting on a trigger.
onEdit is triggered only if user updates a cell, not when the cell is updated programmatically.
You should expose onEdit from sheet A (or extract the method that actually does send the emails) and publish it as API executable
Then you can call that method from sheet B, after the edit is made programmatically.

How to allow to run google script on a protected sheet (in a spreadsheet) [Can't use trigger and web app]

I'm coding a script for a spreadsheet. This script creates a menu.
Then by choosing an option in this menu a function (which uses API) will run in order to filter some columns and hide others.
The problem is:
This sheet is protected (because shared with coworkers) but I want to allow people to run the script, which is impossible without the permission.
I already looked at different solutions:
Using a trigger: Doesn't work because a trigger can't correctly call a function which uses API (yes, my functions use API).
Web App: When the script is run from the spreadsheet, the script is run as the current user, not the script editor. (the web app is efficient if the user uses the HTML page.)
Remove protection -> run the function -> Re-add protection: Can't modify the protection without permission, which is logical.
Add the current user in the list editor -> run the function -> Remove the current user from the list editor: Can't modify the editor list without permission, which is logical.
How can I solve this problem?
This is a pain that I have not seen a good workaround. Google should know that in a collaborative environment that the owner would create script and want users to be able to run those scripts while at the same time not messing with formulas or cells that you desire to protect. The only way I have found to solve this on the sheet itself is to Unprotect and then Protect the cell or range you are making modifications to during the script run. Do be mindful that if the script fails in the middle (after you have unprotected it) the cell remain unprotected. Might want to run within a "try" script.
Are you the spreadsheet owner?
If no, you are not authorized to bound an apps script on it.
But you can make a copy of the spreadsheet by duplicate it and save in your drive.
If the spreadsheet is created by you yourself, maybe you created it by another username. If it does, log in by that user and change the sharing option to allow the specific user can edit it.

Google Apps Script - Is it possible to allow change to some cell in Sheets only through GoogleAppScript?

My making a catalog in Google SpreadSheet. My spreasheet has a table where the user can add information. This table is dynamically created through a Script that gets the data from the database sheet.
The user can edit some cells in the table and click on a button that saves the changes back into the database sheet.
I want to limit the cells he can edit manually, but I can´t protect theses cells, because there is a script that modifies it (since the table is dynamically generated through a script).
Is there a way to protect cells so that they can only be edit by a script?
I can not just protect some ranges, because it is the user that runs the script that modifies the cells. If I protect the ranges, the user can´t change the range either manually or using the script. I want a way to protect the range against manual input, but allow the script, that the user runs (not the spreadsheet owner), to change the cells.
Thank you
I have solved this issue by the following steps
printsht.getRange("A1:Q22").setDataValidation(null);
Do whatever processing in the range
printsht.getRange(1,1,22,17).setValues(prin);
After all the processing and setValues etc
// block editing after printing
var cell = printsht.getRange( "A1:Q22");
var rule = SpreadsheetApp.newDataValidation().requireTextEqualTo('$$').setAllowInvalid(false).setHelpText("Do not edit in print sheet").build();
cell.setDataValidation(rule);
Now no one can edit the range manually. All the formula etc will work normally.
But, if the user is having edit rights, he can easily remove the validation from the system menu. So, it is not foolproof.
Instead of using a bounded script use a stand alone script to create a web app that runs with the owner permissions. This because the bounded scripts runs this the permissions of the user that execute the script.
To help you user to execute the webapp, I think that you could create a user dialog or side panel that will display the link to the webapp.

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().

Can a google script edit a cell if the user does not have permission to edit the sheet?

I am writing a script for google sheets, and I need to know if the following is possible:
If a user does not have editing permission for a sheet, could they still run scripts which edit the sheet?
Basically, my goal is this: I have a sheet of data, and certain people need to be able to edit certain cells. However, I do not want them directly editing the spreadsheet, so I have created a GUI that allows users (which the script validates) to edit particular cells. Will the users be able to run the scripts which edit cells if they do not have permission to edit the sheet itself?
Thanks,
Jordan
I had came across this same requirement. I did some work around.
In your case what you can do is
1. Create a hidden sheet. This will not be protected for any user, but it will hidden
2. Save the data that is entered through GUI to this hidden sheet and then call onEdit form here.
3. onEdit will move the data in hidden sheet to the visible-protected sheet.
Since onEdit works under the script owners id it will be having access to the visible-protected sheet.
This way has worked for me. Please check.
Best
That's easy to test by yourself... try this sheet with "view only " rights
No possibility to run a script ! not even running the installed onOpen trigger.
The only way to get such a workflow it to deploy a webapp that runs "as you" where users could view and edit the cells in the Ui itself and have the webapp reflect/update the spreadsheet.
Quite a lot of work (depending on the complexity of your spreadsheet) but perfectly doable.