checkUrlStatuses(urls, opts) checks a list of URLs in batches with UrlFetchApp.fetchAll and muteHttpExceptions. It returns one [url, status, error] row per input, ready to write next to a column. {batchSize} defaults to 10.
When to use
- Checking a sheet column of links for 404s before a customer hits them
- A weekly sweep of "Open ticket" or "View report" URLs
- Any list of http(s) URLs that is too long to fetch one at a time
How to use this snippet
/**
* Fetch a list of URLs in batches and return [url, status, error] rows.
* Uses UrlFetchApp.fetchAll with muteHttpExceptions and followRedirects.
* Blank entries become ['', '', '']. Non-http(s) values are not fetched.
* If a batch throws, those URLs are retried one at a time so one failure
* does not blank the rest. opts.batchSize defaults to 10.
*
* @param {Array} urls
* @param {{batchSize?: number}} [opts]
* @return {Array<Array>}
*/
function checkUrlStatuses(urls, opts) {
opts = opts || {};
var batchSize = opts.batchSize == null ? 10 : Number(opts.batchSize);
if (!(batchSize >= 1) || Math.floor(batchSize) !== batchSize) {
throw new Error('checkUrlStatuses: batchSize must be an integer >= 1');
}
var list = urls || [];
var rows = [];
var pending = [];
function flush_() {
if (!pending.length) return;
var requests = pending.map(function (p) {
return { url: p.url, muteHttpExceptions: true, followRedirects: true };
});
try {
var responses = UrlFetchApp.fetchAll(requests);
for (var i = 0; i < pending.length; i++) {
rows[pending[i].index][1] = responses[i].getResponseCode();
rows[pending[i].index][2] = '';
}
} catch (e) {
for (var j = 0; j < pending.length; j++) {
try {
var resp = UrlFetchApp.fetch(pending[j].url, {
muteHttpExceptions: true,
followRedirects: true
});
rows[pending[j].index][1] = resp.getResponseCode();
rows[pending[j].index][2] = '';
} catch (e2) {
rows[pending[j].index][1] = '';
rows[pending[j].index][2] = e2.message;
}
}
}
pending = [];
}
for (var i = 0; i < list.length; i++) {
var url = list[i] == null ? '' : String(list[i]).trim();
if (!url) {
rows.push(['', '', '']);
continue;
}
if (!/^https?:\/\//i.test(url)) {
rows.push([url, '', 'not an http(s) URL']);
continue;
}
rows.push([url, '', '']);
pending.push({ url: url, index: rows.length - 1 });
if (pending.length >= batchSize) flush_();
}
flush_();
return rows;
}
Example
function checkLinkColumn() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Links');
var last = sheet.getLastRow();
if (last < 2) return;
var urls = sheet.getRange(2, 1, last - 1, 1).getValues().map(function (r) { return r[0]; });
var rows = checkUrlStatuses(urls, { batchSize: 10 });
var stamp = new Date();
var out = rows.map(function (r) { return [r[1], r[2], stamp]; });
sheet.getRange(2, 2, out.length, 3).setValues(out); // B status, C error, D checked
}
Notes
- The status is the final HTTP code after redirects. A 301 that lands on a live page shows
200. A 404 is a status, not anerror.erroris filled when the fetch itself failed (bad DNS, a blocked request, a malformed URL the service rejected). 200does not mean the page is the one you wanted. Some sites return200with a sign-in or bot-check page; some block the Apps Script user agent with403.- Each URL counts toward the project's daily UrlFetch cap. Start with a small
batchSizeand a few rows. If you see429, slow down (a shortUtilities.sleepbetween batches is a common fix) or check fewer URLs per run. Budget Apps Script quotas like a grown-up and Retry UrlFetch With Backoff are the usual companions. - Row count always matches the input list, including blanks, so you can write the result next to the source column.
Happy Coding!
