= google工作表中的IMPORTXML自动更新NFL分数

时间:2017-08-20 16:00:42

标签: xml google-apps-script google-sheets xml-parsing formulas

我在google工作表中使用了以下importhtml,我尝试了一些选项让它尽可能地自动更新,但到目前为止所有事情都失败了。我知道有一些人在使用Apps脚本方面取得了成功,但我对如何让我的示例工作有点困惑。这些是我正在使用的以下Importxml:

A1 = cd C:\Program Files (x86)\DevDesktop\apache\bin
B1 = importxml("http://www.nfl.com/liveupdate/scorestrip/ss.xml","//g/@q" )

1 个答案:

答案 0 :(得分:0)

您能做的最好的就是创建一个新脚本。然后创建2个新的脚本触发器。使它在开放状态下运行,并在每分钟运行一次触发时间。 这是我用于相同目的的脚本。

function GetNFLScore(){

var url = ('http://www.nfl.com/liveupdate/scores/scores.json');
var res = UrlFetchApp.fetch(url);
var content = res.getContentText();
var json = JSON.parse(content);   
var bye = "";

// Extracts the keys (these change every week)
var keys = [];
for(var k in json) keys.push(k);

// Declare array for results
var NFLResults =[];

// Make a Title Row
NFLResults.push(["Qtr","Time","Home","Pts","Away","Pts","Pos.","Down","To Go","Yard Line","Note","Network"]);

// Extracts the Games one per line
for(n=0; n<keys.length; n++)  {   
  var Clock = json[keys[n]]["clock"];                 // Kickoff time if pregame, Game Clock if game time
  var Qtr = json[keys[n]]["qtr"];                     // Gamestate (Pre, or Final), Game quater if game time
  var Home = json[keys[n]]["home"]["abbr"];           // Home Team
  var HsT = json[keys[n]]["home"]["score"]["T"];      // Home Total Score
  var Away = json[keys[n]]["away"]["abbr"];           // Away Team
  var AsT = json[keys[n]]["away"]["score"]["T"];      // Away Total Score
  var TV = json[keys[n]]["media"]["tv"];              // TV Network (NBC, FOX, ESPN, ect..)
  var YardLine = json[keys[n]]["yl"];                 // Current scrimage yard line
  var PosTeam = json[keys[n]]["posteam"];             // Team with ball possession
  var RedZone = json[keys[n]]["redzone"];             // Redzone boolean
  var Down = json[keys[n]]["down"];                   // Current Down
  var ToGo = json[keys[n]]["togo"];                   // Current To Go yardage for 1st down
  var Note = json[keys[n]]["note"];                   // Scoring play (Touchdown, Field Goal, Extra Point, Safety)

  // Correcting team abbr
  if( Home == "JAC" ){ Home = "JAX";}                 // JAC > JAX
  else if( Home == "WAS" ){ Home = "WSH";}            // WAS > WSH
  else if( Home == "LA"  ){ Home = "LAR";}            // LA > LAR

  if( Away == "LA"  ){ Away = "LAR";}      
  else if( Away == "WAS" ){ Away = "WSH";}
  else if( Away == "JAC" ){ Away = "JAC";}

  // Clear non-score related game data if game is final
  if( Qtr == "Final" ){ 
    Clock = "";
    PosTeam = "";
    RedZone = "";
    Down = "";
    ToGo = "";
    Note = "";
    TV = "";
  } 
  else if( Qtr == "Final OT" ){ 
    Clock = "";
    PosTeam = "";
    RedZone = "";
    Down = "";
    ToGo = "";
    Note = "";
    TV = "";
  }

  // Display Game data
  NFLResults.push([Qtr,Clock,Home,HsT,Away,AsT,PosTeam,Down,ToGo,YardLine,Note,TV]);
}

// Clear the bottom of the scoreboard from lack of scores caused by bye weeks
var rows = NFLResults.length
for(n=0; n<(17-rows); n++) {
  NFLResults.push([bye,bye,bye,bye,bye,bye,bye,bye,bye,bye,bye,bye]);   // N'SYNC
}

// Create new tab 'NFL Scores' if it doesn't exist.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var itt = ss.getSheetByName('NFL Scores');
if (!itt) {

    ss.insertSheet('NFL Scores');
}

// Push Scoreboard to spreadsheet
SpreadsheetApp.getActive().getSheetByName('NFL Scores').getRange(1,1,17,12).setValues(NFLResults);}