UNPKG

gsweet

Version:

Help with writing scripts to run against gSuite

264 lines (146 loc) 10.7 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Home</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Home</h1> <h3> </h3> <section> <article><h1>gsweet</h1><h2>Summary</h2><p>A project for gathering the core methods and tools for making it easier to write scripts across all the products in the gSuite.</p> <h2>Installation</h2><p><code>npm i gsweet --save</code></p> <h2>Basic Use</h2><p>Once you have your <a href="#Authentication">authentication JSON file</a> set up in a file named <code>.env.json</code> you require the package and call the auth() function</p> <pre class="prettyprint source lang-javascript"><code>const GSweet = require(&quot;gsweet&quot;); const gsweet = new Gsweet(); const { driveOps } = gsweet; const { sheetOps } = gsweet;</code></pre><p>In the above example the lack of a parameter to the new GSweet constructor is telling the package to look for <code>.env.json</code> at the root of the project. If your credentials file lives somewhere else you can just pass in a relative or absolute path like this</p> <pre class="prettyprint source lang-javascript"><code>const gsweet = new Gsweet({pathOrVarName:&quot;/Users/your-user-name/dev/ENV_VARS/gsweet.env.json&quot;, useExistingEnvVar:false); const { driveOps } = gsweet; const { sheetOps } = gsweet;</code></pre><p>The second parameter supports using an environment variable that already contains the proper credential string. For example, if you're using Heroku and you have a config var named &quot;GSWEET&quot; the call would be</p> <pre class="prettyprint source lang-javascript"><code>const gsweet = new GSweet({ pathOrVarName: &quot;GSWEET&quot;, useExistingEnvVar: true });</code></pre><p>The code that runs loads the JSON file and parses the top level objects into environment variables needed by this package.</p> <p>A full example usage of the package might look like this</p> <pre class="prettyprint source lang-javascript"><code>cconst gsweet = new Gsweet({pathOrVarName:&quot;/Users/your-user-name/dev/ENV_VARS/gsweet.env.json&quot;, useExistingEnvVar:false); const { driveOps, sheetOps } = gsweet; const main = async () => { // Drive Examples const TEST_FILE = &quot;&lt;name of sheet in your drive>&quot;; const result = await driveOps.getFiles({ withName: TEST_FILE, exactMatch: true }); console.log(result); // Sheet Examples const sheetRange = { id: &quot;&lt;google id of a sheet in your drive>&quot;, range: &quot;Sheet1!A1&quot;, data: [[&quot;Test1&quot;], [&quot;Test2&quot;]] }; const result2 = await sheetOps.setRangeData(sheetRange); console.log(result2.config.data.values); // just showing the values passed in console.log(&quot;Num Cells Updated:&quot;, result2.data.updatedCells); }; main();</code></pre><h2>Authentication</h2><p>If you clone this repo it will not contain the needed authorization pieces. You will need to create a JSON file with the following structure:</p> <pre class="prettyprint source lang-JSON"><code>&quot;gsweet&quot;:{ &quot;client_secrets&quot;: { }, &quot;drive_credentials&quot;: { }, &quot;sheet_credentials&quot;: { } } }</code></pre><p>If you want to store all of this in an <code>.env</code> file you will need to turn it into a properly escaped string. A partial example looks like this. Note that all but the starting and ending quote marks need to be escaped. JSON wants double quotes so you can't substitute single quotes.</p> <pre class="prettyprint source lang-JSON"><code>&quot;gsweet&quot;:&quot;{\&quot;client_secrets\&quot;:{\&quot;installed\&quot;:{\&quot;client_id\&quot;...&quot;</code></pre><p>You will need to fill in those objects with the expected json that Google requires. Google has a <a href="https://developers.google.com/sheets/api/quickstart/nodejs">quick-start</a> on how to create all of these credentials. You can see a <a href="#Full-.json.env-example">full example</a> of the JSOn file in the Reference section of this Readme.</p> <h2>Testing</h2><p>This package contains both unit tests and integration tests. The integration tests are fragile since they require access to specific files and folders in google drive. The constants for these files are stored in the <code>test-data/integration.json</code>. Modify that document to contain the names and ids for your files. The expected structure of the test data is test-folder</p> <pre class="prettyprint source"><code>---node-test-subfolder |---doc-in-subfolder |---sheet-in-subfolder node-test-sheet node-test-doc</code></pre><p>The top of the integration test files also uses <code>create-env</code> to load credentials. You will need to change that path to point to your credentials json file.</p> <p>This project is set up such that unit tests will be written with a <code>test.js</code> suffix and integration tests will end with <code>testi.js</code>. You can run unit tests with <code>npm test</code> -- the unit tests<br><code>npm run test:int</code> -- the integration tests<br><code>npm run test:all</code> -- both unit and integration tests</p> <h2>Reference on Using the Google Apis</h2><p>This is information for anyone contributing to (rather than using) this project.</p> <p><a href="https://www.npmjs.com/package/googleapis">GoogleApis npm package</a></p> <p>Check out some <a href="https://github.com/googleapis/google-api-nodejs-client/tree/master/samples/drive">Drive Samples</a>.</p> <p>The <a href="https://developers.google.com/drive/api/v3/folder">Drive API</a></p> <h2>Additional Documentation</h2><p>This project uses JSDoc comments. If you want to generate the HTML documentation in an <code>/out</code> folder at the root of the project use:<br><code>npm run doc</code></p> <h2>Reference</h2><h3>Full .json.env example</h3><pre class="prettyprint source lang-JSON"><code>{ &quot;gsweet&quot;:{ &quot;client_secrets&quot;: { &quot;installed&quot;: { &quot;client_id&quot;: &quot;your id goes here&quot;, &quot;project_id&quot;: &quot;your project id&quot;, &quot;auth_uri&quot;: &quot;https://accounts.google.com/o/oauth2/auth&quot;, &quot;token_uri&quot;: &quot;https://accounts.google.com/o/oauth2/token&quot;, &quot;auth_provider_x509_cert_url&quot;: &quot;https://www.googleapis.com/oauth2/v1/certs&quot;, &quot;client_secret&quot;: &quot;your client secret&quot;, &quot;redirect_uris&quot;: [ &quot;urn:ietf:wg:oauth:2.0:oob&quot;, &quot;http://localhost&quot; ] } }, &quot;drive_credentials&quot;: { &quot;access_token&quot;: &quot;a really long token&quot;, &quot;refresh_token&quot;: &quot;a shorter token&quot;, &quot;token_type&quot;: &quot;Bearer&quot;, &quot;expiry_date&quot;: 123 }, &quot;sheet_credentials&quot;: { &quot;access_token&quot;: &quot;your token&quot;, &quot;refresh_token&quot;: &quot;the refresh toeken&quot;, &quot;scope&quot;: &quot;https://www.googleapis.com/auth/spreadsheets&quot;, &quot;token_type&quot;: &quot;Bearer&quot;, &quot;expiry_date&quot;: 123 } } }</code></pre><h2>Documentation on GitHub</h2><p>See the <a href="https://htmlpreview.github.io/?https://github.com/pluralsight/gsweet/blob/master/documentation/index.html">GitHub documentation</a> for the list of available modules, functions, function signatures, and usage examples.</p></article> </section> <section> <header> <h2> <span class="ancestors"><a href="module-drive_driveOps.html">drive/driveOps</a>~</span>drive/driveOps.js</h2> </header> <article> <div class="container-overview"> <div class="description"><p>Handles talking to the Google Spreadsheet API </p> <h2>Links to Google Drive documentation</h2><p><a href="https://developers.google.com/drive/api/v3/mime-types">mime-types</a><br><a href="https://developers.google.com/drive/api/v3/reference/files">all file meta data</a><br><a href="https://developers.google.com/drive/api/v3/search-parameters">search parameters</a><br>a few meta types we aren't using that might be interesting are <code>starred, shared, description</code></p> <p>NOTE: Before using init() <strong>MUST</strong> be called and a driveService passed in.</p></div> <dl class="details"> <dt class="tag-author">Author:</dt> <dd class="tag-author"> <ul> <li><a href="mailto:tod-gentille@pluralsight.com">Tod Gentille</a></li> </ul> </dd> <dt class="tag-license">License:</dt> <dd class="tag-license"><ul class="dummy"><li>GPL-3.0-or-later [Full Text](https://spdx.org/licenses/GPL-3.0-or-later.html)</li></ul></dd> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="drive_driveOps.js.html">drive/driveOps.js</a>, <a href="drive_driveOps.js.html#line3">line 3</a> </li></ul></dd> </dl> </div> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="Drive_driveOps.module_test.html">test</a></li><li><a href="module-drive_driveOps.html">drive/driveOps</a></li><li><a href="module-Drive_driveOps%2520Integration%2520Tests.html">Drive/driveOps Integration Tests</a></li><li><a href="module-drive_driveService.html">drive/driveService</a></li><li><a href="module-Sheet_sheetOps%2520Integration%2520Tests.html">Sheet/sheetOps Integration Tests</a></li><li><a href="sheets_sheetOps.module_js.html">sheets/sheetOps.js</a></li><li><a href="sheets_sheetService.module_js.html">sheets/sheetService.js</a></li><li><a href="-_sheetOps.module_test.html">test</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Sat May 11 2019 23:52:27 GMT-0700 (Pacific Daylight Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>