Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-api

The download /back-up of Google scripts (.gs files) bounded to Google Spreadsheets


I have google spreadsheets having Google scripts (.gs files) bounded to it. When I download them from Google Drive, only csv or xlsx get downloaded. The associated scripts files (.gs files) are not downloaded. However the standalone scripts can be downloaded from Google Drive. How can the back up of Google scripts (.gs files) bounded to Google Spreadsheets can be taken? How can they be downloaded?


Solution

  • You can back up file-bound scripts locally on your machine using the command line tools for Google Apps Script (clasp).

    The clasp is an open-source tool, provided by Google, to interact with Google App script projects. It may be used, for instance, to develop websites or Google Docs plugins locally instead of using the web environment. You can use clasp to download or to deploy projects to the Google App Scritps. You may check a tutorial in a the Google codelab for Clasp.


    Making a copy of a Google Apps Script project

    1. To use clasp, you must install first Node-js. You can download an installer for Windows or Linux from the official website

    2. One installed Node.js, you must install clasp. Using a command line terminal, you may execute npm to perform the installation

      npm install -g @google/clasp
      
    3. After installing clasp, you can download the project files using the script id. You can go to Google Script Editor and copy the Script Id under File -> Project properties.

    GAS project properties page

    1. On your computer, create a folder for the project and navigate into the newly created folder. You can use a File Explorer to create the folder. Using Windows, Linux or Mac, you can create the folder using the command line terminal and the following commands:

      mkdir gas-project 
      cd gas-project
      
    2. Finally, to make the copy, you must execute the following command, where 'scriptid' is the id you copied from Project properties.

      clasp clone <scriptid>
      

    Once you have a local copy of the files, you can run clasp pull to update the code locally or clasp push to send any locally-made changes to the server.