How do I download previous saved revisions of a Google Apps Script? - google-apps-script

I am still attempting to recover from somehow 'loosing' a Google Apps Script when I created a copy of the script to fork the code to another project... My thought was to attempt to download older revisions of the Google Apps Script via the drive API, but this page indicates that perhaps it is not possible.
Actions such as versioning, publishing or executing the script are not available through the API.
So I attempted it anyway and alas this error occurs...
<HttpError 400 when requesting
https://www.googleapis.com/drive/v2/files/..snipped../revisions?alt=json
returned "File does not support revisions">
So is there no mechanism to download previous versions or "Revisions" of Google Apps Script? What I would love is to get access to 'Revision 1' shown here within the script.google.com interface:

This is a bit manual but:
Create another script project
Include your original script as a library in your new script and select v1
In the new script add some code that uses your "library"
Use the script editor debugger to step into the library and you should see the code from the correct version
For example if your library is called something like "MyOldCode", include some code like this in your new script:
function getOldCode() {
var a = MyOldCode.anExampleFunction()
}
Put a breakpoint below the "var a" line, run getOldCode() with the debugger and then step into anExampleFunction(). This will open up one file of your old code allowing you to copy and paste it elsewhere.
You can then piece together v1.
Edit 2019-04-11: There is also Romain's function for accessing old versions.
Edit 2022-05-05: With V8 and the new editor I can no longer find a way to step into a library. Rhino gives a server error, and V8 ignores the "step in".

FYI, the Apps Script API can get project files from a previous version number:
https://developers.google.com/apps-script/api/reference/rest/v1/projects/getContent
If you'd like to see this in the Apps Script CLI, clasp, like clasp pull 3, I'm sure I could add this feature.
https://github.com/google/clasp/
Just file a new issue.

Grant is the right answer. Clasp is the way. I just wanted to update the command for the current state of clasp which is
clasp pull --versionNumber 12

Related

How to create Google scripts on desktop using WebStorm?

Scripting on google's IDE is a big pain, it's very laggy and it just sucks.
How can I make the google script editor work on my PC?
I tried downloading this Clasp
Also I added a library from google to javascript.
but when I run it it gives me this error:
var ss = SpreadsheetApp.getActiveSpreadsheet();
^
ReferenceError: SpreadsheetApp is not defined
How can I fix this?
What I'm basically trying to do with this script is to send an email based on a bunch of conditions on a google sheets.
Solution
In order to be able to run an Apps Script project remotely from your local machine, you need to use the clasp command run as you will be required to also use some kind of authorization (you will basically need to connect your local project to your remote Apps Script project and verify it is you the one trying to run it).
Here is a complete guide on how to use this command but just to summarise a bit you basically will need to set up a Project ID and create an OAuth Client ID to then be able to login using the credentials obtained from it clasp login --creds creds.json1.
This is an example of running a function using the command run:
clasp run 'functionName'
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)
Adding the google-apps-script library to webstorm might also help you out.
Follow the instructions here, but search for "google-apps-script" in the download library modal. Instead of "chrome" which is what this article is about.
How do I use WebStorm for Chrome Extension Development?
You'll then be able to create *.gs files. Open them as javascript.

App script web change version for each deploy

I created a web app for my google sheet script.
I deployed it with version 1 and and it works
Now I want to make on going changes and deploy my changes,
Do I need to increase the version for each change I deploy? or can I update on current version?
Also, is there a way to get the current version programmaticaly?
For your situation, I would like to introduce because I had a workaround that I also use.
For question 1 :
In generally, when the script of Web Apps was updated, the project version is required to be increased and Web Apps is redeployed with the new version.
But if you want to deploy Web Apps without increasing the version, as a workaround, you can achieve it by using a library. The flow of for this workaround is as follows.
Create 2 projects.
One is the project that Web Apps is deployed.
It supposes that this is the project that you are using now.
Another project is used as a library. Please create this as a standalone project.
Please create a version.
On script editor
File -> Manage versions...
After input description, please click "Save new version".
Please copy this script ID. This is used for installing the library.
For the project that Web Apps is deployed.
Please install the library project as a library.
On script editor
Resources -> Libraries...
For "Add a library", please input the file ID of library project.
Turn on "Development mode".
Set the version to the saved version.
In this sample, please set "Identifier" to "lib".
Please copy and paste the following function. In this project, the script is only this. And please save the project after copy and paste it.
function doGet(e) {return lib.main(e)}
Please deploy Web Apps as a new version.
After this setting, you are not required to modify this project.
For the project which is used as a library.
Please copy and paste the following function.
function main(e) {return yourfunction(e)}
yourfunction(e) is the function in the project that you are using.
In this workaround, when you want to modify the script, please modify the library project. By this, when the library is updated, the project which deployed Web Apps is automatically updated because "Development mode" is turned on when the library is installed. This update is also reflected to Web Apps. So the script of Web Apps can be updated without increasing the version.
As a sample, the scripts for both project after making the settings described above are as follows:
For the project that Web Apps is deployed.
function doGet(e) {
return lib.main(e);
}
For the project which is used as a library.
function main(e) {
return yourfunction(e);
}
function yourfunction(e) {
do something: The script which is run when users accessed to Web Apps.
return something
}
For question 2 :
In order to retrieve the current version of the project, you can use Apps Script API. This is also answered by tehhowch. In a recent update, users got to be able to retrieve and create the project versions. By using this, you can achieve this. The required scope is https://www.googleapis.com/auth/script.projects or https://www.googleapis.com/auth/script.projects.readonly.
The endpoint for retrieving the version list is as follows.
GET https://script.googleapis.com/v1/projects/### scriptId ###/versions
If you want to create the new version using script, you can use the following endpont.
POST https://script.googleapis.com/v1/projects/### scriptId ###/versions
Request body is
{
"versionNumber": 3,
"description": "sample description"
}
References :
Web Apps
Libraries
Google Apps Script API
Method: projects.versions.list
If this was not useful for you, I'm sorry.
When creating a web app for apps script, two links are given:
The one ending exec will only load the specified published version of the code.
The one ending dev will load the latest saved code (allowing for testing of code prior to making a new version).
Programmatic control of the deployed version is available via the Apps Script API, and to a Dev environment via clasp.
Getting versions
Creating versions
You need to increase the version for each change, unless you use the "dev" version of the app when it will always display the latest version. This will only be visible when you are logged into your account though.
It's much easier than everyone makes it sound
Go to Deploy:
Click "Manage deployments"
Under Configuration click the edit pencil
Under "Version" click "New Version"
Click "Deploy" it will auto increment the version for you

Does Google Apps Script support external IDEs?

I am using Google Apps Script and was wondering if I could use any sort of editor outside of the one Google provides. (I purchased Sublime Text and would like to use that.)
The one Google provides is disgusting, with tiny text even though I have a giant screen and syntax colors that I find a bit difficult to distinguish on a white background with small text, and I don't have the beauties of Sublime like tons of custom keyboard shortcuts, all of my packages, etc.
As of today (May 2018), every scripts that you add on Google App Scripts (including scripts on Google Drive like spreadsheets) will get accessible through https://script.google.com and will have a specific id that you can get through the url by opening it.
By using clasp which handle a project (like git), you can do:
clasp clone {id}
to have your script in a local folder.
Then, after editing your file with your favorite text editor, upload it back with :
clasp push
Google has dropped support for Apps Script inside Eclipse but you can set up a local development environment inside VS Code using the Apps Script Starter kit and CLASP.
Here's a video tutorial to help you get started.
The eclipse plug-in is migrated now. You should install many
files. lastly it couldn't import my google script files.
nod-google-apps-script is depercated.
gdrive can't handle google scripts beyond exporting the project in single json file. issue issue
sublime-editor-plug-in has n't worked for me (I read the whole documents & issues)
The only working solution for me (ubuntu xenial) is clasp.
According to doc it can :
Develop Locally: clasp allows you to develop your Apps Script projects locally. That means you can check-in your code into source control, collaborate with other developers, and use your favorite tools to develop Apps Script.
Manage Deployment Versions: Create, update, and view your multiple deployments of your project.
Structure Code: clasp automatically converts your flat project on script.google.com into folders. For example:
On script.google.com:
tests/slides.gs
tests/sheets.gs
locally:
tests/
slides.js
sheets.js
N.B:. The fature of 'Manage Deployment Versions' isn't supported by eclipse plug in.
It give the following commands:
clasp login [--no-localhost]
clasp logout
clasp create [scriptTitle] [scriptParentId]
clasp clone <scriptId>
clasp pull
clasp push
clasp open
clasp deployments
clasp deploy [version] [description]
clasp redeploy <deploymentId> <version> <description>
clasp version [description]
clasp versions
By using it; you can use your favorite script editor; then push the changes.
Edit
GAS Editor Autocomplete
Thanks to #tehhowch comment.
Auto complete of google objects is almost exclusive for online GAS editor, If you use any external editor , you will only get auto complete for the functions & variables of your own script (I am not sure about eclipse).
But online editor provides autocomplete which reveals the global objects as well as methods and enums that are valid in the script's current context.
To show autocomplete suggestions, select the menu item Edit > Content assist or press Ctrl+Space. Autocomplete suggestions also appear automatically whenever you type a period after a global object, enum, or method call that returns an Apps Script class. For example:
If you click on a blank line in the script editor and activate autocomplete, you will see a list of the global objects.
If you type the full name of a global object or select one from autocomplete, then type . (a period), you will see all methods and enums for that class.
If you type a few characters and activate autocomplete, you will see all valid suggestions that begin with those characters.
You can use node-google-apps-script to edit scripts locally (described in this blog post from the google apps developer blog on 2015-12-17). This way you can use a more comfortable editor, version control and build scripts.
Caveats:
Like the eclipse plugin, node-google-apps-script requires that your add-on is a standalone script.
Debugging still requires google's script editor. With some additional setup you could run functions via the Execution API
It would be awesome if there was a Sublime Text plugin for code completion of google library methods, but I haven't found one (I don't think their default browser editor even offers this, though)
This is an answer for basic/beginner users of Intellij that are looking for code completion when writing Google App Scripts apps. Most of the answers above were helpful for getting me going, especially with Clasp. But none of them actually got Intellij to do the code complete. But this does:
Select File, Settings, Languages & Frameworks, JavaScript, Libraries.
Then on the right select Download, the scroll down to 'google-app-scripts'. (Lots of scrolling) Download and install and you're done. Almost so easy that it really shouldn't have been so hard to find.
Another option to contemplate: using "AppsScript Color" Google Chrome plugin and GAS editor shortcuts.
Here you have a list of most useful shortcuts:
https://opensourceseo.org/useful-tips-for-using-the-google-apps-script-editor/
You can just install a Google Drive command line client (e.g. https://github.com/prasmussen/gdrive) and edit .gs scripts directly.
There's also a way to edit Google Apps Scripts in Eclipse, which may be interesting. https://developers.google.com/eclipse/docs/apps_script

getting Current script version using Google Apps script

I am aware of the versioning that can be done using "Manage Version". Is there a way available to get/fetch the current version of the Spreadsheet script in/using Apps script .
If you mean "Can we programmatically retrieve the script version number?", the answer is no. There is an outstanding request for this, in Issue 1369, so visit the issue tracker and star it to indicate your own interest.
Perhaps you mean "Can we programmatically retrieve the most recent source code?"
You can Import and Export Stand-alone script files through the Drive SDK. In Apps Script, this can be accomplished using UrlFetch Services.
There is no way at this time to retrieve the source of container-bound scripts, except through the Script Editor. (See Limitations.)
Reviving a dead one here but others might find this as I have still searching for a solution.
Resolution of Issue 1369 as linked in the other answer is:
Jan 22, 2018 12:09PM
Marked as fixed, reassigned to ti...#google.com.
As Federico states, with the new Apps Script API you can get script versions.
Also, with the CLI (github.com/google/clasp), you can get version info with:
clasp versions
this means we can fetch a list of versions but I still have not found a way of programmatically getting the version number and description from within a script.

Google Apps Script - Tweet Approver

I've been following the guide found here: https://developers.google.com/apps-script/articles/twitter_tutorial
About creating a twitter app.
I've been looking at the part below:
To get started, copy this spreadsheet containing the script for this tutorial. Once you've got your own copy of the spreadsheet, open it and you will notice the spreadsheet will be empty; that is intentional, as we won't be using any of the spreadsheet's cells in this tutorial.The first thing you need to do is publish your script as a service. To do this, open the script editor by clicking Tools > Script Editor... In the new window, click Publish > Publish as Service...
This doesn't actually work at all.
If I open the spreadsheet and click Tools > Script Editor.. I'm taken to the script wizard. It then give me the option of creating a new script. The script can only be published to gallery, or as a web app. Where is the publish as service option?
Thanks
I checked on the tutorial page and it seems indeed that the template spreadsheet has not script included...
But the full code is still published at the end of the same page so you could copy/paste the full code in the script editor (choose new code from the wizard and delete everything in there ) and start from there.
About the "publish as service" this is the old name of what is now known as deploy as webapp, the procedure is slightly different and is described here, you will need to first save a version (manage version in the file menu) and you will get 2 urls, one for development and one for use, both will run from your browser starting with the doGet() function.
The remaining part of the tutorial about initialisation and authorization from the spreadsheet menu is unchanged, just follow the tutorial instructions.
Hoping it will help you,