Google Sheets Script not sticky - google-apps-script

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.

Related

Launching special links from a selected or clicked cell in google sheets

Google sheets restricts which link types can be launched (https://support.google.com/docs/answer/3093313?hl=en), but with google apps scripts I was hoping to build a workaround so that people can launch an internal app with relevant information (ie App:123132). onSelectionChange would seem to be an option (when the user selects the cell with the relevant link, open the app) but it's not an installable trigger, limiting what can be done (specifically, I can not launch a new page or try to redirect to a uri such as App:34234). The other triggers don't seem to provide the functionality to do this without polling, which doesn't seem to be the right way to do this. Is there any other way people are aware of to make this happen?
Without polling there isn't another way to make this happen. As you already mentioned there aren't simple/installable triggers able to open a link.
By using Google Apps Script one option is to create a web app to show the content of the "special link".
Another option is to use a dialog/sidebar to use client-side code to open the "special link"

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. 

Can I create a shortcut to execute a Google Apps script?

I have a script that runs on a Google Sheets that is fed by a Forms entry. Basically, when I fill out a form, it creates an entry on the sheet. From that entry, a new document is created by my script that "prettifies" the fields on the sheet for distribution.
This sheet is located on a Team Drive, but in order to have the resulting document created in my drive, I had to place the unbounded script in my drive and reference the sheet. If I have it bounded to the sheet, it will always put the newly created document in the Team Drive -- which I don't want.
When it was bounded to the sheet, I had a trigger that would automatically create the document when the form was submitted. Now that the script is on my drive, triggering on submission is not an option. The only options I have are time based or calendar based, and neither of these really work -- unless I only want to get the document once a month or every single minute.
So now, the only way I know to execute the script is to do it manually every time, which is a major hassle and requires multiple steps no matter how you go about it.
Is there a way to trigger this script on command without having to open the Script editor, click the script I want, and then click run?
Why are there no keyboard shortcuts in Apps? I know about Ctrl+R -- but this only works if you already have the script you want highlighted in the top action bar. If I type Ctrl+R without it already highlighted, nothing happens. Nothing happens with Alt+R or Alt anything, really. I have tried simply tabbing 187 times to get to where I need, but the tab never seems to focus "in" the script menu area. It just bounces around the address bar, the shortcuts bar, etc.
NOTE: I do not want to make major changes to THIS script. It took forever to get this working right and I don't want to jack it up. I just want to be able to trigger it without having to go through 18 steps to do so.
There is no way to execute a standalone script project's function with a keyboard shortcut from within the Apps Script Editor. If your script was bound, you would have the option of keyboard shortcuts from the container Google Sheets file (as a google-sheets-macro), but macros have some restrictions.
However, you are not without options. Two of them:
Publish the script project as a webapp and use a page visit as the trigger.
Open the page / click a bookmark -> execute the function via your doGet().
Convert your script to a 'polling' style that processes any number of form responses, and use a time-based trigger (e.g. hourly, daily) to automate the whole process.
(Include a "did I process this response yet" flag to prevent reprocessing a response)
There are more elaborate configurations (e.g. Apps Script API + local cron job) but those are unlikely to be simpler than the above two options.
As I mentioned in my comment, you more than likely just needed an extra step for the original bound script to create the file in your Google Drive and not the host Team Drive. If you're interested in that avenue, you'll want to ask the appropriate (read: new) question.
This is a workaround not an answer.
I use the Alt/ menu to efficiently look up functions by name (For me, its faster than using a cursor to work thru the nest of menus). If you design your function names so they differ from the built-in function names, the lookup may take only a few keystrokes)

Make spreadsheet only editable through script

I have a script which adds new rows and changes the sheet in some ways.
I'd like to lock this sheet so it cannot be edited manually, but allow the script to be run.
Is there a way to do this?
Yes, it's not the most easier thing to do, but it's possible.
But before we start it's important to understand how Google spreadsheet/sheet protection works.
There's no way to protect a sheet or range from the file owner. So, if you also want to protect a file from yourself (assuming you're the owner). Then the only solution available now (I do hope they change this in the future) is to change the ownership of the file to another account. What most people do is use another Google account that they don't use actively (you can create a bogus easily if you don't already have one).
Then, after you have transferred the file to another account, you can easily lock changes to ranges, sheets or the whole spreadsheet using the regular GUI.
Ok, now to the script. When a script runs, it's always using the authorization of a specific account, and it can do everything that this account can. For example, when someone runs a script directly from the script editor, or clicking on a custom menu or image: it runs under the account of who's clicking. But if you set a installable trigger, then the script always runs under the account of who setup the trigger in the first place (not who's performing the action that actually triggers it). Simple event triggers run as who's triggering the action (you can think anonymously), but they can't do much, it's a security feature (read the link for a better understanding).
Lastly, when one is publishing a script as a web-app, then there's a selection box to choose if the script runs as the developer or the user. Very simple.
Back to the problem. Since we have locked out everybody from the desired sheet/range. For a script to make changes to this locked area, it must run as the file owner!
If the changes you do are automatic, e.g. via a installable trigger, then you're good. Just setup the trigger using the file owner account and that's it. Also, if your users are accessing the script "externally", that is, via a web-app. Then that's easy to, just setup the web-app to run as the developer (the file owner).
The most complicated scenario is if you need to run the script from a custom menu, which will then run under the privileges of who's clicking on it, which themselves can not make changes to the protected area. The solution to this is to deploy the script as web-app running as the developer, and have the function running from the button click to call the deployed url using UrlFetch, possibly passing some parameters to designate what needs to be done. Then, since the webapp runs as the developer (which is the file owner), it can make any changes required, and return (if there's something to return) any value (usually a JSON) to the calling function (which is running as the user and is associated with his session), so you can show him a message on a popup or toaster, etc.
It's kind of tricky but works great, and you can do all that on the same script contained in the spreadsheet. But if you're concerned that your users may access the script editor and change the code (which is possible), than you should separate the web-app part on a different file, that you don't need to share with them.
Yes but only for others not for the script owner. Just use 2 accounts one as the owner and the other one as the reader.

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