Access Google-apps public spreadsheet via Javascript

前端 未结 2 1435
臣服心动
臣服心动 2020-12-29 16:14

Spent a bunch of time looking at this.. It seems that what little info there was about accessing a Google-apps spreadsheet is not very well maintained.. At Google IO this ye

2条回答
  •  春和景丽
    2020-12-29 16:49

    Google provide a documented way to access google spreadsheet via JSONP that works for normal gmail.com accounts. In short:

    • Create a spreadsheet
    • Click on the dropdown next to "Share" and select "Publish as a web page"
    • Copy and paste out the key from the URL that shows (i.e. the bit after &key=)
    • Go to https://spreadsheets.google.com/feeds/cells/0AmHYWnFLY1F-dG1oTHQ5SS1uUzhvTnZTSHNzMjdDaVE/od6/public/values?alt=json-in-script&callback=myCallback replacing "0AmHYWnFLY1F-dG1oTHQ5SS1uUzhvTnZTSHNzMjdDaVE" with whatever key you cut out of the url

    To access this from within JavaScript you'll have to insert a HTML script tag into your document:

    
    

    And you'll need to implement the callback function in your webpage:

    function myCallback(spreadsheetdata) {
      // do something with spreadsheet data here
      console.log(spreadsheetdata);
    }
    

    You can simplify this with jQuery:

    var url = "https://spreadsheets.google.com/feeds/cells/0AmHYWnFLY1F-dG1oTHQ5SS1uUzhvTnZTSHNzMjdDaVE/od6/public/values?alt=json-in-script&callback=?";
    $.getJSON(url,{}, function (d) { console.log(d); });
    

提交回复
热议问题