LoginSignup
5
5

More than 3 years have passed since last update.

スプレッドシートでウェブページの簡易リダイレクトテストツールを作った

Last updated at Posted at 2019-06-26

なんで作った?

URLたくさんポチポチするの嫌だったから・・・。
ちゃんとリダイレクト先が合っているかも確かめたかった。

実行結果

スクリーンショット 2019-06-26 20.31.43.png

実装

google apps scriptで作っています。
spread sheetからスクリプトエディタ開いてもらって下記をコピペしてください。
redirectTestを実行してもらえれば実行結果のように3列に対して処理が走る仕組みです。

URL列は、調べたいURLをhttpから始めてもらっても、省略した形でも大丈夫です。
status code列は、200なら緑、他は赤に塗られます。
redirect to列は、HTTPレスポンス部分にリダイレクト先が明記されている場合に、リダイレクト先URLを記載します。

いじるのはURL列にURLを羅列するだけ。
gasの処理時間制限の6分があるので、対象URLが多い場合は小分けにするなどしてみてください。

function redirectTest() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheetByName('redirect test');
  var range = sheet.getDataRange();
  var values = range.getValues();

  var options = {
    'followRedirects' : false
  };

  var url, code, location, response;
  for (var i = 1; i < values.length; i++) {
    url = values[i][0];
    code = '';
    location = '';

    response = UrlFetchApp.fetch(url, options);    
    code = response.getResponseCode();
    location = response.getAllHeaders().Location;

    paintForStatusCode(range, i + 1, code);
    setLocation(range, i + 1, location);
  }
}

function paintForStatusCode(range, i, code) {
  var cell = range.getCell(i, 2);
  cell.setValue(code);
  if(code == 200) {
    cell.setBackground('#00FF00');
  } else {
    cell.setBackground('#FF0000');
  }
}

function setLocation(range, i, location) {
  var cell = range.getCell(i, 3);
  if(location != undefined) {
    cell.setValue(location);
  } 
}
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5