Skip to content

Instantly share code, notes, and snippets.

@nac-39
Created November 21, 2021 15:55
Show Gist options
  • Save nac-39/e75048b950b4be91917a78d590e64065 to your computer and use it in GitHub Desktop.
Save nac-39/e75048b950b4be91917a78d590e64065 to your computer and use it in GitHub Desktop.
NUCTのカレンダー→Google Calendar→Trello
function Getnow() {
var d = new Date();
var y = d.getFullYear();
var mon = d.getMonth() + 1;
var d2 = d.getDate();
var h = d.getHours();
var min = d.getMinutes();
var s = d.getSeconds();
var now = y+"/"+mon+"/"+d2+" "+h+":"+min+":"+s;
return now;
}
function setup() {
//初期状態を作るために使う.最初に一回だけ実行してね.
CalendarApp.getDefaultCalendar();
const calendarId = cID();
const calendar = CalendarApp.getCalendarById(calendarId);
const startdate = new Date();
const enddate = new Date('2029/12/31');
const events = calendar.getEvents(startdate,enddate);
for(const event of events){
let titleEvent = event.getTitle();
let description = event.getDescription();
let shimekiri = event.getEndTime().toISOString();
console.log(shimekiri);
addCard(listId,titleEvent,description,shimekiri);
}
const sheet = SpreadsheetApp.getActiveSheet();
const cell = sheet.getRange(sheet.getLastRow(),1);
cell.setValue(Getnow());
}
const trelloKey = trello().id;//keyを入力してください
const trelloToken = trello().token;//tokenを入力してください
const userName = trello().uname;//user nameを入力してください
const boardId = trello().board;
const listId = trello().list;
function getBoard() {
var url = 'https://trello.com/1/members/' + userName + '/boards?key=' + trelloKey + '&token=' + trelloToken + '&fields=name';
res = UrlFetchApp.fetch(url, {'method':'get'});
Logger.log(res);
}
function getListId(boardId){
var url = "https://trello.com/1/boards/" + boardId + "/lists?key=" + trelloKey + "&token=" + trelloToken + "&fields=name";
res = UrlFetchApp.fetch(url, {'method':'get'});
Logger.log(res);
}
function getCardId(listId) {
var url = "https://trello.com/1/lists/" + listId + "/cards?key=" + trelloKey + "&token=" + trelloToken + "&fields=name";
res = UrlFetchApp.fetch(url, {'method':'get'});
Logger.log(res);
}
//
function addCard(listId,name,desc,due){
var url = 'https://api.trello.com/1/cards/?key=' + trelloKey + '&token=' + trelloToken;
var options = {
'method' : 'post',
'muteHttpExceptions' : true,
'payload' : {
'name' : name,//タイトル
'desc' : desc,//内容
'due' : due,//締め切り
'idList' : listId,//リストid
'urlSource' : ''
}
}
console.log(UrlFetchApp.fetch( url, options ));
}
//google calendarの設定のところから持ってきてね.
function cID() {
return 'ほほほほほほhf@import.calendar.google.com';
}
//trello api keyとかで検索して,apikey(=id)とtokenを手に入れてね.
//unameは自分のユーザーネーム.
//board id とlist idはhandle_trelloの中の関数を実行して手に入れてね.
function trello(){
return {
"id": "",
"token": "",
"uname": "",
"board": "",
"list": "",
};
}
//この関数をトリガーにセットしてね.
function update() {
CalendarApp.getDefaultCalendar();
const sheet = SpreadsheetApp.getActiveSheet();
const calendarId = cID();
const calendar = CalendarApp.getCalendarById(calendarId);
const startdate = new Date();
const enddate = new Date('2030/12/31');
const events = calendar.getEvents(startdate,enddate);
const cell = sheet.getRange(sheet.getLastRow(),1);
let lastUpdate = new Date(cell.getValue());
lastUpdate = lastUpdate.getTime();
for(const event of events){
let whenNewEvent = event.getDateCreated().getTime();
let bool = lastUpdate < whenNewEvent;
if(bool == true){
let titleEvent = event.getTitle();
let description = event.getDescription();
let shimekiri = event.getEndTime().toISOString();
console.log(shimekiri);
addCard(listId,titleEvent,description,shimekiri);
//スプレッドシートに更新日時を書き足す
const cell = sheet.getRange(sheet.getLastRow()+1,1);
cell.setValue(Getnow());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment