Action

Google Task: Import List

Posted by agiletortoise, Last update over 3 years ago

Example code for working with Google Tasks via script. See scripting reference for details on available methods.

This action prompts user to select one of their Google Task lists, then imports the tasks from that list and builds a new draft with a Markdown list representation of the tasks.

Steps

  • script

    // Demo script
    // Prompts user to select from the Google Tasks lists
    // And builds new Markdown list from the active tasks
    
    let f = () => {
    	// create GoogleTask object
    	let gtask = GoogleTask.create();
    	// get all lists from account
    	let lists = gtask.getLists();
    	if (!lists || lists.length == 0) {
    		alert("No lists to import");
    		return false;
    	}
    
    	// prompt to select a list
    	let p = Prompt.create();
    	p.title = "Select List";
    	for(let list of lists) {
    		p.addButton(list.title, list);
    	}	
    	if (!p.show()) { return false; }
    
    	// get tasks from the selected list
    	let selectedList = p.buttonPressed;
    	let tasks = gtask.getTasks(selectedList);
    	if (!tasks || tasks.length == 0) {
    		alert(`No tasks found in list "${selectedList.title}"`)
    		return false;
    	}
    
    	// build an array of strings with task values
    	let result = [];
    	result.push(`# ${selectedList.title}
    
    `);
    
    	// separate subtasks from root level tasks
    	// both will be in list
    	let parentTasks = [];
    	let subTasks = [];
    	for (let task of tasks) {
    		if (task.parent) {
    			subTasks.push(task);
    		}
    		else {
    			parentTasks.push(task);
    		}
    	}
    
    	// loop over parent tasks and build list
    	for (let task of parentTasks) {
    		result.push(`- [ ] ${task.title}
    `);
    		for (let subTask of subTasks) {
    			if (subTask.parent == task.id) {
    				result.push(`    - [ ] ${subTask.title}
    `);
    			}
    		}
    	}
    
    	// put the results in new draft and load it
    	let d = new Draft();
    	d.content = result.join('');
    	d.update();
    	editor.load(d);
    	return true;
    }
    
    if(!f()) {
    	context.cancel();
    }

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.