How to setup service to pass google sheet IDs? AngularJS

后端 未结 3 927
无人及你
无人及你 2021-01-18 04:19

I am building a small widget using angular that takes Google sheet Ids given by user and publishes the output in nice json format. The problem is that my code does nothing.

3条回答
  •  独厮守ぢ
    2021-01-18 04:24

    Working example here

    Example path:

    https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0AmYzu_s7QHsmdDNZUzRlYldnWTZCLXdrMXlYQzVxSFE&output=html



    Best way is with Promise

    Use q framework ($q service)

    Remove the logic from the controller to the service

    App

    angular.module('myApp', []);
    

    Service

    angular.module("myApp")
      .service("chartService", function($q) {
    
        return {
          getSpreadsheet: function init(path) {
            var deferred = $q.defer();
            //if no path use the config key
            Tabletop.init({
              key: path,
              callback: deferred.resolve,
              simpleSheet: true
            });
            return deferred.promise;
    
          }
        }
      });
    

    Controller

            angular.module('myApp')
      .controller('piechartCtrl', ['$scope', 'chartService', function($scope, chartService) {
        $scope.getSheet = function() {
          chartService.getSpreadsheet($scope.googleSheetPath).then(function(data) {
            $scope.data = data;
          })
        }
      }]);
    

    index.html

    
    
    
    
      
      
      
      
    
    
    
    
    
    {{data}}

提交回复
热议问题