Checking redirects and status codes can be very complicated when you’re using a third-party website.
But this can be automated using Google sheets and if you don’t have a lot of knowledge in coding then you don’t need to worry.
You will also be able to know the chains of redirect with this code.
Script in Action
How to Check HTTP status codes and Redirected URLs on Google Sheets
- Firstly, Open the Google Sheets from here – Create a New Google Sheets
- Then you need to goto Tools –> Script Editor
- You can copy and paste the below given code.
function getStatusCode(url) {
const url_trimmed = url.trim();
let cache = CacheService.getScriptCache();
let result = cache.get(url_trimmed);
if (!result) {
const options = {
'muteHttpExceptions': true,
'followRedirects': false
};
const response = UrlFetchApp.fetch(url_trimmed, options);
const responseCode = response.getResponseCode();
cache.put(url_trimmed, responseCode, 21600);
result = responseCode;
}
return result;
}
function getRedirects(url) {
const urlKey = url.trim()+"k";
let cache = CacheService.getScriptCache();
let result = cache.get(urlKey);
if (!result) {
const params = {
'followRedirects': false,
'muteHttpExceptions': true
};
const res = UrlFetchApp.fetch(url, params);
const finalURL = res.getHeaders()['Location'];
cache.put(urlKey, finalURL, 21600);
result = finalURL;
}
return result;
}
- Then you need to click on the run button
- It will ask you for a few permission. You can allow those permissions.
- Then you can add these formula’s
=getStatusCode(A1)
=getRedirects(A1)
A1 represents any cell, and you can drag the formula down to check status codes and redirects for multiple URLs.
To make it quicker, there’s also a cache built into the script, so that the formula won’t refresh with new values for up to 6 hours.
Final Verdict
So, this is how you can easily check HTTP Status Codes and Redirected URLs on Google Sheets. I hope you loved it.
The inspiration for this app script is taken from keywords in the sheet website.
If this script isn’t working or if there is an issue let us know in the comments section. We will be replying to the comments in 8 hours.
Hi, I am getting this error
TypeError: Cannot read property ‘url_trimm’ of undefined
getStatusCode @ Code.gs:2
How to resolve this?
Hey Prabha,
You can download the google sheet template mentioned above.
Simple and brilliant. Simply brilliant. Thanks for sharing!