简体   繁体   English

循环期间Google表格脚本为空错误

[英]Google Sheet Script Null Error during loop

I am parsing an XML file using the following loop and Google Scripts, but one of the Childs is missing in the XML File at one location. 我正在使用以下循环和Google Scripts解析XML文件,但是XML文件中的某个位置缺少一个Childs。 So the entire process returns a null value. 因此,整个过程返回一个空值。 How do i retrieve all of the data that is not null? 如何检索所有不为空的数据?

> function stugas() {
>   var live = new Array();   
>   var url ="http://xml.pinnaclesports.com/pinnacleFeed.aspx?sportType=Football&contest=no&sportSubType=NFL";
>   var parameters = {method : "get", payload : ""};   var xml =
>   UrlFetchApp.fetch(url, parameters).getContentText();   
>   var document = XmlService.parse(xml);
>   var games = document.getRootElement().getChild('events').getChildren('event');  
> if(document == null) {
>     document.getRootElement().getChild('events').getChildren('event');   }   for (var i=0; i < games.length; i++) {
>       vegas=[games[i].getChildText('event_datetimeGMT'),games[i].getChild('participants').getChildren('participant')[0].getChildText('participant_name'),
>                 games[i].getChild('participants').getChildren('participant')[0].getChildText('visiting_home_draw'),
>                 games[i].getChild('participants').getChildren('participant')[1].getChildText('participant_name'),
>                 games[i].getChild('participants').getChildren('participant')[1].getChildText('visiting_home_draw'),
>                 /**games[i].getChild('periods').getChildren('period')[0].getChild('moneyline').getChildText('moneyline_visiting'),
>                 games[i].getChild('periods').getChildren('period')[0].getChild('moneyline').getChildText('moneyline_home'),**/
>                 games[i].getChild('periods').getChildren('period')[0].getChild('spread').getChildText('spread_visiting'),
>                 games[i].getChild('periods').getChildren('period')[0].getChild('spread').getChildText('spread_home'),
>                 games[i].getChild('periods').getChildren('period')[0].getChild('spread').getChildText('spread_adjust_visiting'),
>                 games[i].getChild('periods').getChildren('period')[0].getChild('spread').getChildText('spread_adjust_home'),
>                 games[i].getChild('periods').getChildren('period')[0].getChild('total').getChildText('total_points'),
>                 games[i].getChild('periods').getChildren('period')[0].getChild('total').getChildText('over_adjust'),
>                 games[i].getChild('periods').getChildren('period')[0].getChild('total').getChildText('under_adjust')]
>       
>       
>       live.push(vegas);
>     }
>     return live;   }

If you write your inner loop this way you could then test for each element to see if they exist or not (did that for the moneyline only here): 如果以这种方式编写内循环,则可以测试每个元素是否存在(仅在此处针对赚钱线进行检查):

vegas = [];
var game = games[i];
vegas.push(game.getChildText('event_datetimeGMT'));
var participant = game.getChild('participants').getChildren('participant');
vegas.push(participant[0].getChildText('participant_name'));
vegas.push(participant[0].getChildText('visiting_home_draw'));
vegas.push(participant[1].getChildText('participant_name'));
vegas.push(participant[1].getChildText('visiting_home_draw'));
var period = game.getChild('periods').getChildren('period')[0];
if (period.getChild('moneyline')) {
  vegas.push(period.getChild('moneyline').getChildText('moneyline_visiting'));
  vegas.push(period.getChild('moneyline').getChildText('oneyline_home'));
}
else {
  vegas.push("");
  vegas.push("");
}
var spread = game.getChild('periods').getChildren('period')[0].getChild('spread');
vegas.push(spread.getChildText('spread_visiting'));
vegas.push(spread.getChildText('spread_home'));
vegas.push(spread.getChildText('spread_adjust_visiting'));
vegas.push(spread.getChildText('spread_adjust_home'));
vegas.push(period.getChild('total').getChildText('total_points'));
vegas.push(period.getChild('total').getChildText('over_adjust'));
vegas.push(period.getChild('total').getChildText('under_adjust'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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