DNS in Google Sheets

Last updated May 6, 2026

Create a function

This tutorial creates a custom Google Sheets function that queries Cloudflare's 1.1.1.1 DNS resolver using DNS over HTTPS (DoH) — a protocol that encrypts DNS lookups over HTTPS. Once set up, you can type a formula like =NSLookup("A", "example.com") in any cell to retrieve DNS records without leaving your spreadsheet. This is useful for bulk domain audits, migration planning, or monitoring DNS changes across many domains at once.

To get started, open your Google Sheet and create a custom function in Google Apps Script ↗︎ with the following code:

function NSLookup(type, domain, useCache = false, minCacheTTL = 30) {
    // --- Parameter validation ---
    if (typeof type == "undefined") {
        throw new Error("Missing parameter 1 dns type");
    }

    if (typeof domain == "undefined") {
        throw new Error("Missing parameter 2 domain name");
    }

    if (typeof useCache != "boolean") {
        throw new Error("Only boolean values allowed in 3 use cache");
    }

    if (typeof minCacheTTL != "number") {
        throw new Error("Only numeric values allowed in 4 min cache ttl");
    }

    type = type.toUpperCase();
    domain = domain.toLowerCase();

    // --- Optional caching layer (uses Google Apps Script CacheService) ---
    let cache = null;
    if (useCache) {
        // Cache key and hash
        cacheKey = domain + "@" + type;
        cacheHash = Utilities.base64Encode(cacheKey);
        cacheBinKey = "nslookup-result-" + cacheHash;

        cache = CacheService.getScriptCache();
        const cachedResult = cache.get(cacheBinKey);
        if (cachedResult != null) {
            return cachedResult;
        }
    }

    // --- DNS-over-HTTPS query to Cloudflare's 1.1.1.1 resolver ---
    const url =
        "https://cloudflare-dns.com/dns-query?name=" +
        encodeURIComponent(domain) +
        "&type=" +
        encodeURIComponent(type);
    const options = {
        muteHttpExceptions: true,
        headers: {
            accept: "application/dns-json",
        },
    };

    const result = UrlFetchApp.fetch(url, options);
    const rc = result.getResponseCode();
    const resultText = result.getContentText();

    if (rc !== 200) {
        throw new Error(rc);
    }

    // --- Standard DNS response codes ---
    const errors = [
        { name: "NoError", description: "No Error" }, // 0
        { name: "FormErr", description: "Format Error" }, // 1
        { name: "ServFail", description: "Server Failure" }, // 2
        { name: "NXDomain", description: "Non-Existent Domain" }, // 3
        { name: "NotImp", description: "Not Implemented" }, // 4
        { name: "Refused", description: "Query Refused" }, // 5
        { name: "YXDomain", description: "Name Exists when it should not" }, // 6
        { name: "YXRRSet", description: "RR Set Exists when it should not" }, // 7
        { name: "NXRRSet", description: "RR Set that should exist does not" }, // 8
        { name: "NotAuth", description: "Not Authorized" }, // 9
    ];

    const response = JSON.parse(resultText);

    if (response.Status !== 0) {
        return errors[response.Status].name;
    }

    // --- Extract answer records and determine cache TTL ---
    const outputData = [];
    let cacheTTL = 0;

    for (const i in response.Answer) {
        outputData.push(response.Answer[i].data);
        const ttl = response.Answer[i].TTL;
        cacheTTL = Math.min(cacheTTL || ttl, ttl);
    }

    const outputString = outputData.join(",");

    if (useCache) {
        cache.put(cacheBinKey, outputString, Math.max(cacheTTL, minCacheTTL));
    }

    return outputString;
}

Using 1.1.1.1

When you call the NSLookup function with a record type and a domain, the cell displays the corresponding DNS record value — the data (such as an IP address) that DNS returns for that domain and record type.

The full function signature is:

=NSLookup(type, domain, useCache, minCacheTTL)

Parameter Required Default Description
type Yes — DNS record type to query (for example, A, AAAA, MX).
domain Yes — The domain name to look up.
useCache No false Set to true to cache results using Google Apps Script's CacheService, which reduces repeated DNS lookups in large spreadsheets.
minCacheTTL No 30 Minimum cache duration in seconds. The actual TTL is the higher of this value or the TTL returned by the DNS response.

<details>

<summary>

Supported DNS record types

</summary>

  • <code>A</code>
  • <code>AAAA</code>
  • <code>CAA</code>
  • <code>CNAME</code>
  • <code>DS</code>
  • <code>DNSKEY</code>
  • <code>MX</code>
  • <code>NS</code>
  • <code>NSEC</code>
  • <code>NSEC3</code>
  • <code>RRSIG</code>
  • <code>SOA</code>
  • <code>TXT</code>

</details>

For example, if cell B1 contains A (the record type) and B2 contains example.com (the domain), typing the following formula in another cell:

=NSLookup(B1, B2)

Depending on your regional settings, you may need to use a semicolon as the argument separator:

=NSLookup(B1; B2)

Google Sheets cell containing the NSLookup formula

Returns the A record for that domain:

198.41.214.162, 198.41.215.162

Google Sheets cell displaying the DNS lookup result

Was this helpful?

YesNo

On this page

DNS in Google Sheets - 图3Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/1.1.1.1/additional-options/dns-in-google-sheets/#page","headline":"DNS in Google Sheets","description":"Look up DNS records directly inside Google Sheets using Cloudflare's 1.1.1.1 DNS resolver and a custom Google Apps Script function.","url":"https://developers.cloudflare.com/1.1.1.1/additional-options/dns-in-google-sheets/","inLanguage":"en","image":"https://developers.cloudflare.com/1.1.1.1/additional-options/dns-in-google-sheets/og.png?v=5b7768ba01799aa5","dateModified":"2026-05-06","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"},"keywords":["JavaScript","JSON","Integration"]}


description: Run DNS lookups and WHOIS queries directly in Discord using the 1.1.1.1 bot. Invite the bot to a server or add it to your account to query DNS records without leaving Discord. title: DNS over Discord image: https://developers.cloudflare.com/1.1.1.1/additional-options/dns-over-discord/og.png?v=c6f98255cfda3133


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/1.1.1.1/llms.txt
Use this file to discover all available pages before exploring further.