public
Created

Google Apps Script function to return a sitemap.xml to Google Sheets (Spreadsheets)

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
function getSitemapData(url) {
try {
var options =
{
"method" : "get"
};
var response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() == 200) {
var output = [];
var row = [];
var responseStr = response.getContentText();
var XMLdoc = Xml.parse(responseStr); // parse xml
var urlset = XMLdoc.urlset;
var urls = urlset.getElements("url");
for (var i = 0; i < urls.length; i++) {
var el = urls[i].getElements();
if (i == 0){
for (j in el){
row.push(el[j].getName().getLocalName());
}
output.push(row);
var row = [];
}
for (j in el){
row.push(el[j].getText());
}
output.push(row);
var row = [];
}
return output;
}
} catch(e) {
throw e;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.