Skip to content

smcsar/node-google-apps-script

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gapps (Google Apps)

The easiest way to develop Google Apps Script projects from the command line.

Requirements

Quickstart

1. Create and Save a Blank Apps Script Project

  1. Head to https://script.google.com and create a new blank project.
  2. Save the mostly empty project and give it a name.
  • Saving is important; the project is not in your Google Drive until it is saved.

2. Get Google Drive Credentials

You can do this one of two ways:

  1. Adding Google Drive permissions to the default Developer Console Project that is created for each Apps Script project
  2. Using an independent Developer Console Project and enabling the Google Drive API

2.1 Exising Apps-Script-Created Dev Console Project

  1. Access the automatically created apps script Developer Console Project by
  2. Resources > Developers Console Project
  3. Click on the blue link at the top (project-name - api-project-##########) to access the correct Developer Console Project
  4. Enable the Google Drive API
  5. Click APIs & auth in the left nav and then select APIs
  6. Search for Drive and select the Google Drive API listing.
  7. Click Enable API
  8. Acquire Google Drive Client Secret Credentials
  9. In the Credentials section of the APIs & auth group, Select Create New Client ID
  10. In the menu that appears, choose Installed application for the Application type. - You can leave the Installed Application Type field as Other.
  11. Click Create Client ID.
  12. Finally, download your credentials using the Download as JSON button below them. - Save these credentials to a location of your choosing; ~/Downloads is fine.
  13. You may close the Developer Console window.

2.2 Independent Dev Console Project

  1. Use this link to create the project.
  • That link will auto-activate the Google Drive API.
  • If you have multiple Google Accounts, append &authuser=1 to the end of the url to choose which account to login with.
    • note that authuser is 0-indexed.
  1. Make sure Create a New Project is selected and hit Continue.
  2. Once the project has been created, click Go to Credentials.
  3. Select Create New Client ID, choose the Installed Application type, and then click Configure Consent Screen.
  4. Select your email address from the dropdown and assign your add-on a Project Name.
  • This can always be changed later.
  1. Save your Consent Screen
  2. In the credentials section (which you should be redirected to after the last step), Select Create New Client ID
  3. In the menu that appears, choose Installed application for the Application type.
  • You can leave the Installed Application Type field as Other.
  1. Click Create Client ID.
  2. Finally, download your credentials using the Download as JSON button below them.
  • Save these credentials to a location of your choosing; ~/Downloads is fine.
  1. You may close the Developer Console window.
  • To return to this project later, select Resources > Developer Console Project while editing your script. Then click the link at the top of the dialog to open the Developer Console with this project selected.

3. Authenticate gapps

  1. Run gapps auth ~/Downloads/path/to/client_secret_abcd.json
  • i.e. gapps auth ~/Downloads/client_secret_1234567890-abcd.apps.googleusercontent.com.json
  • This process will set up Google Drive authentication to allow uploading and importing of the Apps Script project.
  1. Follow the directions by clicking on the link generated by the script.
  2. After you're successfully authenticated, feel free to delete the client_secret.json credentials file.

You can pass the option --no-launch-browser to generate a url that will give you a code to paste back into the console.

4. Initialize your project

  1. Run gapps project init within your project directory. This will create config.json
  2. Get your project ID from the address bar, located after /d/ and before /edit.
  • For example '//script.google.com/a/google.com/d/abc123-xyz098/edit?usp=drive_web'
  1. Run gapps add <deploymentId> <projectId>
  • For example, gapps add dev abc123-xyz098

5. Upload New Files

Awesome! Now, to upload changes, run gapps deploy <deploymentId>. You should then be able to reload your script and see changes.

Best Practices

  • Only upload to Google Drive and never edit files in the web IDE
    • This way the true state of the code is locally or in git and you'll never need to deal with issues where the states locally and remotely are not in sync.
    • You also get the benefits of structuring code within directories
      • Individual developers can have separate devevelopment Apps Script projects, and the overall solution can have shared tst, uat, prd targets with different settings.

Docs

gapps auth

  Usage: gapps auth [options] <path/to/client/secret.json>

  Authorize gapps to use the Google Drive API

  Options:

    -b, --no-launch-browser  Do not use a local webserver to capture oauth code
                              and instead require copy/paste of key returned in 
															the browser after authorization completes.
    -p, --port [port]        Port to use for webserver

Performs the authentication flow described in the quickstart above.

gapps init

  Usage: gapps project init|create [options]

  Initialize blank project. The external Apps Script project must exist.

  Options:

    -s, --subdir [subdir]
    -o, --overwrite

Creates config.json, which contains information about your apps script project.

gapps add

  Usage: gapps deployment add [options] <deploymentId> <fileId>

  Add a new Deployment Target

  Options:

    -k, --key [key]        Project Key
    -s, --subdir [subdir]  Subdirectory for deployment files

This adds entry to the deployments array in config.json containing information regarding the id, key, location, and package dependencies of your deployment.

It's popular to have dev, test, and prod deployments, each linked to different apps script projects.

This command will also create the relevant ./gapps_deployments/<deploymentId> folder where deployment-specific code can live.

gapps deploy

  Usage: gaps deployment deploy|upload [options] <deploymentId>

  Upload back to Google Drive. Run from root of project directory

Upload the project to Google Drive. Sources files from ./gapps_src, ./gapps_deployments, and also includes any packages that the deployment depends on.

gapps deployment remove

  Usage: gapps deployment remove [options] <deploymentId>

  Remove a Deployment Target

Removes the deployment info from config.json

gapps package add

  Usage: gapps deployment package add [package] [options]

  Add a package to a deployment

  Options:
    -d, --deployment <deploymentId>  Which deployment to modify (defaults to all)

This will mark the provided package as a dependency to that deployment and download the files to ./gapps_packages/<package>. They will be included when uploading to Google Drive.

gapps package update

  Usage: gapps deployment package update [package] [deploymentId]

  Update a package to the latest version

Simply re-downloads the latest version of the packages required by the deployment.

gapps oauth-callback-url

  Usage: gapps deployment oauth-callback-url [deployments]

  Get the OAuth Callback URL for a deployment, or all of them (default)

For each deployment, returns the OAuth Callback URL required by most 3rd-party OAuth services.

Development

Please submit any bugs to the Issues page. Pull Requests also welcome.

If you want to develop, clone down the repo and have at it! You can run npm link from the root directory of the repo to symlink to your local copy. You'll have to uninstall the production version first npm uninstall -g gapps.

Limitations

gapps allows you to nest files in folders, but the Apps Script platform expects a flat file structure. Because of this, no files can have the same name, even if they are in separate directories. One file will overwrite the other, making debugging difficult.

Your add-on must be developed as a standalone script and tested within Doc or Sheet. This means that it cannot use certain functions of bound scripts, namely installable triggers. While testing within a Doc, you have access to the "Special methods" mentioned in the docs, though. If you followed the quickstart above, you should be set up correctly.

Common Issues

The Google Drive API frequently returns a 400 error without a helpful error message. Common causes for this are:

  • javascript formatting errors
    • The server-side javascript code (.js or .gs) is validated upon upload.
    • If there is a syntax error, the upload will fail.
  • The project does not exist yet.
    • Verify that the project exists in your Google Drive folder.
      • If you have been added to an existing project, verify that the owner has shared the file with you with the "can edit" permission.
  • Missing server-side libraries
    • verify that any required Libraries (frequently Underscore or OAuth2) have been added to the Apps Script project
  • Authentication error
    • Verify that ~/.gapps exists and has 4 values: client_id, client_secret, redirect_uri, and refresh_token`
    • To reset authentication, 'rm ~/.gapps' and then perform the authentication steps in part #2 of the quickstart again.

Extra Resources

About

The easiest way to manager your Google Apps Script

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • JavaScript 100.0%