I'm currently developing an apps script that loads XML data into a function and parses it based on user input.
The XML structure is fairly simple:
<?xml version="1.0" encoding="UTF-8"?>
<Records>
<Record>
<username>jjohnson</username>
<firstName>John</firstName>
<lastName>johnson</lastName>
<email><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="33595c5b59">[email protected]</a></email>
</Record>
<Record>
<username>bsimmons</username>
<firstName>ben</firstName>
<lastName>simmons</lastName>
<email><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb898e85868e8786838197">[email protected]</a></email>
</Record>
...etc
</Records>
The function reads the XML, converts the data to an object array, filters by username, and displays matching rows 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;
}
However, I'm facing an issue where the function doesn't return any output. While I can display the array content without objects, the filtered results are not showing up. Any assistance would be greatly appreciated!
Thank you for your support!