Google App script: You do not have permission to call prompt - google-apps-script

I have created a google script that show me a prompt dialog to write a comment when a column is edited. For some reason this only works with my email (script's creator), but with the other users that I have shared the spreadsheet don't work. When I open the script editor with other user accounts I can see the error in the View -> Execution Transcript: Execution failed: You do not have permission to call prompt.
My script function has a name "sendManualEmail" and I already have the trigger created when Event -> From spreadsheet -> On edit
I even created a new project for only that script and asked me the permissions to send emails with my account, but still not working for other users. I have read some other similar topic with same issue but I am still not able to fix mine. So thanks in advance for any comment!

You need your users to authorize your script. To do that, create a menu that activates on onOpen(). When clicked, send a message box, ensuring your users have to authorize your scripts to see the message.
Paste the following at the top of your script:
/** #OnlyCurrentDoc */
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('myName') // your name
.addItem("Activate myName's script", 'activateMyStuff')
.addToUi();
}
function activateMyStuff() {
browser.msgBox('Script is activated! thanks.');
}
Important: when your users click the menu, they will also be prompted to authorize your scripts and all the permissions on the script's page. Make sure you clean up that script, otherwise your users may have to authorize weird things - and likely won't. Do test it with an alternate email address to see what others will see.
Lastly, consider publishing your script as an addon instead. It will make it that much easier for your users to authorize and use your work.

Are you logged into multiple Google accounts in the same browser?
Google Scripts insides may sometimes not work as expected when multiple accounts are authorized in the same browser session. Try logging out of all accounts except the one that owns the script.

Related

GAS Ask again for permission and Add Permissions Description

I have a number of scripts running on Google Workspace. The is other users may blanket deny permissions to access sheets, email or other API calls which automatically generates an error message.
I believe this is because the permissions box does not say why it is requesting permissions only what permissions is requesting and none IT personnel will deny them for safety reasons.
So now I'm stuck with a bunch of errors and a bunch of users not correctly using the scripts i have set out messing up a lot of the forms.
I am looking for a way to ask for permissions every time a user opens a file if permissions have not been granted and add a description of why these permissions are needed for the file to work correctly.
I was hoping the UI app would allow me to insert a UI before the permissions option
example:
function onOpen(e){
try{ //or if (DriveApp.Permissions.Edit === true); if permissions can be queried.
var d = DriveApp;
}catch{
var ui = SpreadsheetApp.getUi();
ui.alert(
'This Sheet requires permissions for The menu options to work correctly.\nPlease allow.'
);
//DriveApp.request_new_permissions(); //psudocode
}
var dapp = DriveApp;
}
I have been trying to check the permissions through code but it doesn't seem part of the API.
Logger.log(DriveApp.Permission.EDIT === true); -> false
Current Behaviour.
User opens a Spreadsheet with onOpen trigger.
The App Scipt require use of Drive API and requests user permission.
User selects Deny
Later the user opens the same Spreadsheet with onOpen trigger.
Drive API has already been denied so the onOpen trigger fails.
Desired Behaviour:
User opens a Spreadsheet with onOpen trigger.
A box pops up saying "This Spreadsheet requires the use of Drive API to create folders for you. Please allow if you wish to use this feature"
The App Scipt require use of Drive API and requests user permission.
User selects Deny.
Later the user opens the same Spreadsheet with onOpen trigger.
The onOpen trigger checks if Drive API permissions have been Aloud.
if not:
A box pops up saying "This Spreadsheet requires the use of Drive API to create folders for you. Please allow if you wish to use this feature"
Drive API requests Permission again.
else:
The script executes normally

Scripts are not executing for users with reader permission

I have added an image on my grid, and assign script to it. But script executes only when I click on image, and when another user(with reader permission) clicks on image nothing happens. The same situation is with onOpen() trigger - nothing happens when user whith reader permission opens the grid. I understand that editor permission will fix it, but I don't need the user to be able to edit the table, I only need that user be able click buttons(image)/get alive triggers/menus that i create, but not edit anything manually. How can i do this(may be i should give some extended reader permission, or to turn on something in GAS project)?
When users execute a script for the first time, they will get an allow permissions prompt, so they can authorize the script to make changes on their behalf.
However, if the user has viewing permissions over the sheet or file, they cannot authorize the script to run or make changes on their behalf since they cannot make changes to the file. (I found a similar question, with the difference that the sheet was embedded in a website that has some information about why it does work here).
And it's documented in the Apps Script documentation here:
An installable open trigger runs when a user opens a spreadsheet, document, or form that they have permission to edit.
You can also request a feature request to Google asking for an option to allow users with "viewing permissions" to run scripts here.
In this case I think you may give editor's permissions but protect the sheet/range so only you can edit them with this option -->
https://support.google.com/docs/answer/1218656?hl=en&co=GENIE.Platform%3DDesktop#zippy=%2Cprotect-a-range-or-sheet
Let me know if this worked!
Option 2:
Adding to those protected ranges you can too create a Library with your functions in another spreadsheet (or in Google App Script independently) -> read about it here
You can now set buttons/menus associated with those original functions, but I think that this can give you an altenative
For example:
function function1_toOrigin() {
LibraryName.function1()
}
function function2_toOrigin() {
LibraryName.function2()
}
Yes, they'll be able to access to these linking functions but nothing more, I think they won't be that unreliable??
IMPORTANT: If there are modifications to the script you should deploy them again as a new version of the library and update the version in your spreadsheet by double-clicking in your library

Google App Script and PropertiesService.getUserProperties()

I use the google app script in my google spreadsheet document.
The settings of each user of my spreadsheet doc is saved in:
var userProperties = PropertiesService.getUserProperties();.
To save the settings I use
PropertiesService.getUserProperties()setProperties({some properties}, true)
The problem is the first user saves his settings and another users get the settings of the first user using PropertiesService.getUserProperties()
But they should not, they should get own settings.
Do you have any idea how it is possible?
They must be logging in with the same user account for this to happen; a user can only ever access their own user properties. This could be because any triggers were created on your own account (say for form submissions), or web apps were deployed to run as you.
Share a copy of your sheet (andrew#roberts.net) if you would like me to a take a deeper look.
In fact as the support answered me, it is not a bug. I had wrong user properties on handling triggering event.
User properties are private to the user executing the script. However, when you setup an installable onEdit trigger, the code always runs as the user that setup the trigger:
"... runs with the authorization of the user who created the trigger, even if another user with edit access opens the spreadsheet."
https://developers.google.com/apps-script/guides/triggers/installable
Since the code always runs as the same user, it's always accessing the same user properties. In general it's not possible to get the identity of a user if they haven't authorized the script.

Share a Google App Script to other users - permission requested & authorize script

I've created a spreadsheet with some GAS behind it that opens a form with some inputs, and now I need to deploy the webapp and share it to some other users. The problem is that when the other users try to open the spreadsheet (shared in Google Drive), they get this error message:
I've already tried to:
Share the spreadsheet to the user/s (Adding the user/s email address with permission Can edit)
Deploy the webapp settings with the following settings:
As you can see, the setting screen reports "You need to authorize the script before distributing the URL".
How can I authorize the script? I can't find a way to do that. Do you know if there's another way to share and let other users run the script?
Since you selected to execute the app as you, you need to authorize this script first.
Just select method doGet and press run in the editor, you will get the chance to authorize.
The problem is you are mixing web app with ui dialog on a sheet.
The webapp is accesed from the webapp url (given when you publish the webapp).
The ui dialog is a window shown on top of the spreadsheet.
You want to use webapp, follow the tutorial for that. Publish it to be accesed from the domain if all users are in the same gapps domain. Else use public anonymous.
Run doGet before publish to authorize. Its also possible to do the other ui-on-sheet but you need more steps for each user to authorize.
You need to manually verify that your script has access to the document.
Add a function to your script that accesses the document:
function doInstall() {
var mysheet = SpreadsheetApp.openById('x4535khxxhidh...');
}
In script-editor select: Run->doInstall (Or what ever you call your function) when you should be asked to verify that the script is authorized to access the document.
The following worked for me:
Go to «Publish»
Click on «Deploy as web app»
Click on «Disable web app» (in case you have published it already)
Execute the Script clicking on «Run»
Authorize the Script and grant permissions with your account
Again, go to «Publish» and click on «Deploy as web app»
Add another «Project version» (in case you have published it already)
Change «Who has access to the app:» to «Anyone, even anonymous» as needed

Automatic Notification when Google Doc is opened

I have a Google Apps Script that contains the following code:
function onOpen() {
var emailAddress = "jthfortyone#gmail.com";
var message = "Viewed"
var subject = "Someone Viewed Your Resume";
MailApp.sendEmail(emailAddress, subject, message);
}
I want to receive an email every time someone views my resume
But for some reason I only receive an email if I am the one open it and not when anyone else does.
What do I need to change to get the desired results?
Your container-bound script in a shared document will only run if:
Sharing has granted edit privileges. (Viewing and Commenting aren't enough.)
User is logged in with a Google account. (Only scripts run as webapps can be triggered anonymously, at which time they run as the owner. No such option, in this case.)
User agrees to authorize the script. (A script that sends emails will require authorization.)
Since this is your resume, I think you're going to need to find another solution.
The onOpen trigger should run in response to the active user opening the document.
The active user is defined as:
The user (Google account) that causes a script to execute. Depending on the execution method this may be different from the effective user. You can also think of this as the user at the keyboard
Have you tried accessing the document with a dummy account?