Apps Script公式リファレンス: Apps Script Reference |障害・課題追跡: IssueTracker |Google Workspace: Status Dashboard - Summary

2018年2月16日金曜日

指定した期間の営業日を出す(平日・土・日・祝日をそれぞれ分ける)HTML Service


Google Apps Scriptで指定した期間の営業日を出す(平日・土・日・祝日をそれぞれ分ける)
をHTML ServiceでWebアプリにするコードの例です


実行手順
  1. 以下のコードgsとindex.htmlを保存する
  2. Webアプリとして公開する(HTMLでページを作る
  3. startとendの日付を入力する
  4. 「計算する」ボタンをクリックする
  5. ミリ秒から祝日の数までの計算結果が表示されます

Webアプリで計算する例(画像)



コード.gs
function doGet() {
  return HtmlService.createHtmlOutputFromFile("index");
}

function get_days_gs(start, end) {
  var start_date = new Date(start);
  var end_date = new Date(end);
  var miliseconds = end_date - start_date;
  var seconds = miliseconds / 1000;
  var minutes = seconds / 60;
  var hours = minutes / 60;
  var days = hours / 24;
  var weeks = Math.floor(days / 7);
  var rest_days = (days % 7);
  var days_obj = get_each_days(start_date, days);
  var result = [days_obj, miliseconds, seconds, minutes, hours, days]
  return result;
}

function get_each_days(start_date, diff) {
  var obj = {}
  var sundays = obj["sundays"] = [];
  var saturdays = obj["saturdays"] = [];
  var public_holidays = obj["public_holidays"] = [];
  var weekdays = obj["weekdays"] = [];
  for (var i = 0; i < diff; i++) {
    var year_now = start_date.getFullYear();
    var month_now = start_date.getMonth();
    var date_now = start_date.getDate();
    var new_date = new Date(year_now, month_now, date_now + i);
    var day = new_date.getDay();
    var date = Utilities.formatDate(new_date, 'Asia/Tokyo', 'yyyy/MM/dd');
    if (day == 0) {
      sundays.push(date);
    } else if (day == 6) {
      saturdays.push(date);
    } else if (public_holidays_obj[date]) {
      public_holidays.push(date);
    } else {
      weekdays.push(date);
    }
  }
  return obj;
}

var public_holidays_obj = {
  "2017/01/01": "元日",
  "2017/01/02": "元日 振替休日",
  "2017/01/09": "成人の日",
  "2017/02/11": "建国記念の日",
  "2017/03/20": "春分の日",
  "2017/04/29": "昭和の日",
  "2017/05/03": "憲法記念日",
  "2017/05/04": "みどりの日",
  "2017/05/05": "こどもの日",
  "2017/07/17": "海の日",
  "2017/08/11": "山の日",
  "2017/09/18": "敬老の日",
  "2017/09/23": "秋分の日",
  "2017/10/09": "体育の日",
  "2017/11/03": "文化の日",
  "2017/11/23": "勤労感謝の日",
  "2017/12/23": "天皇誕生日",
  "2018/01/01": "元日",
  "2018/01/08": "成人の日",
  "2018/02/11": "建国記念の日",
  "2018/02/12": "建国記念の日 振替休日",
  "2018/03/21": "春分の日",
  "2018/04/29": "昭和の日",
  "2018/04/30": "昭和の日 振替休日",
  "2018/05/03": "憲法記念日",
  "2018/05/04": "みどりの日",
  "2018/05/05": "こどもの日",
  "2018/07/16": "海の日",
  "2018/08/11": "山の日",
  "2018/09/17": "敬老の日",
  "2018/09/23": "秋分の日",
  "2018/09/24": "秋分の日 振替休日",
  "2018/10/08": "体育の日",
  "2018/11/03": "文化の日",
  "2018/11/23": "勤労感謝の日",
  "2018/12/23": "天皇誕生日",
  "2018/12/24": "天皇誕生日 振替休日"
}




index.html
<!DOCTYPE html>
<html>
<body>
  start:<input type="date" id="start"> end:
  <input type="date" id="end">
  <button id="bt">計算する</button><br>
  <input type="text" id="miliseconds">ミリ秒<br>
  <input type="text" id="seconds">秒<br>
  <input type="text" id="minutes">分<br>
  <input type="text" id="hours">時間<br>
  <input type="text" id="days">日<br>
  <input type="text" id="weekdays">営業日<textarea id="weekdays_date"></textarea><br>
  <input type="text" id="saturdays">土曜日の数<textarea id="saturdays_date"></textarea><br>
  <input type="text" id="sundays">日曜日の数<textarea id="sundays_date"></textarea><br>
  <input type="text" id="public_holidays">祝日の数<textarea id="public_holidays_date"></textarea><br>

  <script>
    var start = document.getElementById("start");
    var end = document.getElementById("end");
    var bt = document.getElementById("bt");
    var miliseconds = document.getElementById("miliseconds");
    var seconds = document.getElementById("seconds");
    var minutes = document.getElementById("minutes");
    var hours = document.getElementById("hours");
    var days = document.getElementById("days");
    var weekdays = document.getElementById("weekdays");
    var saturdays = document.getElementById("saturdays");
    var sundays = document.getElementById("sundays");
    var public_holidays = document.getElementById("public_holidays");
    var weekdays_date = document.getElementById("weekdays_date");
    var saturdays_date = document.getElementById("saturdays_date");
    var sundays_date = document.getElementById("sundays_date");
    var public_holidays_date = document.getElementById("public_holidays_date");

    bt.onclick = bt_clicked;

    function bt_clicked() {
      var start_value = start.value;
      var end_value = end.value;

      google.script.run
        .withFailureHandler(onFailure)
        .withSuccessHandler(onSuccess)
        .get_days_gs(start_value, end_value);
    }

    function onSuccess(result) {
      var days_obj = result[0];
      weekdays.value = days_obj["weekdays"].length;
      sundays.value = days_obj["sundays"].length;
      saturdays.value = days_obj["saturdays"].length;
      public_holidays.value = days_obj["public_holidays"].length;
      weekdays_date.value = days_obj["weekdays"];
      sundays_date.value = days_obj["sundays"];
      saturdays_date.value = days_obj["saturdays"];
      public_holidays_date.value = days_obj["public_holidays"];

      miliseconds.value = result[1];
      seconds.value = result[2];
      minutes.value = result[3];
      hours.value = result[4];
      days.value = result[5];
    }

    function onFailure(e) {
      alert([e.message, e.stack]);
    }
  </script>
</body>
</html>


Latest post

Google Apps Scriptの障害時はIssueTrackerを見てみる - Incidents for Apps Script are reported on Issue Tracker

IssueTracker > Apps Script issues https://issuetracker.google.com/savedsearches/566234 Google Apps Scriptの障害時は IssueTracker に課題が上がっていることが...