Quickly get sitemap URLs into Google Sheets

With this script you can import an XML sitemap into Google Sheets just by specifying the XML sitemap URL. 🚀

Check out the GIF to see it in action 🎥


How to add the Script to Google Sheets

1. Copy the script below:

/**
 * Get XML sitemap URLs and metadata. 
 *
 * @param {"https://example.com/sitemap.xml"} url - Input the XML sitemap URL. 
 * @param {"keyword"} filter [Optional] - keyword to filter URLs.
 * @customfunction
 */
function sitemap(url, filter) {
    // Validate if the URL is provided.
    if (!url) return 'You need to enter the URL';

    // Fetch the content of the sitemap from the given URL.
    const contentText = fetchSitemapContent(url);

    // If there was an error fetching the content, return the error message.
    if (typeof contentText === "string" && contentText.startsWith("Failed")) {
        return contentText;
    }

    // Parse the fetched XML content.
    const document = XmlService.parse(contentText).getRootElement();

    // Determine if the XML has a namespace.
    const namespace = document.getNamespace();

    // Handle extraction based on presence of a namespace and the type of sitemap (index vs regular).
    if (!namespace) {
        if (document.getName() === "sitemapindex") {
            return extractSitemapIndices(document);
        } else {
            return extractURLs(document, filter);
        }
    } else {
        if (document.getName() === "sitemapindex") {
            return extractSitemapIndices(document, namespace);
        } else {
            return extractURLs(document, namespace, filter);
        }
    }
}

// Fetch the content of a sitemap from a given URL.
function fetchSitemapContent(url) {
    // Fetch the content from the URL.
    const response = UrlFetchApp.fetch(url, {
        muteHttpExceptions: true,
        method: "GET",
        followRedirects: true
    });

    // Return an error message if fetching failed.
    if (response.getResponseCode() !== 200) {
        return `Failed to fetch sitemap from ${url}. Response code: ${response.getResponseCode()}`;
    }

    // Return the fetched content.
    return response.getContentText();
}

// Extract sitemap URLs from a sitemap index file.
function extractSitemapIndices(document, namespace) {
    const sitemaps = namespace ? document.getChildren('sitemap', namespace) : document.getChildren('sitemap');
    let results = [['Sitemap Indices']];
    sitemaps.forEach(sitemap => {
        const loc = namespace ? sitemap.getChild('loc', namespace).getText().trim() : sitemap.getChild('loc').getText().trim();
        results.push([loc]);
    });
    return results;
}

// Extract URLs and their metadata from a regular sitemap.
function extractURLs(document, namespace, filter) {
    const urls = namespace ? document.getChildren('url', namespace) : document.getChildren('url');
    let results = [['URLs', 'Last Modified', 'Change Frequency', 'Priority']];

    urls.forEach(url => {
        const loc = namespace ? url.getChild('loc', namespace).getText().trim() : url.getChild('loc').getText().trim();

        // If a filter keyword is provided, only include URLs that contain the keyword.
        if (!filter || (filter && loc.includes(filter))) {
            const lastmod = namespace ? (url.getChild('lastmod', namespace) || {getText: () => ""}).getText().trim() : (url.getChild('lastmod') || {getText: () => ""}).getText().trim();
            const changefreq = namespace ? (url.getChild('changefreq', namespace) || {getText: () => ""}).getText().trim() : (url.getChild('changefreq') || {getText: () => ""}).getText().trim();
            const priority = namespace ? (url.getChild('priority', namespace) || {getText: () => ""}).getText().trim() : (url.getChild('priority') || {getText: () => ""}).getText().trim();

            results.push([loc, lastmod, priority, changefreq]);
        }
    });

    return results;
}

2. Head over to Google Sheets

Or if you’re really smart, create a new sheet by going to: https://sheets.new

Select Script editor from the Tools menu.

Paste the script and save it.


3. Add the formula to any cell in your sheet

=sitemap("https://www.example.com/sitemap.xml","add keyword here")

Replace “https://www.example.com/sitemap.xml” with any XML sitemap URL and it will return a list of URLs from the sitemap.

Replace “add keyword here” with the keyword you want to filter XML sitemap pages by.

*Custom functions in Google Sheets have a 30 second timeout. This means, for larger XML sitemaps, it won’t work.


Thanks for stopping by 👋

I’m Andrew Charlton, the Google Sheets nerd behind Keywords in Sheets. 🤓

Questions? Get in touch with me on social or comment below 👇


More Scripts

Submit your response

Your email address will not be published. Required fields are marked *