Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Returns URLs in sitemap.xml file
  3. *
  4. * @param {"https://www.google.com/gmail/sitemap.xml"} sitemapUrl REQUIRED The url of the sitemap
  5. * @param {"http://www.sitemaps.org/schemas/sitemap/0.9"} namespace REQUIRED Look at the source of the xml sitemap, look for the xmlns value
  6. * @return Returns urls <loc> from an xml sitemap
  7. * @customfunction
  8. */
  9.  
  10. function sitemap(sitemapUrl,namespace) {
  11.  
  12.   var xml = UrlFetchApp.fetch(sitemapUrl).getContentText();
  13.   var document = XmlService.parse(xml);
  14.   var root = document.getRootElement()
  15.   var sitemapNameSpace = XmlService.getNamespace(namespace);
  16.  
  17.   var urls = root.getChildren('url', sitemapNameSpace)
  18.   var locs = []
  19.  
  20.   for (var i=0;i <urls.length;i++) {
  21.     locs.push(urls[i].getChild('loc', sitemapNameSpace).getText())
  22.   }
  23.  
  24.   return locs  
  25. }
  26.  
  27.  
  28. /**
  29. * Returns URLs in sitemap index file
  30. *
  31. * @param {"https://www.google.com/sitemap.xml"} sitemapIndexUrl REQUIRED The url of the sitemap
  32. * @param {"http://www.sitemaps.org/schemas/sitemap/0.9"} namespace REQUIRED Look at the source of the xml sitemap, look for the xmlns value
  33. * @return Returns urls <loc> from an xml sitemap
  34. * @customfunction
  35. */
  36.  
  37. function sitemapIndex(sitemapIndexUrl,namespace) {
  38.  
  39.   var xml = UrlFetchApp.fetch(sitemapIndexUrl).getContentText();
  40.   var document = XmlService.parse(xml);
  41.   var root = document.getRootElement()
  42.   var sitemapNameSpace = XmlService.getNamespace(namespace);
  43.  
  44.   var urls = root.getChildren('sitemap', sitemapNameSpace)
  45.   var locs = []
  46.  
  47.   for (var i=0;i <urls.length;i++) {
  48.     locs.push(urls[i].getChild('loc', sitemapNameSpace).getText())
  49.   }
  50.  
  51.   return locs  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement