Created
January 7, 2013 15:24
-
-
Save mhawksey/4475786 to your computer and use it in GitHub Desktop.
Google Apps Script function to return a sitemap.xml to Google Sheets (Spreadsheets)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment