Action

Import Todoist Project

Posted by agiletortoise, Last update over 6 years ago

Import project tasks from Todoist into Markdown list.

For more Todoist examples, visit the Todoist Integration Guide.

Steps

  • script

    // Prompt to select Todoist project and labels
    // Draft as task in Todoist
    
    // utility function
    let mapIds = (arr, key, valKey) => {
    	let result = {};
    	arr.forEach(o => {
    		result[o[key]] = o[valKey];
    	})
    	return result;
    }
    
    let f = () => {
    	// create Todoist object and load projects
    	let todoist = Todoist.create();
    	let projects = todoist.getProjects();
    	
    	if (!projects) { 
    		console.log("Unable to retreive data from Todoist.");
    		return false; 
    	}
    
    	// create prompt
    	let p = Prompt.create();
    	p.title = "Select Project to Import";
    
    	// add projects
    	let projectLookup = mapIds(projects, "name", "id");
    	let projectNames = Object.keys(projectLookup);
    	p.addPicker("project", "Project", [projectNames], [0]);
    	
    	p.addButton("Import Project");
    	if (!p.show()) {
    		context.cancel();
    		return true;
    	}
    
    	// read values from prompt
    	let projectIndex = p.fieldValues["project"][0];
    	let projectName = projectNames[projectIndex];
    	let projectID = projectLookup[projectName];
    
    	let tasks = todoist.getTasks({
    		"project_id": projectID
    	});
    	if (!tasks) { 
    		alert("No tasks to import.");
    		return false;
    	}
    	
    	let content = [];
    	content.push("## " + projectName);
    	content.push("");
    	
    	for (let task of tasks) {
    		content.push("- " + task.content);
    	}
    
    	let d = Draft.create();
    	d.content = content.join("\n");
    	d.update();
    	editor.load(d);
    	return true;
    }
    
    if (!f()) {
    	context.fail();
    }
    
    

Options

  • After Success Archive
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.