Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This script is called when I complete tasks on Todoist, and writes Todoist's completed task to google spreadsheet by IFTTT's webhook.
// ref: https://internetthingy.com/todoist-ifttt-gas-63.html
function doPost(e) {
//Todoist->IFTTTからwebhook->GASで受け取るスクリプト
//データはjson形式で受け取り
var data = JSON.parse(e.postData.getDataAsString());
//IFTTTはjsonを3項目までしか出力できないので、タスク名、時間関係データ、それ以外のデータの3項目にした。
var TaskContent = data.TaskContent;
var CompletedAt = data.CompletedAt;
var options = data.options;
CompletedAt = CompletedAt.replace(/January/g,"01")
.replace(/February/g,"02")
.replace(/March/g,"03")
.replace(/April/g,"04")
.replace(/May/g,"05")
.replace(/June/g,"06")
.replace(/July/g,"07")
.replace(/August/g,"08")
.replace(/September/g,"09")
.replace(/October/g,"10")
.replace(/November/g,"11")
.replace(/December/g,"12");
//完了日時とスケジュール日時を配列に格納
var TaskDate = CompletedAt.split( '-' );
var CompletedDate = convertISO8601(TaskDate[0]);
var ScheduledDate = convertISO8601(TaskDate[1]);
//その他データを切り出し
var opt = options.match(/^(.*)-%labels%-(.*)-%priority%-Priority ([1-4]{1})-%url%-(.*)$/);
var project = opt[1];
var labels = opt[2];
var priority = opt[3];
var url = opt[4];
var taskData = [CompletedDate, TaskContent, ScheduledDate, project, labels, priority, url]
var spread_body = SpreadsheetApp.openById("<ChangeMeToYourSpreadsheetID>").getActiveSheet();
spread_body.appendRow(taskData);
}
function convertISO8601(todoistDateFormatString) {
// 月、日、年、時、分、AM/PMの順に取り出す。
var dateOpt = todoistDateFormatString.match(/([0-9]{2}) ([0-9]{2}), ([0-9]{4})/);
var timeOpt = todoistDateFormatString.match(/at ([0-9]{2}):([0-9]{2})([A-Z]{2})/);
if (timeOpt === null) { // 時間が指定されていないとき
return Utilities.formatString('%4d-%02d-%02dT00:00:00+09:00', dateOpt[3], dateOpt[1], dateOpt[2]);
}
else if (timeOpt[3] === "PM" && timeOpt[1] !== "12") { // 昼の12時は12:00PM
var tmp = parseInt(timeOpt[1]) + 12;
timeOpt[1] = tmp.toString(10);
}
else if (timeOpt[3] === "AM" && timeOpt[1] === "12") { // 午前0時は12:00AM
timeOpt[1] = "00";
}
return Utilities.formatString('%4d-%02d-%02dT%02d:%02d:00+09:00', dateOpt[3], dateOpt[1], dateOpt[2], timeOpt[1], timeOpt[2]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.