Backing up Gapps source - google-apps-script

I have fallen into the trap of (accidentally) deleting all scripts from the script editor. Lost 3 weeks of work in this lesson. I (maybe) will not do that again, but it raises the question "What is a complete backup solution for Gapps source?"
I, of course, can make a copy of my script either in the editor, or within Google drive. I however question if my (very complex) User interface (built using GUIBilder) is protected. So far, I have been unable to locate the code GUI Builder throws.
Also note it is not possible to download an apps script in GDrive, so how do I get a personal copy of my work product?
=====
To clarify - I know how to copy and paste the source of a script BUT this does not capture the 200 gadget GUI that was built using GUI Builder.
As to the trap door in the editor -- I had several versions of this development -- I highlighted ONE in the left hand list and requested delete (from withing the Google script editor) file/delete -- magic -- ALL SCRIPTS DISAPPEARED. I will never touch the File/Delete menu again.

In some cases, you should be able to get your code back. (EDIT - except, it seems, any script file you delete from within the script editor, and any auto-generated code related to the GUI Builder, or Forms. -- Ouch!)
Your options depend on what you mean by deleting all scripts from the script editor.
If you only deleted the text of your script while in the editor, say by Ctrl-A + del, then you can recover it within the editor. File - See Revision History will bring up a dialog that lets you time-travel back to when your script was intact.
If you deleted the file from Google Drive, then it's in a trash folder, and can be recovered from there. You'll find the Trash folder in the Drive navigation tree.
If you deleted a script "file" from within the editor, you're out of luck until Google fixes Issue 1424: There is no possibility to recover script files/projects.
If you deleted any auto-generated code related to the GUI Builder, or Forms, you can't get them back. (Forms are not included in Restore from Revision History)
If you want to store your scripts some other place, that's a good idea - that will provide additional safety for items 1 to 3, but unfortunately not 4. You will need to copy the text from the editor into another file or online repository (e.g. Github). Get into the habit of including info about Libraries in your comments, because there is no way to back up those resource links.

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.

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)

How to list projects associated with Google Apps triggers?

Under All Triggers, I can see all the Google Apps triggers that exist for my user, but I cannot see what project they are associated with. How can I find the project associated with each Google Apps trigger?
mcarans,
Facing that problem i've found a (dirty) workaround. Please continue reading if you are really desperate and has a strong stomach.
Using Chrome open any script (where you can access to Edit >> All your triggers menu)
Open the Chrome developers tools (Ctrl+Shift+j)
Go to Network tab
Go to Edit >> All your triggers
Copy the recorded triggersService XHR Response
Paste it in any txt editor and dive in for triggers names and document keys !!!
Use the keys to locate de documents.
After that, keep a log with your installed triggers!

How to prevent Apps Script from opening multiple projects?

I have an html dialog for Sheets (container bound dialog) that is used for input and saving of customer data. I made a copy of it for use in a different office, so now I have Worksheet#1 with Script project "Scripts" and Copy of Worksheet#1 with Script project "Copy of Scripts". We've made changes to Scripts so it is no longer the same as Copy of Scripts.
Sometimes Worksheet#1 triggers scripts that are in Copy of Scripts. Not sure why. On these occasions I can open up script editor and it asks which project to open (Scripts or Copy or Scripts) and once I select the right one it behaves properly until the next time this happens.
How can I insure that it always opens the correct project?
You will see this behavior sometimes if you have multiple projects within a script.
Check whether you have multiple projects in one of those sheets.
File > Open...
Here's a spreadsheet-contained script with two projects in it:
Open the project you want to delete.
Delete it.
File > Delete Project...

Sharing a Google Spreadsheet containing a script with a workgroup so they can use it, but not modify it

I have written a Google Spreadsheet script which reads data from a user provided CSV file, populates a sheet with the data, makes a copy of the spreadsheet with a different name, and provides a link for the user to click to see the new spreadsheet. After making the new copy, the original spreadsheet is then cleared of user data and restored to its original state so other users can use it.
I want to share this spreadsheet with the workgroup (several hundred users), but I don't want them to be able to modify either the sheet or the script. If I share it read-only the script won't run, but to get the script to run I have to allow users edit capability and that is a bad thing.
I have googled myself silly trying to find a direction in which to go, but I am still wandering in the wilderness.
Can any of you point me in the right direction? If I need to be more specific or provide more information I will be glad to do so.
Thanks,
Larry
P.S. Other questions of note:
o Is it possible for a app-script in a spreadsheet to make the new copy active and 'close' the original shared spreadsheet?
o Can the user be made the owner of the new copy?
There is the option to put the code that you don't want modified, into a Stand Alone Apps Script file, then use that code as a "Library" in your script bound to the sheet. You can set the permissions on the Stand Alone Apps Script to VIEW only. That will keep people from changing it. When you share the Apps Script file with the users, they will get an email notification. So, they'll have the URL to the file, and can view it, but they can't edit it. (If you set it to VIEW only)
So, you'll be using both your spreadsheet file, and a Apps Script file. Two files, and setting the sharing to VIEW only on the Apps Script file. You can still give people EDIT access to the spreadsheet.
You'll need to go through a process of making the function available to your spreadsheet. This is called a "Library", but don't pay much attention to that name when creating the file.
Create a Stand Alone Apps Script
Create the function you want to share.
Get the Project Key --> FILE, PROJECT PROPERTIES
Share the Apps Script file with users you want to give access to, but only give them VIEW access to the file. NOT Edit.
Give the Project Key to the user, (In this case that's just you.) and have them add the library, or if you have access to the spreadsheet, you can probably add the key yourself. So, in your case, you will not be sharing the Project Key with anyone. You don't need to.
In the script file that is bound to the spreadsheet, click the RESOURCES menu, and the LIBRARIES menu item.
Enter the Project Key
Click SELECT button, and turn the library ON.
Make sure to set the library Identifier to a key word you'd like to use.
Go to the code editor, create a function, and type the Library Identifier.
All the functions that are available from the Library will show up
Google Documentation - Gaining Access to a Library
Keep in mind, that creating a library is nothing more than writing code in an Apps Script in a stand alone Apps Script file. If you've done that, then all you need to share is the Project Key. The user of the Library won't see a list of all the available Libraries when Resources, Libraries is opened. You need the Key.