Javascript 循环期间出现Google工作表脚本Null错误

Javascript 循环期间出现Google工作表脚本Null错误,javascript,google-apps-script,xml-parsing,google-sheets,Javascript,Google Apps Script,Xml Parsing,Google Sheets,我正在使用以下循环和Google脚本解析XML文件,但XML文件中某个位置缺少一个child。所以整个过程返回一个空值。如何检索所有非空的数据 > function stugas() { > var live = new Array(); > var url ="http://xml.pinnaclesports.com/pinnacleFeed.aspx?sportType=Football&contest=no&sportSubType=NFL

我正在使用以下循环和Google脚本解析XML文件,但XML文件中某个位置缺少一个child。所以整个过程返回一个空值。如何检索所有非空的数据

> 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;   }
>函数stugas(){
>var live=新数组();
>变量url=”http://xml.pinnaclesports.com/pinnacleFeed.aspx?sportType=Football&contest=no&sportSubType=NFL";
>var参数={method:“get”,有效负载:};var xml=
>获取(url,参数).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;ivegas=[games[i].getChildText('event_datetimeGMT'),games[i].getChild('participants').getChildren('participant')[0].getChildText('participant_name'),
>游戏[i].getChild(“参与者”).getChildren(“参与者”)[0].getChildText(“到家与抽签”),
>游戏[i].getChild(“参与者”).getChildren(“参与者”)[1].getChildText(“参与者名称”),
>游戏[i].getChild(“参与者”).getChildren(“参与者”)[1].getChildText(“到家与抽签”),
>/**games[i].getChild('periods').getChildren('periods')[0].getChildren('moneyline').getChildText('moneyline_'),
>游戏[i].getChild('periods').getChildren('periods')[0].getChildren('moneyline').getChildText('moneyline_home')**/
>游戏[i]。getChild('periods')。getChildren('periods')[0]。getChild('spread')。getChildText('spread_'),
>游戏[i]。getChild('periods')。getChildren('periods')[0]。getChild('spread')。getChildText('spread_home'),
>游戏[i].getChild('periods').getChildren('periods')[0].getChild('spread').getChildText('spread\u adjust\u Visition'),
>游戏[i]。getChild('periods')。getChildren('periods')[0]。getChild('spread')。getChildText('spread\u adjust\u home'),
>游戏[i]。getChild('periods')。GetChilds('periods')[0]。getChild('total')。getChildText('total_points'),
>游戏[i]。getChild('periods')。GetChilds('periods')[0]。getChild('total')。getChildText('over_adjust'),
>游戏[i]。getChild('periods')。getChildren('periods')[0]。getChild('total')。getChildText('under_adjust')]
>       
>       
>现场推送(维加斯);
>     }
>返回live;}

如果以这种方式编写内部循环,则可以测试每个元素,以查看它们是否存在(仅在此处对moneyline进行了测试):


如果以这种方式编写内部循环,则可以测试每个元素,以查看它们是否存在(此处仅针对moneyline):


谢谢你,哈罗德,你修好了,哈罗德,你修好了
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'));