Last active
November 1, 2022 02:24
-
-
Save ushitora-anqou/a68ef7328087669e3cac9bb5687050fc to your computer and use it in GitHub Desktop.
Transfer plans in Google calendar to Todoist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CALENDAR_IDS = ["カレンダー連携ID"]; | |
const TODOIST_TOKEN = "Todoistトークン"; | |
const TODOIST_PROJECT_ID = "Todoist追加先プロジェクトID"; | |
function pad02(s) { | |
return ("0" + s).slice(-2); | |
} | |
function createTodoistTasks(todoistToken, todoistProjectId, tasks, dateForAllDayTask) { | |
for (let task of tasks) { | |
let payload = { | |
content: task.title, | |
project_id: todoistProjectId, | |
}; | |
if (task.isAllDayTask) { | |
// Don't use startTime because it indicates the starting date of the (all-day) task. | |
const t = dateForAllDayTask; | |
payload.due_date = `${t.getFullYear()}-${pad02(t.getMonth()+1)}-${pad02(t.getDate())}`; | |
} else { | |
payload.due_datetime = task.startTime.toISOString(); | |
} | |
let res = UrlFetchApp.fetch("https://api.todoist.com/rest/v2/tasks", { | |
method: "POST", | |
contentType: "application/json", | |
headers: { | |
"Authorization": `Bearer ${todoistToken}`, | |
}, | |
payload: JSON.stringify(payload), | |
}); | |
//console.log(res.toString()); | |
} | |
} | |
function fetchEventsOn(calendar, date) { | |
const events = calendar.getEventsForDay(date); | |
let res = []; | |
for (let ev of events) { | |
const task = {}; | |
task.title = ev.getTitle(); | |
task.isAllDayTask = ev.isAllDayEvent(); | |
if (ev.isAllDayEvent()) | |
task.startTime = ev.getAllDayStartDate(); | |
else | |
task.startTime = ev.getStartTime(); | |
res.push(task) | |
} | |
return res; | |
} | |
function doTrigger(){ | |
const today = new Date(); | |
for (let calendarId of CALENDAR_IDS) { | |
const calendar = CalendarApp.getCalendarById(calendarId); | |
if (calendar === null) { | |
console.log(`Can't get calendar: ${calendarId}`); | |
continue; | |
} | |
const events = fetchEventsOn(calendar, today); | |
createTodoistTasks(TODOIST_TOKEN, TODOIST_PROJECT_ID, events, today); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment