-
Notifications
You must be signed in to change notification settings - Fork 1
/
FinalUrl.js
48 lines (38 loc) · 1.06 KB
/
FinalUrl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Created by: RemcoE33
https://github.com/RemcoE33/apps-script-codebase
1. Select the urls you want to final url from, then invoke script via menu.
2. Values will be places in the +1 offset column.
*/
function onOpen(e){
const menu = SpreadsheetApp.getUi().createMenu('Utill');
menu.addItem('Find','urlRedirects');
menu.addToUi();
}
function urlRedirects() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const activeRange = sheet.getActiveRange();
const urls = [].concat(...activeRange.getValues());
const result = [];
urls.forEach(url => {
result.push([findUrl(url)]);
});
activeRange.offset(0,1).setValues(result)
}
function findUrl(url) {
const options = {
'followRedirects': false,
'muteHttpExceptions': false
}
const response = UrlFetchApp.fetch(url, options);
const redirectUrl = response.getHeaders()['Location'];
if (redirectUrl) {
const nextUrl = findUrl(redirectUrl);
console.log(nextUrl);
return nextUrl;
} else {
console.log(url);
return url;
}
}