简体   繁体   中英

How to query from an online database in Google Apps Scripts?

I am trying to use google apps scripts and google sheets to basically automatically update a google sheet with statistical information every time an NHL team plays. How do I query nhl.com online database within the Google Apps Scripts environment? For example -- a stat that I am interested in is total points for a certain team. How would I get that integer value from nhl.com in Google Apps Scripts?

You need to use UrlFetchApp that is the GAS's service to send http requests and accept responses.

Your code might look something like:

 function getMatchResults() {
    var options = {
        'method': 'get',
        //and any other additional parameters you might need
    };
    var url = 'http://www.nhl.com/stats/team?aggregate=0&gameType=2&report=teamsummary&teamId=24&reportType=season&seasonFrom=20172018&seasonTo=20172018&filter=gamesPlayed,gte,1&sort=points%22'
    var results = UrlFetchApp.fetch(url, options); 
    Logger.log(results);
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    //further on goes your code to actually save the results into your sheet
}

If you need this function to run periodically, you'll need to set up a time driven trigger which is described here .

UPDATE: After your comment, there goes the second part. At first, I assumed that nhl.com has some kind of API that would return responses in JSON format but turns it does not. This is why you were getting the error at JSON.parse — the returned result is not a JSON object but an HTML web page.

So your task gets trickier: you try scraping the page and get the results out of html or use third party APIs like MySportFeeds . You can also see this Quora discussion .

If you use MySportFeeds, which seems to be free for personal use, the above function will probably look something like:

function getMatchResults() {
  var headers = {
    'Authorization': 'username:password',
  };
  var options = {
    'method': 'get',
    'headers': headers,
    //and any other additional parameters you might need
  };
  var url = 'https://www.mysportsfeeds.com/api/feed/pull/nfl/2016-2017-regular/scoreboard.json?fordate=20161211'
  var results = UrlFetchApp.fetch(url, options); 
  Logger.log(results);
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  //further on goes your code to actually save the results into your sheet
}

UPDATE #2:

As per one more comment :) You have 3 options actually:

  • scrape the whole page (see wikipedia article about that and here )
  • use some third party API (MySportFeeds above)
  • look into page source and find where it loads its data from — see this answer for more detail.

Using the last option, the code look like:

function getMatchResults() {
  var headers = {
    "Origin": "http://http://www.nhl.com/"
  };
  var options = {
    'method': 'get',
    //and any other additional parameters you might need
  };
  var baseUrl = 'http://www.nhl.com/stats/rest/individual/team/basic/game/teamsummary'
  var urlParameters = '?cayenneExp=teamId=23';
  var url = baseUrl + urlParameters;
  var results = UrlFetchApp.fetch(url, options); 
  Logger.log(results);
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  //further on goes your code to actually save the results into your sheet
}

You can see here that stats are returned by http://www.nhl.com/stats/rest/individual/team/basic/game/teamsummary?cayenneExp= and in the cayenneExp parameter you should specify you additional settings (like teamId=23 which is Vancouver in the example). This is how it looks in devtools: http://take.ms/fSH7H

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM