Action

Todoist: Import Tasks from selected Project with sections

Posted by @FlohGro, Last update over 4 years ago - Unlisted

Select a project from your Todoist account. Action creates a Markdown list with all the tasks of the project divided by their sections.

Steps

  • script

    // Prompt to select Todoist project and labels
    // Draft as task in Todoist
    // action modified by @FlohGro
    // original action: https://actions.getdrafts.com/a/1fH
    
    
    // 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];
    
    
        // sections
    
        let sectionsResult = todoist.request({
            // data: {};
            "method": "GET",
            parameters: {
                "project_id": projectID
            },
            "url": "https://api.todoist.com/rest/v1/sections"
        });
    
    
        if (sectionsResult.error) {
            context.fail(result.error)
            return true
        }
        let sectionData = sectionsResult.responseData;
        let content = [];
        content.push("## " + projectName);
        //    content.push("");
        
        let sectionIDs = [];
        
        if (sectionData.length > 0) {
            // get tasks from every section
            for (section of sectionData) {
    
                let tasks = todoist.getTasks({
                    "project_id": projectID
                });
                content.push("");
                content.push("### " + section.name)
                content.push("");
                sectionIDs.push(section.id)
                for (let task of tasks) {
    					
                    if (task.section_id == section.id) {
                        let s = "- [ ] " + task.content;
                        if (task.due && task.due.string) {
                            s = s + " (due - " + task.due.string + ")";
                        }
                        content.push(s);
                    }
                }
    
            }
    
        }
    
    
        // tasks without section
    let tasks = todoist.getTasks({
                    "project_id": projectID
                });
                content.push("");
                content.push("### NO SECTION")
                content.push("");
    
        for (let task of tasks) {
     			if(!sectionIDs.includes(task.section_id)){   
            let s = "- [ ] " + task.content;
            if (task.due && task.due.string) {
                s = s + " (due - " + task.due.string + ")";
            }
            content.push(s);
            }
        }
    
        let d = Draft.create();
        d.content = content.join("\n");
        d.update();
        editor.load(d);
        return true;
    }
    
    if (!f()) {
        context.fail();
    }

Options

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