Action

Todoist: Import Project Comments with Date

Posted by FlohGro, Last update over 3 years ago - Unlisted

Steps

  • script

    // Prompt to select Todoist project and labels
    // Draft as task in Todoist
    // script from @TDK_SA90 modified by @FlohGro
    // original Action: https://actions.getdrafts.com/a/1fI
    
    
    // set this to false if you don't want an additional delimiter after each comment (since the date headers are also delimitting the comments
    let printDelimiter = true
    let commentDelimiter = "---"
    
    const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    ];
    
    
    // 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 Comments");
        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 comments = todoist.getComments({
            "project_id": projectID
        });
        if (!comments) {
            alert("No comments to import.");
            return false;
        }
    
        let content = [];
        content.push("## " + projectName);
        content.push("");
    
        let isFirstComment = true
    
        for (let comment of comments) {
            let date = new Date(comment.posted)
            //        let formattedDate = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes()
            let timeString = date.toLocaleString('en-US', {
                hour: 'numeric',
                minute: 'numeric',
                hour12: true
            })
    
            let formattedDate = date.getDate() + " " + monthNames[date.getMonth()] + " " + timeString
    
    
    
            if (printDelimiter) {
                if (!isFirstComment) {
                    content.push(commentDelimiter)
                    content.push("")
                }
            }
            // date as md header in layer 4 header
            content.push("#### " + formattedDate)
            // newline after date
            content.push("")
    
    
            content.push("- " + comment.content)
            content.push("")
            isFirstComment = false
    
        }
    
        let d = Draft.create();
        d.content = content.join("\n");
        d.update();
        editor.load(d);
        return true;
    }
    
    if (!f()) {
        context.fail();
    }

Options

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