Tasks 服務
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以在 Apps Script 中使用 Tasks 服務的 Google Tasks API。這項 API 可讓使用者在 Gmail 中管理工作。
參考資料
如要進一步瞭解這項服務,請參閱 Tasks API 的參考說明文件。與 Apps Script 中的所有進階服務一樣,Tasks 服務使用的物件、方法和參數都與公開 API 相同。詳情請參閱「如何判斷方法簽章」。
如要回報問題及尋求其他支援,請參閱 Tasks 支援指南。
應用程式範例
範例網路應用程式「Simple Tasks」示範如何使用 Tasks 服務執行讀取和寫入作業。您可以在 GitHub 存放區查看完整原始碼。
程式碼範例
下列程式碼範例使用 API 的第 1 版。
列出工作清單
這個範例會列出帳戶中的工作清單。
列出工作
這個範例會列出指定工作清單中的工作。
新增任務
這個範例會在工作清單中新增工作。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eThe Tasks service in Apps Script enables you to manage tasks in Gmail using the Google Tasks API.\u003c/p\u003e\n"],["\u003cp\u003eThis advanced service requires enabling before use and utilizes the same structure as the public Tasks API.\u003c/p\u003e\n"],["\u003cp\u003eSample code is provided to demonstrate common operations like listing task lists, listing tasks within a list, and adding new tasks.\u003c/p\u003e\n"],["\u003cp\u003eA simple tasks web application showcases read and write operations with the Tasks service and is available with full source code on GitHub.\u003c/p\u003e\n"],["\u003cp\u003eSupport and further information regarding the Tasks service can be found in the Tasks support guide and reference documentation.\u003c/p\u003e\n"]]],[],null,["# Tasks Service\n\nThe Tasks service allows you to use the\n[Google Tasks API](/tasks) in Apps Script. This API\ngives users the ability to manage their tasks in Gmail.\n| **Note:** This is an advanced service that must be [enabled before use](/apps-script/guides/services/advanced).\n\nReference\n---------\n\nFor detailed information on this service, see the\n[reference documentation](/tasks/v1/reference) for the Tasks API.\nLike all advanced services in Apps Script, the Tasks service uses the same\nobjects, methods, and parameters as the public API. For more information, see [How method signatures are determined](/apps-script/guides/services/advanced#how_method_signatures_are_determined).\n\nTo report issues and find other support, see the\n[Tasks support guide](/tasks/support).\n\nSample application\n------------------\n\nThe sample web application Simple Tasks demonstrates how to use the Tasks\nservice for both read and write operations. You can view the full source code\non our\n[GitHub repository](https://github.com/googleworkspace/apps-script-samples/tree/main/tasks/simpleTasks).\n\n[](https://github.com/googleworkspace/apps-script-samples/tree/main/tasks/simpleTasks)\n\nSample code\n-----------\n\nThe sample code below uses [version 1](/tasks/v1/reference) of\nthe API.\n\n### List task lists\n\nThis sample lists the task lists in your account. \nadvanced/tasks.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/tasks.gs) \n\n```javascript\n/**\n * Lists the titles and IDs of tasksList.\n * @see https://developers.google.com/tasks/reference/rest/v1/tasklists/list\n */\nfunction listTaskLists() {\n try {\n // Returns all the authenticated user's task lists.\n const taskLists = Tasks.Tasklists.list();\n // If taskLists are available then print all tasklists.\n if (!taskLists.items) {\n console.log('No task lists found.');\n return;\n }\n // Print the tasklist title and tasklist id.\n for (let i = 0; i \u003c taskLists.items.length; i++) {\n const taskList = taskLists.items[i];\n console.log('Task list with title \"%s\" and ID \"%s\" was found.', taskList.title, taskList.id);\n }\n } catch (err) {\n // TODO (developer) - Handle exception from Task API\n console.log('Failed with an error %s ', err.message);\n }\n}\n```\n\n### List tasks\n\nThis sample lists the tasks within a given task list. \nadvanced/tasks.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/tasks.gs) \n\n```javascript\n/**\n * Lists task items for a provided tasklist ID.\n * @param {string} taskListId The tasklist ID.\n * @see https://developers.google.com/tasks/reference/rest/v1/tasks/list\n */\nfunction listTasks(taskListId) {\n try {\n // List the task items of specified tasklist using taskList id.\n const tasks = Tasks.Tasks.list(taskListId);\n // If tasks are available then print all task of given tasklists.\n if (!tasks.items) {\n console.log('No tasks found.');\n return;\n }\n // Print the task title and task id of specified tasklist.\n for (let i = 0; i \u003c tasks.items.length; i++) {\n const task = tasks.items[i];\n console.log('Task with title \"%s\" and ID \"%s\" was found.', task.title, task.id);\n }\n } catch (err) {\n // TODO (developer) - Handle exception from Task API\n console.log('Failed with an error %s', err.message);\n }\n}\n```\n\n### Add task\n\nThis sample adds a new task to a task list. \nadvanced/tasks.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/tasks.gs) \n\n```javascript\n/**\n * Adds a task to a tasklist.\n * @param {string} taskListId The tasklist to add to.\n * @see https://developers.google.com/tasks/reference/rest/v1/tasks/insert\n */\nfunction addTask(taskListId) {\n // Task details with title and notes for inserting new task\n let task = {\n title: 'Pick up dry cleaning',\n notes: 'Remember to get this done!'\n };\n try {\n // Call insert method with taskDetails and taskListId to insert Task to specified tasklist.\n task = Tasks.Tasks.insert(task, taskListId);\n // Print the Task ID of created task.\n console.log('Task with ID \"%s\" was created.', task.id);\n } catch (err) {\n // TODO (developer) - Handle exception from Tasks.insert() of Task API\n console.log('Failed with an error %s', err.message);\n }\n}\n```"]]