I am working on an apps script that loads xml data into a function and parses the data based on user input.
The xml is simple and straightforward:
<?xml version="1.0" encoding="UTF-8"?>
<Records>
<Record>
<username>jjohnson</username>
<firstName>John</firstName>
<lastName>johnson</lastName>
<email>[email protected]</email>
</Record>
<Record>
<username>bsimmons</username>
<firstName>ben</firstName>
<lastName>simmons</lastName>
<email>[email protected]</email>
</Record>
...etc
</Records>
The function takes the XML and loads the data into an array of objects and attempts to parse the data based on username and display the content as a row in google sheets.
function XMLCONTACT(username) {
var url = '<XML source>';
var ContactXml = UrlFetchApp.fetch(url).getContentText();
var document = XmlService.parse(ContactXml);
var root = document.getRootElement();
var arr = [];
var XMLChildren = root.getChildren("Record");
for (var i = 0; i < XMLChildren.length; i++) {
arr.push({
username: XMLChildren[i].getChild("username").getText(),
firstName: XMLChildren[i].getChild("firstName").getText(),
lastName: XMLChildren[i].getChild("lastName").getText(),
email: XMLChildren[i].getChild("email").getText()
});
}
var newarr = []
for(var j= 0; j < arr.length; j++){
if(arr[j].username === username ){
newarr.push(arr[j]);
}
}
return newarr;
}
The problem is that when the function runs, nothing is returned. I can get sheets to display the contents of the array without objects, but not the filtered output.
Thanks for your help!
Logger.log(arr)somewhere and see what is logged. - Liora HaydontLogger, for complex data, is to useconsoleand then view (and interact!) with the logged object in Stackdriver. - tehhowch