Action

Duplicate Todoist Project

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

Basic Duplication of a Todoist Project

Steps

  • script

    let strProjectTemplate = "General";
    let strProjectNew = "Packing";
    
    
  • script

    //Dump template project into a variable
    
    let strProjectTemp = "";
    
    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; 
    	}
    
    	// add projects
    	let projectLookup = mapIds(projects, "name", "id");
    	let projectID = projectLookup[strProjectTemplate];
    
    	let tasks = todoist.getTasks({
    		"project_id": projectID
    	});
    	if (!tasks) { 
    		alert("No tasks to import.");
    		return false;
    	}
    	
    	let content = [];
    	content.push("## " + strProjectTemplate);
    	content.push("");
    	
    	for (let task of tasks) {
    		content.push("- " + task.content);
    	}
    
    	strProjectTemp = content.join("\n");
    	return true;
    }
    
    if (!f()) {
    	context.fail();
    }
    
    
  • script

    //Create a new project based on the project variable (strProjectTemp) content
    
    
    //Initialise
    let todoist = Todoist.create();
    let ctErrors = 0;
    
    //Create the project
    todoist.createProject({"name" : strProjectNew});
    
    //Process Lines
    let lines = strProjectTemp.split("\n");
    for (let line of lines) {
    	if (line.length > 0 && !line.startsWith("#")) {
    		let success = todoist.quickAdd(line + " #" + strProjectNew);
    		if (success) {
    			console.log("Todoist task created: " + line);
    		}
    		else {
    			ctErrors++;
    			console.log("Todoist error: " + todoist.lastError);
    		}
    	}
    }
    
    if (ctErrors > 0) {
    	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.