The branch works for scripts sitting in a Team Drive by adding the parameter supportsTeamDrives to the upload request.
- Moving the script to a team drive causes https://github.com/danthareja/node-google-apps-script to fail uploading
- This is because the update command wants a parameter
supportTeamDrives
set to true, see https://developers.google.com/apis-explorer/?hl=en_US#p/drive/v3/drive.files.update and https://developers.google.com/drive/v3/reference/files/update - https://developers.google.com/drive/v3/web/enable-teamdrives : “If your application isn't affected by the behavioral differences, then you only need to include thesupportsTeamDrives=true query parameter in requests. This is an acknowledgement that the application is aware of behavioral differences of files contained in Team Drives.”
- Adding
supportsTeamDrives: true
to.then(function(auth) { var drive = google.drive({ version: 'v2', auth: auth }); var options = { fileId: id, supportsTeamDrives: true, media: { mimeType: 'application/vnd.google-apps.script+json', body: JSON.stringify({ files: files }) } }; ```
fixes the problem for a team drive
The easiest way to develop Google Apps Script projects
Using gapps, you can develop your Apps Script locally and push files to the Apps Script servers. This allows you to use any editor of your choice, version control, and other modern webdev patterns in to Apps Script development.
- node v0.12.x
npm install -g node-google-apps-script
You can do this one of two ways:
- Add Drive permissions to the default Developer Console Project that is created for each Apps Script project
- Use an independent Developer Console Project and enable the Drive API
- Access the automatically created apps script Developer Console Project by
Resources
>Developers Console Project
- Click on the blue link at the top (
project-name - api-project-##########
) to access the correct Developer Console Project - Enable the Google Drive API
- Click
APIs & auth
in the left nav and then selectAPIs
- Search for
Drive
and select the Google Drive API listing. - Click
Enable API
- Acquire Google Drive Client Secret Credentials
- In the
Credentials
section, selectAdd credentials
and chooseOAuth 2.0 client ID
- In the menu that appears, choose
Other
for theApplication type
. - Give it any name you like and click
Create
. - Finally, download your credentials using the
Download as JSON
button to the right. * Save these credentials to a location of your choosing;~/Downloads
is fine. - You may close the Developer Console window.
- Create a new Dev Console Project
- Use this link to create the project. It 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 thatauthuser
is zero-indexed. - Make sure
Create a New Project
is selected and hitContinue
. - Once the project has been created, click
Go to Credentials
. - Acquire Google Drive Client Secret Credentials
- Select
Add credentials
, choose theOAuth 2.0 client ID
type - Click
Configure Consent Screen
. - Select your email address from the dropdown and assign your add-on a Project Name. * This can always be changed later.
Save
your Consent Screen- In the menu that appears, choose
Other
for theApplication type
. - Give it any name you like and click
Create
. - Finally, download your credentials using the
Download as JSON
button to the right. * Save these credentials to a location of your choosing;~/Downloads
is fine. - 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.
This process will set up Google Drive authentication to allow uploading and importing of the Apps Script project.
- Run
gapps auth path/to/client_secret_abcd.json
- i.e.
gapps auth ~/Downloads/client_secret_1234567890-abcd.apps.googleusercontent.com.json
- Follow the directions by clicking on the link generated by the script.
- 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. This is useful if you're using ssh to develop.
This proces will create gapps.config.json
and a sub-directory where all Apps Script project files will be downloaded to. You can either use an existing project or create a new one.
- Navigate to your Apps Script project from Google Drive (must be a standalone script)
- 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'
- Navigate to a directory where your Apps Script project will live
- Run
gapps init <fileId>
within your project directory.
- For example,
gapps init abc123-xyz098
- Head to https://script.google.com and create a new blank project.
- 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.
- 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'
- Navigate to a directory where your Apps Script project will live
- Run
gapps init <fileId>
within your project directory.
- For example,
gapps init abc123-xyz098
Start scripting and enjoy total freedom of your local dev environment and source control options! Create script files with a .gs
or .js
extension and html files with a .html
extension
Run gapps upload
from within your project directory. You should then be able to reload your Apps Script project in the browser and see the changes.
Check out Matt Hessinger's blog post: Advanced development process with apps
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.
Usage: gapps init|clone <fileId> [options]
Initialize project locally. The external Apps Script project must exist.
Options:
-k, --key [key]
-s, --subdir [subdir]
-o, --overwrite
Creates gapps.config.json
, which contains information about your Apps Script project and downloads all project files into a subdirectory (default: src/
)
Usage: gapps upload|push
Upload back to Google Drive. Run from root of project directory
Upload the project to Google Drive. Sources files from ./src
or the
configured subdirectory.
Usage: gapps deployment oauth-callback-url
Get the OAuth Callback URL for a project
Returns the OAuth Callback URL required by most 3rd-party OAuth services.
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 node-google-apps-script
.
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.
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 server-side javascript code (
- 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
orOAuth2
) have been added to the Apps Script project
- verify that any required Libraries (frequently
- 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.
- Verify that
Huge thanks to Shrugs for a massive PR that bumped this project to v1.0