Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Google Spreadsheet to JSON Converter</h1>
<p>It's pretty useful to be able to get data from a Google Spreasheet as JSON, however it takes a bit of messing around with the URL to do so. This little converter should help.</p>
<p>The best part is that this tool handles mutiple sheets, returning the right sheet ID for whichever is specified in the URL</p>
<div ng-app="app">
  <div ng-controller="controller">
    <h2>Enter Your Google Spreadsheet URL here</h2>
    <small><a href="https://support.google.com/docs/answer/37579?hl=en" target="_blank">Make sure it's published to the web <span class="glyphicon glyphicon-question-sign" aria-hidden="true"></span>
</a></small>
    <form ng-submit="getGoogleJSON(inputURL)">
      <input type="text" ng-model="inputURL" />
      <input class="btn btn-lg btn-success" type="submit" />
    </form>
    <span class="error">{{error}}</span>
    <div class="result" ng-show="newURL">
      <h3>Your new URL is:</h3>
      <code>{{newURL}}</code><br>
      <small><a ng-href="{{newURL}}" target="_blank">Open URL <span class="glyphicon glyphicon-new-window" aria-hidden="true"></span>
</a></small>


      <h3>Usage</h3>
      <!-- Nav tabs -->
      <ul class="nav nav-tabs" role="tablist">
        <li role="presentation"><a href="#jquery" class="active" aria-controls="jquery" role="tab" data-toggle="tab">JQuery</a></li>
        <li role="presentation"><a href="#angular" aria-controls="angular" role="tab" data-toggle="tab">AngularJS</a></li>
        <li role="presentation"><a href="#json" aria-controls="json" role="tab" data-toggle="tab">JSON Response</a></li>
      </ul>

      <!-- Tab panes -->
      <div class="tab-content">
        <div role="tabpanel" class="tab-pane" id="jquery">
          <pre><code>
        $.get('{{newURL}}', function(data){
          var entries = data.feed.entry;
          
          $(entries).each(function(){
            // Do something with each entry
          });
        });
      </code></pre>
        </div>
        <div role="tabpanel" class="tab-pane" id="angular">
          <pre><code>
        $http.get('{{newURL}}').then(function(response){
          // Assign data to scope
          $scope.data = response.data;
        });
      </code></pre>
        </div>
        <div role="tabpanel" class="tab-pane" id="json">
          <pre>{{JSONData | json}}</pre>
        </div>
      </div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                body {
  text-align:center;
}

p {
  width:600px;
  margin:20px auto;
}

h1, h2, h3 {
  color:coral;
}
input {
  display:block !important;
  margin:10px auto !important;
  &[type="text"]{
    width:90%;
    padding: 10px;
    font-size:1.3em;
    text-align:center;
  }
}

.result {
  width:80%;
  margin:auto;
  
  pre {
    margin-top:10px;
    text-align:left;
    padding:0 10px;
  }
}

.error {
  color:red;
}
              
            
!

JS

              
                (function() {
  var app = angular.module('app', []);

  app.controller('controller', function($scope, $http) {

    // Default URL
    $scope.inputURL = "https://docs.google.com/spreadsheets/d/1I3tEfgYtWKNYK9vqTmsCGfZfbHckS0-odDIgEilHiOQ/edit#gid=471572974";

    // Convert the URL function
    $scope.getGoogleJSON = function(url) {

        // Clear newURL
        $scope.newURL = '';

        // Empty array for all the original grid IDs
        $scope.oldGrids = [];
        // Empty array for all the new grid IDs
        $scope.newGrids = [];

        // Get the spreadsheet ID from the URL
        var id;
        var preString = url.substring(url.indexOf('/d/') + 3);
        if (preString.indexOf('/') > -1) {
          id = preString.substring(0, preString.indexOf('/'));
        } else {
          id = preString;
        }

        // Get the grid ID from the URL
        var grid = url.substring(url.indexOf('=') + 1);

        // Get XML data of the spreadhseet which includes the new IDs. Using cors.io to avoid cross origin errors. This is for when a spreadsheet has multiple sheets
        $http.get('https://cors.io/?https://spreadsheets.google.com/feeds/worksheets/' + id + '/public/full').then(function(response) {

            // Parse the response as xml
            var xml = $($.parseXML(response.data));
          
            // Find each entry and loop through
            $(xml).find('entry').each(function() {
                // Get each old grid ID from XML data - contained in a '<link>'
              
                var oldGridSrc = $(this).children('link[rel="http://schemas.google.com/visualization/2008#visualizationApi"]').attr('href');
              
                // Chop the ID from the end of the href
                var oldGrid = oldGridSrc.substring(oldGridSrc.indexOf('=') + 1, oldGridSrc.indexOf('&'))

                // Find the '<id>' element in the xml. This contains the new Grid ID
                var newGridSrc = $(this).children('id').text();
                // Chop the ID from the end of the string
                var newGrid = newGridSrc.substring(newGridSrc.lastIndexOf('/') + 1);

                // Push new Grid ID to array
                $scope.newGrids.push(newGrid);
                // Push old Grid ID to array
                $scope.oldGrids.push(oldGrid);

              }) // End Each

            // Get new Grid ID by cross referencing the original grid ID in the oldGrids array, return the index, and use that index to get the new Grid ID from the newGrids array. If undefined set to '1' which is the default for the first sheet
            $scope.newGridID = $scope.newGrids[$scope.oldGrids.indexOf(grid)] || '1';

            // Compose new URL
            $scope.newURL = 'https://spreadsheets.google.com/feeds/list/' + id + '/' + $scope.newGridID + '/public/values?alt=json';

            // Get JSON Response
            $http.get($scope.newURL).then(function(response) {
              $scope.JSONData = response.data;
            });

            // Show first tab
            $('.nav-tabs a:first').tab('show');

            setTimeout(function() {
              $('pre code').each(function(i, block) {
                hljs.highlightBlock(block);
              });
            }, 500);
          }, function(response){
            $scope.error = "Sorry, there has been an error. Please check your Spreadsheet URL";
        }) // End $http.get

      } // End getGoogleJSON function

  }); // End Controller

})();
              
            
!
999px

Console