google-sheets.html 12.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
<!-- Copyright (c) 2015 Google Inc. All rights reserved. -->

<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-ajax/iron-ajax.html">
<link rel="import" href="../google-signin/google-signin-aware.html">

<!--
Element for interacting with Google Sheets.

`<google-sheets>` pulls cell data from the Google Sheet specified by `key`.
A spreadsheet's key can be found in the URL when viewing it in google docs (e.g. `docs.google.com/spreadsheet/ccc?key=<KEY>#gid=12345`).

Optionally, pass the `tab-id` attribute to specify a particular worksheet tab in the spreadsheet. For example, the first tab would be `tab-id="1"`. If `tab` is updated at a later time, the underlying data is also updated. **API calls are cached** as to not make extraneous calls.

See [developers.google.com/google-apps/spreadsheets](https://developers.google.com/google-apps/spreadsheets) for full Spreadsheets API documentation.

#### Example

    <google-sheets key="..." tab-id="1" client-id="..."></google-sheets>

    <script>
      var sheet = document.querySelector('google-sheets');

      sheet.addEventListener('google-sheet-data', function(e) {
       // this.spreadsheets - list of the user's spreadsheets
       // this.tab - information on the tab that was fetched
       // this.rows - cell row information for the tab that was fetched
      });

      sheet.addEventListener('error', function(e) {
       // e.detail.response
      });
    </script>

<b>Example</b> - `published` is a perf optimization and hints that the spreadsheet has been published (public):

    <google-sheets key="0Anye-JMjUkZZdDBkMVluMEhZMmFGeHpYdDJJV1FBRWc" published></google-sheets>

<b>Example</b> - leaving off the `key` returns as list of the user's spreadsheets.

    <google-sheets client-id="..."></google-sheets>

<b>Example</b> - show a list of Map markers, using data-binding features inside Polymer:

    <template is="dom-bind">
      <google-sheets
        key="0Anye-JMjUkZZdDBkMVluMEhZMmFGeHpYdDJJV1FBRWc" tab-id="1" rows="{{rows}}"
        client-id="...">
      </google-sheets>
      <google-map>
        <google-map-marker latitude="{{gsx$lat.$t}}" longitude="{{gsx$lng.$t}}">
      </google-map>
    </template>

<b>Example</b> - list a user's private spreadsheets. Authenticate with google-signin button.

    <google-signin
      client-id="1054047045356-j8pgqgls9vdef3rl09hapoicumbte0bo.apps.googleusercontent.com"
      scopes="https://spreadsheets.google.com/feeds">
    </google-signin>

    <template is="dom-bind">
      <google-sheets client-id="1054047045356-j8pgqgls9vdef3rl09hapoicumbte0bo.apps.googleusercontent.com"
         key="1QMGizivw3UJ3-R9BFK7sfrXE0RL87dygk2C0RcuKoDY" tab-id="1"
         spreadsheets="{{spreadsheets}}"></google-sheets>
      <template is="dom-repeat" items="[[spreadsheets]]">
        <p>{{item.title.$t}}</p>
      </template>
    </template>

@demo
-->

<dom-module id="google-sheets">
  <template>
    <template is="dom-if" if="{{!published}}">
      <google-signin-aware client-id="{{clientId}}"
                     scopes="https://spreadsheets.google.com/feeds"
                     on-google-signin-aware-success="_onSignInSuccess"
                     on-google-signin-aware-signed-out="_onSignInFail"></google-signin-aware>
    </template>

    <iron-ajax id="publicajax" params='{"alt": "json"}' handle-as="json"
               on-response="_onCellRows"></iron-ajax>
    <iron-ajax id="listsheetsajax" params='{"alt": "json"}' handle-as="json"
               on-response="_onSpreadsheetList"></iron-ajax>
    <iron-ajax id="worksheetajax" params='{"alt": "json"}' handle-as="json"
               on-response="_onWorksheet"></iron-ajax>
    <iron-ajax id="cellrowsajax" params='{"alt": "json"}' handle-as="json"
               on-response="_onCellRows"></iron-ajax>

  </template>
</dom-module>

<script>
(function() {
  var SCOPE_ = 'https://spreadsheets.google.com/feeds';

  // Minimal cache for worksheet row data. Shared across instances so subsequent
  // accesses are fast and API calls only happen once.
  var rowDataCache_ = {};

  function generateCacheKey_() {
    return this._worksheetId + '_'+ this.tabId;
  }

  function getLink_(rel, links) {
    for (var i = 0, link; link = links[i]; ++i) {
      if (link.rel === rel) {
        return link;
      }
    }
    return null;
  }

  // Conversion of Worksheet Ids to GIDs and vice versa
  // od4 > 2
  function wid_to_gid_(wid) {
    return parseInt(String(wid), 36) ^ 31578;
  }
  // 2 > 0d4
  function gid_to_wid_(gid) {
    // (gid xor 31578) encoded in base 36
    return parseInt((gid ^ 31578)).toString(36);
  }

  window.GoogleSheets = Polymer({

    is: 'google-sheets',

    /**
     * Fired when the spreadsheet's cell information is available.
     *
     * @event google-sheet-data
     * @param {Object} detail
     * @param {Object} detail.data The data returned by the Spreadsheet API.
     * @param {string} detail.type The type of data that was fetched.
     *     One of 'spreadsheets', 'tab', 'rows' * to correspond to the feed type.
     */

    hostAttributes: {
      hidden: true
    },

    properties: {
      /**
       * A Google Developers client ID. Obtain from [console.developers.google.com](https://console.developers.google.com). Required for accessing a private spreadsheet. Optional if accessing a public spreadsheet.
       */
      clientId: {
        type: String,
        value: '',
        observer: '_configUpdate'
      },

      /**
       * The key of the spreadsheet. This can be found in the URL when viewing
       * the document is Google Docs (e.g. `docs.google.com/spreadsheet/ccc?key=<KEY>`).
       *
       * Leaving off this attribute still returns a list of the users spreadsheets in the `spreadsheets` property.
       */
      key: {
        type: String,
        value: '',
        observer: '_keyChanged'
      },

      /**
       * Tab within a spreadsheet. For example, the first tab in a spreadsheet
       * would be `tab-id="1"`.
       */
      tabId: {
        type: Number,
        value: 1,
        observer: '_configUpdate'
      },

      /**
       * A hint that the spreadsheet is published publicly in Google Docs. Used as a performance optimization.
       * Make sure the sheet is also publicly viewable by anyone in the Share settings.
       *
       * @attribute published
       * @type boolean
       * @default false
       */
      published: {
        type: Boolean,
        value: false,
        observer: '_configUpdate'
      },

      /**
       * The fetched sheet corresponding to the `key` attribute.
       */
      sheet: {
        type: Object,
        value: function() { return {}; },
        readOnly: true,
        notify: true,
        observer: '_sheetChanged'
      },

      /**
       * Meta data about the particular tab that was retrieved for the spreadsheet.
       */
      tab: {
        type: Object,
        value: function() { return {}; },
        readOnly: true,
        notify: true,
        observer: '_tabChanged'
      },

      /**
       * If a spreadsheet `key` is specified, returns a list of cell row data.
       */
      rows: {
        type: Array,
        value: function() { return []; },
        readOnly: true,
        notify: true
      },

      /**
       * List of the user's spreadsheets. Shared across instances.
       */
      spreadsheets: {
        type: Array,
        readOnly: true,
        notify: true,
        value: function() { return []; }
      },

      /**
       * The URL to open this spreadsheet in Google Sheets.
       */
      openInGoogleDocsUrl: {
        type: String,
        computed: '_computeGoogleDocsUrl(key)',
        notify: true
      }
    },

    _worksheetId: null,

    _computeGoogleDocsUrl: function(key) {
      var url = 'https://docs.google.com/spreadsheet/';
      if (key) {
        url += 'ccc?key=' + key;
      }
      return url;
    },

    _configUpdate: function(key, published, tabId, clientId) {
      this._tabIdChanged();
    },

    _keyChanged: function(newValue, oldValue) {
      // TODO(ericbidelman): need to better handle updates to the key attribute.
      // Below doesn't account for private feeds.
      if (this.published) {
        var url = SCOPE_ + '/list/' + this.key + '/' +
                  this.tabId + '/public/values';
        this.$.publicajax.url = url;
        this.$.publicajax.generateRequest();
      }
    },

    _tabIdChanged: function(newValue, oldValue) {
      if (this._worksheetId) {
        this._getCellRows();
      } else if (this.published) {
        this._keyChanged();
      }
    },

    _sheetChanged: function(newValue, oldValue) {
      if (!this.sheet.title) {
        return;
      }

      // Make metadata easily accessible on sheet object.
      var authors = this.sheet.author && this.sheet.author.map(function(a) {
        return {email: a.email.$t, name: a.name.$t};
      });

      this.set('sheet.title', this.sheet.title.$t);
      this.set('sheet.updated', new Date(this.sheet.updated.$t));
      this.set('sheet.authors', authors);

      this._worksheetId = this.sheet.id.$t.split('/').slice(-1)[0];
      this._getWorksheet();
    },

    _tabChanged: function(newValue, oldValue) {
      if (!this.tab.title) {
        return;
      }

      var authors = this.tab.authors = this.tab.author && this.tab.author.map(function(a) {
        return {email: a.email.$t, name: a.name.$t};
      });

      this.set('tab.title', this.tab.title.$t);
      this.set('tab.updated', new Date(this.tab.updated.$t));
      this.set('tab.authors', authors);

      this.fire('google-sheet-data', {
        type: 'tab',
        data: this.tab
      });
    },

    _onSignInSuccess: function(e, detail) {
      var oauthToken = gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse();

      var headers = {
        'Authorization': 'Bearer ' + oauthToken.access_token
      };

      this.$.listsheetsajax.headers = headers;
      this.$.worksheetajax.headers = headers;
      this.$.cellrowsajax.headers = headers;

      // TODO(ericbidelman): don't make this call if this.spreadsheets is
      // already populated from another instance.
      this._listSpreadsheets();
    },

    _onSignInFail: function(e, detail) {
      // TODO(ericbidelman): handle this in some way.
      console.log(e, e.type);
    },

    _listSpreadsheets: function() {
      var url = SCOPE_ + '/spreadsheets/private/full';
      this.$.listsheetsajax.url = url;
      this.$.listsheetsajax.generateRequest();
    },

    _onSpreadsheetList: function(e) {
      e.stopPropagation();

      var feed = e.target.lastResponse.feed;

      this._setSpreadsheets(feed.entry);

      this.fire('google-sheet-data', {
        type: 'spreadsheets',
        data: this.spreadsheets
      });

      // Fetch worksheet feed if key was given and worksheet exists.
      if (this.key) {
        for (var i = 0, entry; entry = feed.entry[i]; ++i) {
          var altLink = getLink_('alternate', entry.link);
          if (altLink && altLink.href.indexOf(this.key) != -1) {
            this._setSheet(entry);
            break;
          }
        }
      }
    },

    _getWorksheet: function() {
      if (!this._worksheetId) {
        throw new Error('workesheetId was not given.');
      }

      var url = SCOPE_ + '/worksheets/' + this._worksheetId +
                '/private/full/' + this.tabId;
      this.$.worksheetajax.url = url;
      this.$.worksheetajax.generateRequest();
    },

    _onWorksheet: function(e) {
      e.stopPropagation();

      // this.tab = e.target.lastResponse.entry;
      this._setTab(e.target.lastResponse.entry);
      this._getCellRows();
    },

    _getCellRows: function() {
      // Use cached data if available.
      var key = generateCacheKey_.call(this);
      if (key in rowDataCache_) {
        this._onCellRows(null, null, rowDataCache_[key]);

        return;
      }

      var url = SCOPE_ + '/list/' +
                this._worksheetId + '/' + this.tabId +
                '/private/full';
      this.$.cellrowsajax.url = url;
      this.$.cellrowsajax.generateRequest();
    },

    _onCellRows: function(e) {
      e.stopPropagation();

      var feed = e.target.lastResponse.feed;

      // Cache data if key doesn't exist.
      var key = generateCacheKey_.call(this);
      if (!(key in rowDataCache_)) {
        rowDataCache_[key] = {response: {feed: feed}};
      }

      // this.rows = feed.entry;
      this._setRows(feed.entry);
      var authors = feed.author && feed.author.map(function(a) {
        return {email: a.email.$t, name: a.name.$t};
      });
      this.set('rows.authors', authors);

      if (this.published) {
        // this.tab = feed;
        this._setTab(feed);
      }

      this.fire('google-sheet-data', {
        type: 'rows',
        data: this.rows
      });
    }

  });

})();
</script>