2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GASでシフトを皆のカレンダーに一括登録:第2回(第1回で作成した表からイベント登録)

Last updated at Posted at 2021-09-03

#前回の記事
https://qiita.com/meinai929/items/be1dff3cfff54740a0bf

今回はいよいよ↑の記事で作成した表を読み込んで、カレンダーにイベント登録していきたいと思います!

#シフト読み込み用の表
image.png

#今回作成した関数

#関数1:eventCreate()
実行結果。塊でイベント登録しなおすため、関数2を実行。
image.png
###コード

function eventCreate(){

//シートの項目を以下変数定義
  var sht, i, eventday, eventname, endEventday,name;
  var mailList;

  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  sht = spreadsheet.getSheetByName("readSheet");
  var Cal =CalendarApp.getCalendarById('メールアドレス');


  for(j =1; j<= sht.getLastColumn(); j+=4) {
    for(i=2; i<=sht.getLastRow();i++){
    //i行2列目の値(イベント日)をeventdayへ格納
      eventday = new Date(sht.getRange(i,j).getValue());

      endEventday =new Date(sht.getRange(i,j+1).getValue());
      mailList = sht.getRange(i,j+2).getValue();
      name = "監視シフト1";
      eventname = sht.getRange(i,j+3).getValue();
      var option1 ={
      guests:mailList,
      description:name
      }
//指定のカレンダーへインベント登録
      Cal.createEvent(eventname,
      eventday,
      endEventday,
      option1

     )
    }   
  }       
}

#関数2:uniteEvents()&beforeEventDelete()
実行結果。
image.png
###コード

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("シート1");
function uniteEvents(){
  
  var name = ["Aさん","Bさん","Cさん","Dさん","Eさん","Fさん"];
  var option2 ={
    description:"監視シフト2"
  }
  for(i=3;i<= sheet.getLastColumn(); i++){
    for(n in name){
      var options ={
        search:name[n]
      }
      var events = Cal.getEventsForDay(new Date(sheet.getRange(5,i).getValue()),options);
      if(events.length != 0){
        Cal.createEvent(name[n],events[0].getStartTime(),events[events.length-1].getEndTime(),option2)
      }  
    }
  }
  beforeEventDelete();
}

function beforeEventDelete(){
  var option1={
    search:"監視シフト1"
  }
  for(i=3;i<= sheet.getLastColumn(); i++){
    var beforeEvent = Cal.getEventsForDay(new Date(sheet.getRange(5,i).getValue()),option1);
    for(j in beforeEvent){
      beforeEvent[j].deleteEvent();
    } 
  }  
}

#関数3:deleteCalendarEvents()
塊で入れたシフトのイベントのみ消去。
###コード

function deleteCalendarEvents(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("readSheet");
  var options ={
    search:"監視シフト2"
  }
  for(i=1;i<= sheet.getLastColumn(); i+=4){
    var events = Cal.getEventsForDay(new Date(sheet.getRange(2,i).getValue()),options);
    for(j in events){
      events[j].deleteEvent();
    }
  } 
}

#まとめ
細かい書き方の追記などもしていきたいと思います。
ご指摘などありましたらお願いいたします。

2
1
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?