How to recover an old script version

 Many of us undisciplined hacks (read: not professional developers) sometimes find ourselves wondering when we will buckle down and start using Github to store our Apps script source files and versions. 

If you sometimes find yourself in the same boat: needing to restore or access an old script version, the first thing you probably do is revert back to the old Apps script editor (IDE), (filling out the form regarding the lack of version history as your reason), and then hoping the version queue goes back far enough for you to recover what you need.

Well, today my undisciplined friend, I will show you a way to recover your script files, all the way back to version 1!! Yes, no more switching back to the old code editor.

I thought about writing a Script for this, and still might, but for now, I'll show the manual process.

Step 1) Open the script editor or get the script ID of the script whose version you want to recover some other way.

        --if you already know the version number you want, skip to step 3

Step 2) Open the Script REST API (Project Versions) page so you can get a list of the versions you have to choose from (if you don't know the exact version. If you do, skip to step 3).

    2a) In the right "Try this method" sidebar, paste in the scriptID from step 1. Don't use any quotes around it. 

    2b) Click the Execute button and go through and allow the necessary permissions.

    2c) If all went well, you should see a green 200 OK label at the bottom of the right sidebar, with a JSON object in the text box below it. Review the data for the versions and dates to determine which version you want to recover.

Step 3) Go to the Projects API (still in the Script REST API).

    3a) Paste in the scriptID in the right-side sidebar, and also type in the version number.

    3b) Click the Execute button and go through and allow the necessary permissions.

    3c) If all went well, you should see a green 200 OK label at the bottom of the right sidebar, with a JSON object in the text box below it.  Copy this text and paste it in Notepad or somewhere else you can save it.

Step 4) Review the JSON object you copied. You are looking for the "source" keys. 

    4a) When you find a "source", copy the entire string of code. 

        It may look like a normal javascript function with a bunch of escaped characters, or it may have a bunch of unicode values like:  

        "\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n  \u003chead\u003e\n...    etc etc

    Copy the whole thing. 

    4b. Go to any free code converter where you can decode unicode back to normal html. I found this one that works well. (scroll down past the "Encode/Escape Unicode Text" to the Decode section. )

    4c. Paste in your code string and press convert. 

Hopefully, you will see some familiar old code:

Step 5) Copy the converted code and save it somewhere... hey, how about Github? Nah, too much work. Just open the script editor and paste in the code....   

Step 6) Ignore step 5 and really do something smart with your recovered code. :)  Take the above steps for each "file" in your project. Each file will have its own "source" that will need to be decoded.

Hopefully this has been helpful. And maybe, it might reduce the number of folks having to return to the old script editor.


Popular posts from this blog

Gmail add-ons: Conditional Homepage Content