-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathxmlscript.js
More file actions
39 lines (34 loc) · 1.46 KB
/
Copy pathxmlscript.js
File metadata and controls
39 lines (34 loc) · 1.46 KB
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
36
37
38
39
/* ---------------------------------------------------------------------------
* Sitemap extractor (Google sheets)
*
* @desc Extracts urls from XML sitemap index or sitemap files
* @author Dave Sottimano @dsottimano Twitter
* @license MIT (http://www.opensource.org/licenses/mit-license.php)
* @version 1.0
* -------------------------------------------------------------------------*/
/**
* Returns URLs in sitemap.xml file or sitemap index file
*
* @param {"https://www.google.com/gmail/sitemap.xml"} sitemapUrl REQUIRED The url of the sitemap
* @return Returns urls <loc> from an xml sitemap
* @customfunction
*/
function EXTRACT_SITEMAP(sitemapUrl) {
try {
var xml = UrlFetchApp.fetch(sitemapUrl,{muteHttpExceptions:true});
var document = XmlService.parse(xml.getContentText());
var root = document.getRootElement()
var namespace = root.getNamespace().getURI()
var sitemapNameSpace = XmlService.getNamespace(namespace);
let urls = root.getChildren('url',sitemapNameSpace)[0] ? root.getChildren('url', sitemapNameSpace) : root.getChildren('sitemap', sitemapNameSpace);
var locs = []
for (var i=0;i <urls.length;i++) {
locs.push(urls[i].getChild('loc', sitemapNameSpace).getText())
}
return locs
} catch (e) {
console.log(e)
if (e.toString().includes("The markup in the document preceding the root element must be well-formed")) return "Parsing error: is this a valid XML sitemap?";
return e.toString()
}
}