Skip to content

DNS in Google Sheets

Last updated View as MarkdownAgent setup

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;
		}
	}