9
2

More than 3 years have passed since last update.

面倒なカレンダー登録はGASにやらせる

Last updated at Posted at 2020-12-05

GASを使ってカレンダー登録

毎月の勤務シフトなど不定期な予定をGoogleカレンダーへ
GASを使って半自動的に登録していきます。

1. スプレッドシートへ予定表を作成

カレンダーに入力する元データは手動です。
ぽちぽち作っていきます。

画像はフィクションです。実在の人物や団体などとは関係ありません。
フィクションとは -Wikipedia

”早”の時間は 9時から17時まで
”遅”の時間は 17時から24時まで
空欄は休日9時から24時までの設定とします。

2. テストスクリプトの作成

GASからカレンダー登録用のテストスクリプトを作成します

エディタを開く

「1.」で作ったスプレッドシートから[ツール]→[スクリプト エディタ]をクリック。
スクリプトの作成できるページが表示されます。

スクリプトを作成

デフォルトで入力されているコードは削除し以下のコードを入力します。

function createEvents() {
  let calendar = CalendarApp.getDefaultCalendar();
  let title = 'GASテスト';
  let startTime = new Date('2021/1/1 10:00:00');
  let endTime = new Date('2021/1/1 12:00:00');
  let option = {
    description: 'discription',
    location: 'location'
  }

  calendar.createEvent(title, startTime, endTime, option);
}

let calendar = CalendarApp.getDefaultCalendar();でカレンダーを読み込み
calendar.createEvent(title, startTime, endTime, option);でカレンダー登録します。

let startTime = new Date('2021/1/1 10:00:00');はカレンダーの開始時間
let endTime = new Date('2021/1/1 12:00:00');はカレンダーの終了時間

スクリプトを動かす

入力ができたら「▶︎」のボタンを押して実行

するとアクセス許可のダイアログが開くので問題なければ承認をポチポチ

承認できたら、カレンダーが登録されているか確認します。

こんな感じに登録されているはずです。

let option = {
    description: 'discription',
    location: 'location'
}

optionはカレンダー詳細の内容です。
descriptionは説明、
locationは場所の登録内容
今回の記事では不要ですので後のコードからは省略します。

3. スプレッドシートのデータを読み込む

スケジュールパターンを設定

const scheduleTime = {
  'asa' : {'title' : '早番','startTime' :  '9:00:00','endTime' : '17:00:00'},
  'yoru'  : {'title' : '遅番','startTime' : '16:00:00','endTime' : '24:00:00'},
  'yasumi'  : {'title' : 'シフト休み','startTime' : '10:00:00','endTime' : '18:30:00'}
};
function setTimes(scr){
  if(scr === ''){
    return scheduleTime.yasumi;
  }else if(scr === ''){
    return scheduleTime.asa;
  }else{
    return scheduleTime.yoru;
  };
};

”早”の時間は 9時から17時まで
”遅”の時間は 16時から24時まで
空欄は休日(9時から24時まで)の設定とします。

データ読み込み用のスクリプトを作成

let sheet = SpreadsheetApp.getActiveSheet();
function getSource() {
    let range = sheet.getRange('A1:C31');
    let source = range.getValues();
    let datas = [];
    source.forEach(function (elem, index) {
        datas[index] = {
            "data": "",
            "schedule": ""
        };
        datas[index].data = elem[0].getFullYear() + '/' + (elem[0].getMonth() + 1) + '/' + elem[0].getDate();
        datas[index].schedule = setTimes(elem[2]);
    });
    return datas;
}

let sheet = SpreadsheetApp.getActiveSheet();でスプレッドシートを読み込み
let range = sheet.getRange('A1:C31');で読み込む範囲を指定
let source = range.getValues();で範囲内のデータを読み込み
source.forEach(function(elem, index) {...});で読み込んだデータを整理
ここでは取得したData値をyyyy/mm/dd に変換ししています。
「スケジュールパターンを設定」で作った関数setTime()でスケジュールパターンをデータに挿入

これを実行すると以下の値を返します。


 [ { data: '2021/1/1',shift: { title: 'シフト休み', startTime: '10:00:00', endTime: '24:00:00' } },
  { data: '2021/1/2',shift: { title: '早番', 'startTime' : '9:00:00','endTime' : '17:00:00' } },
  { data: '2021/1/3',....}
  .
  .
  ....]

4. スケジュールをカレンダー登録する

カレンダー登録用のスクリプトとスプレッドシート読み込み用のスクリプトができたので実際にカレンダーを登録していきます。

function setCalendar() {
    let calendar = CalendarApp.getDefaultCalendar();
    let schedule = getSource();
    schedule.forEach(function (elem, index) {
        let title = elem.schedule.title;
        let startTime = new Date(elem.data + ' ' + elem.schedule.startTime);
        let endTime = new Date(elem.data + ' ' + elem.schedule.endTime);
        calendar.createEvent(title, startTime, endTime);
    });
};

カレンダーを読み込み、
「データ読み込み用のスクリプトを作成」で作った関数getSource()でスプレッドシートのデータを取得
schedule.forEach(function (elem, index) {...});で各スケジュールをカレンダーへ登録

これを実行すると各日にちごとにスケジュールがカレンダーに登録されているはずです。

全コード


const scheduleTime = {
  'asa' : {'title' : '早番','startTime' :  '9:00:00','endTime' : '17:00:00'},
  'yoru'  : {'title' : '遅番','startTime' : '16:00:00','endTime' : '24:00:00'},
  'yasumi'  : {'title' : 'シフト休み','startTime' : '10:00:00','endTime' : '18:30:00'}
};
function setTimes(scr){
  if(scr === ''){
    return scheduleTime.yasumi;
  }else if(scr === ''){
    return scheduleTime.asa;
  }else{
    return scheduleTime.yoru;
  };
};

let sheet = SpreadsheetApp.getActiveSheet();
function getSource() {
    let range = sheet.getRange('A1:C31');
    let source = range.getValues();
    let datas = [];
    source.forEach(function (elem, index) {
        datas[index] = {
            "data": "",
            "schedule": ""
        };
        datas[index].data = elem[0].getFullYear() + '/' + (elem[0].getMonth() + 1) + '/' + elem[0].getDate();
        datas[index].schedule = setTimes(elem[2]);
    });
    return datas;
}

function setCalendar() {
    let calendar = CalendarApp.getDefaultCalendar();
    let schedule = getSource();
    schedule.forEach(function (elem, index) {
        let title = elem.schedule.title;
        let startTime = new Date(elem.data + ' ' + elem.schedule.startTime);
        let endTime = new Date(elem.data + ' ' + elem.schedule.endTime);
        calendar.createEvent(title, startTime, endTime);
    });
};

参考文献


:christmas_tree: FORK Advent Calendar 2020
PREV :arrow_left: 4日目 マテリアルデザインのCSSフレームワーク「Materialize」を触ってみた @sk2usa さん
NEXT :arrow_right: 7日目 canvasとJSで、目に優しいオリジナルテトリス @yuzukisakioka さん

9
2
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
9
2