Permission denied when trying to create a spreadsheet using Google script API - google-apps-script

In the corporate environment with a corporate account I have a Google Document. Think of it as a master document that is being maintained.
I want to add a script to it to create a derived spreadsheet that can be used by people who need a subset of data from the master document.
In the master document I created a script file so the script is bound to the document.
Inside the file I defined a function. Function does some search and processing in the master file without problems but once I try to create or open an existing spreadsheet to extract data from the master document to a derived document the call fails.
I tried several approaches:
SpreadsheetApp.openByUrl();
SpreadsheetApp.create("My test sheet");
and approach covered in the solution
I also tried execution from an installed menu trigger or from debugger - same result.
In all cases I get a pop up with :
Authorization required
{doc name} needs your permission to access your data on Google.
with two buttons to review and cancel.
Clicking review leads to a message "An unexpected error occurred" at the top of the screen.
I tried different machines and browsers and switched V8 engine on and off.
I also made sure that the document is open to all in my org if I try to open an existing doc by URL.
What am I doing wrong?

It turned out that there were two things.
Adding scopes solved the problem.
I had an invalid scope that
was already there (I do not know how it got there, probably from an
example) that I preserved in the manifest and might have misspelled.
That was a mistake. It caused all sorts of instability. Only when I
tried to execute the script in FireFox instead of Chrome was I able
to see a popup that gave me a hint that one of the scopes is invalid
and I need to fix it.
Once I fixed it, everything started to load as expected.

Related

Google Sheets Script not sticky

I am trying to create a custom function for a Google Sheet. to do this, I start from within the Sheet that I want to use the function, I then go to the menu Tools|<>Script Editor.
I then write a function. Name the file. And save it.
But, I can not use this function. Error #NAME?
Then if I close the Script Editor and open it again, it's like I have never been there. It presents the same form you see when you first start.
From an OP comment
I have discovered the issue, but not the solution. When the script editor is opened it switches me to another Google account I have. So I switch back. But then I am presented within a screen saying Acess Denied and making me ask permission. To whom and what for I am not sure.
The solution to the root cause of the problem depends on Google, fortunately there are several workarounds.
Use Chrome in incognito mode with all the extensions disabled, then sign in on only one account (the key part is bold)
Use another web browser or web browser profile. Sign in on only one account (the key part is bold)
Signout of all your Google accounts, then sign in first on the account that you will be using for developing your scripts, then sign in on the other accounts. This still migth be problematic under certain circunstances.
Also, as good habit, when you click Tools > Open script editor to create a new bounded project, the first thing that you should do is give a name to your project.
Another god habit, before testing a custom function, be sure that there isn't a project file with a red asterisk (unsaved file). Contrary to spreadsheets, the Apps Script files aren't automacatily saved when a edit is made.

How do I unlock "padlocked" Apps script files in Google Apps Script?

I have a script project in which some "padlocked" .gs files are shown. I can't edit or delete them.
I need to use those files, but the person who locked them is unavailable.
I was trying to unlock the files using methods from the class LockService but it doesn't seem to work that way.
How do I "unlock" these files so I can edit them?
I figuered it. The padlock doesn't point to actual files withing this script. There is a library in a standalone script containing these files. When I use debugger it reaches these files from a standalone library and they are shown within the script with a padlock. So padlock simply means that those modules are from a separate script file! So it's not a lock.
I handled it by creating same modules withing the script and simply renamed few functions. So I am able to use debug for all my route.
Unfortunately, only the owner of the file or an admin of your domain (if you're using G Suite) can do that.
I had the padlock appear on two projects I own, and am the only editor of, without understanding why they first appeared. They appeared on my work laptop but not my home laptop. At work I'd been signed into one browser window with both my work and personal accounts. When opening the google sheet that contains the script it opens signed in as my work account. However, when opening the script from the sheet, the script opened but signed in as my personal account (that also has access). I guess this shouldn't really happen but it did. The solution was simple though, I just had to use the drop down to select my work account, which opened a new window for the script without a padlock and I was able to edit as normal. Was quite confused for a while... phew!
I had the same problem. As it turned out none of the above fixed it although similar but different to the other person who fixed his. In my case i have multiple chrome users on a single machine. I log in at the browser level.  I was in the browser for user A who also owned the file. When I opened up the bound google script project attached to the gooogle sheets file, it opened up as it should but locks were on each tab. I was confused for about 10 minutes.
In the upper right corner of the interface, there was a drop down that was showing User B email address even though above that, at the browser level I was clearly in the browser of user A. I clicked that drop down and changed to User A and the locks disappeared.  It seems they have an odd way of managing users on these gsp's that subverts the browser level user login. 

Sudden permissions problem with SpreadsheetApp.openByID

The code is supposed to copy some data from another spreadsheet and then I manipulate it.
This code worked yesterday, then I tried adding a library today and now it is still throwing this error even after removing the library and rolling back my code to the version yesterday.
Error Message:
You do not have permission to call SpreadsheetApp.openById. Required
permissions: https://www.googleapis.com/auth/spreadsheets (line 21,
file "Import")
Code:
data = SpreadsheetApp.openById('1-SyJRflR1YYYYDRo3e7xnDhYYYY6hIGDBKODl9XXXXX').getSheetByName("g1.1").getDataRange().getValues();
I am NOT using "//#OnlyCurrentDoc" anywhere in either of my documents so that is not my problem.
Google sheets API is turned on for this project, as checked from the resources menu.
The library function I was attempting to use was a simple time one (nothing to do with sheets), and it is not currently being called anywhere in my project and the library has been detached.
Possible Issues:
If your library included //#OnlyCurrentDoc and you authorized after adding the library, your permissions might've just included the current document.
If you had explicitly set scopes1, then those scopes might not have included https://www.googleapis.com/auth/spreadsheets
Possible Solutions:
If you want to still use the library, add the //#NotOnlyCurrentDoc in your main script and re-run the function. If not remove the library and re-run your function.
An opposing annotation, #NotOnlyCurrentDoc, is available if your script includes a library that declares #OnlyCurrentDoc, but the master script actually requires access to more than the current file.
Removing the explicit scopes in your manifest will trigger the normal authorization flow.
If all else fails, visit your account, find your project and remove all access. Now, re-running any function in your script will trigger normal authorization flow.

Google script trigger 'from Spreadsheet' not available

I've successfully created picklists from a Google sheet into a Google form following a tutorial (http://wafflebytes.blogspot.com/2016/10/google-script-create-drop-down-list.html) and Google's documentation, however I do not have the option to set a trigger on the Event 'From spreadsheet', as laid out in the the tutorials/documentation, so I must be missing a step and haven't been able to figure out what it is.
The result is that the options are not automatically updating and must be manually pushed. I would like to enable the options to be refreshed on open.
I do not have the option to updateForm + from spreadsheet, as pictured in those resources.
From tutorial
My options
What step am I missing? Or is this possibly a permissions issue (I am not owner of the documents, but I tried to reproduce this in docs I own and ran into the same issue)?
NOTE: I have not posted my script here because it's nearly identical to the one linked to in the tutorial.
Update:
I'm beginning more and more to think it has to do with our OAuth configurations or permissions. I even updated the manifest to explicitly authorize forms and sheets, but I'm still not offered sharing permissions. I will have our admin check in on this but if anybody has had experience in these matters, I'm all ears.
Since your options shows "From form", you are working on a project bounded to a form instead of the one that you created from your spreadsheet which is the one bounded to spreadsheet.
Rather than missing a tutorial step it could be that you had several script projects open at the same time and you are looking at the wrong one.
Reference
Event Objects

DriveEye only sends errors even after disabling it

I have added the DriveEye app to my Chrome, and enabled it to see my account. It is used for sending automated e-mail alerts on Google Drive shared folder activity.
It is not working. I receive only error messages ( I can not include it here as it is in Hungarian). It is about server error.
Then I removed the DriveEye from Chrome (drag and drop in the Chrome start screen). Nothing changed. I still receive errors.
How can I ged rid of it? How can I remove it completely or take the authorization back?
Thanks for helping
It seems I had the same symptoms.
There doesn't seem any way to remove DriveEye, but I found a way to delete its triggers, which stops the error emails.
Overview: The web-based authoring environment for Google Apps Script contains a menu item "Resources --> All Your Triggers". You can manage your Triggers here. (It appears to be the only place you can do this.)
Trick #1: Due to an apparent masking bug, you can't access this menu item unless you have at least one active script, and it is currently open. A completely empty dummy script will suffice.
Trick #2: Deleting the trigger(s) won't occur right away. You must preserve the script which you used to delete the triggers for an unknown amount of time. Might as well keep it indefinitely, I guess.
If you don't do any Google Apps Scripting, like me, you won't know how to get there. Go here:
https://developers.google.com/apps-script/
and click on the link "Start Scripting". Choose "Create script for ...Blank Project". You'll get an Untitled project window. Save the project with a descriptive name (e.g. "Dummy Scripting Project"). It will appear among your Google Drive (GD) files. (I used the GD web interface for this.) Reopen the script, access the menu item "Resources --> All Your Triggers" and delete DriveEye triggers. Save and close the project.
Now wait. In a few days, all the DriveEye error messages should cease.
HTH