Skip to content

Instantly share code, notes, and snippets.

@ilyasozkurt
Created Sep 24, 2021
Embed
What would you like to do?
function checkURLs() {
var rows = new Array;
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('URLs');
var logsSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Logs');
rows = sheet.getSheetValues(2, 1, sheet.getLastRow(), sheet.getLastColumn());
for (index in rows) {
var isAlive = false; //
var row = rows[index]; //Get current for from rows
var url = row[0]; //Get url from row
var _index = parseInt(index) + 2;
if (typeof url !== 'undefined' && url !== '') {
try {
//try to ping
response = UrlFetchApp
.fetch(url)
//if success set alive true.
isAlive = true;
} catch (error) {
//do nothing.
sheet.getRange(_index, 2).setValue('failed');
sheet.getRange(_index, 3).setValue(getTime());
//Write a log to logs section
logsSheet.appendRow([
url, error, getTime()
])
}
if (isAlive) {
//write live status
sheet.getRange(_index, 2).setValue(response.getResponseCode());
sheet.getRange(_index, 3).setValue(getTime());
continue;
}
}
}
}
function getTime() {
var d = new Date();
var timeStamp = d.getTime();
return d.toLocaleString(); // "12:35 PM", for instance
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment