通过 PyPi 的 Asana API 模块将 Asana 任务导出到 CSV

Exporting Asana tasks to CSV via PyPi's Asana API module

我需要将工作区特定项目中的所有 Asana 任务导出到 csv 文件。我一直通过 Asana 的高级搜索手动执行此操作,但 Asana 生成的 csv 文件仅支持 < 2000 个条目,这不再适合我的需要。以下代码(我在网上找到并稍作修改)几乎可以满足我的要求,除了我需要一个额外的“标签”列(就像 Asana 生成的 csv 文件一样)显示每个任务标签的逗号分隔列表。我需要标签的名称而不是标签 ID。我有 Python 的基本知识,但我没有 API 经验,所以这确实超出了我的能力范围。如果有人能提供帮助,我将不胜感激。这是代码的相关部分:

import sys
import csv
import asana

def process_project_tasks(client, project, ws_dict):
    """Add each task for the current project to the records list."""
    task_list = []
    while True:
        tasks = client.tasks.find_by_project(project['id'] 
{"opt_fields":"name, projects, workspace, id, due_on, created_at, 
modified_at, completed, completed_at, assignee, assignee_status, parent, 
notes"})

        for task in tasks:
            ws_name = ws_dict[task['workspace']['id']]
            assignee = task['assignee']['id'] if task['assignee'] is not 
None else ''
            created_at = task['created_at'][0:10] + ' ' + 
task['created_at'][11:16] if \
                    task['created_at'] is not None else None
            modified_at = task['modified_at'][0:10] + ' ' + 
task['modified_at'][11:16] if \
                    task['modified_at'] is not None else None
            completed_at = task['completed_at'][0:10] + ' ' + 
task['completed_at'][11:16] if \
                task['completed_at'] is not None else None
            rec = [task['name'], project['name'], ws_name,task['due_on'], 
created_at, \
                modified_at, task['completed'], completed_at, assignee, \
                task['assignee_status'], task['parent'], task['notes'], 
task['id']]
            rec = ['' if s is None else s for s in rec]
            task_list.append(rec)
        if 'next_page' not in tasks:
            break
    return task_list

我弄清楚了如何添加标签。您只需添加 'tags' 作为 'opt_fields' 之一。如果您只是这样做,则无法获取标签的名称,因此您需要将 'opt_fields' 更改为 'opt_expand',然后创建每个任务的标签名称的逗号分隔列表。

def process_project_tasks(client, project, ws_dict):
    """Add each task for the current project to the records list."""

    while True:
        tasks = client.tasks.find_by_project(project['gid'], {"opt_expand":"name, \
            projects, workspace, gid, due_on, created_at, modified_at, completed, \
            completed_at, assignee, assignee_status, parent, notes, tags"})

        for task in tasks:
            ws_name = ws_dict[task['workspace']['gid']]

            #get a comma-separated list of the names of each tag
            tags = task['tags']
            if tags is not None:
                tagname=''
                i=0
                for tag in tags:
                    if i==0:
                        tagname = tag['name']
                    else:
                        tagname = tagname + ', ' + tag['name']
                    i=i+1
            assignee = task['assignee']['gid'] if task['assignee'] is not None else 
''
            created_at = task['created_at'][0:10] + ' ' + task['created_at'][11:16] 
if \
                    task['created_at'] is not None else None
            modified_at = task['modified_at'][0:10] + ' ' + task['modified_at'] 
   [11:16] if \
                    task['modified_at'] is not None else None
            completed_at = task['completed_at'][0:10] + ' ' + task['completed_at'] 
   [11:16] if \
                task['completed_at'] is not None else None
            rec = [task['name'], project['name'], ws_name, task['due_on'], 
created_at, \
                modified_at, task['completed'], completed_at, assignee, \
                task['assignee_status'], task['parent'], task['notes'], task['gid'], 
tags]
            rec = ['' if s is None else s for s in rec]
            task_list.append(rec)
        if 'next_page' not in tasks:
            break
   return task_list