From e0118865d1a5374ea9564f768e821ba6ebd63f5f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 21 Nov 2025 22:23:38 +0000 Subject: [PATCH 1/3] fix(tasks): resolve type errors - Replaced outdated Apps Script methods with modern property access. - Added JSDoc annotations to resolve type errors. - Introduced local typedefs for complex objects. - Added checks for undefined properties to improve robustness. - Removed unhelpful try-catch blocks. - Corrected logic in `setCompleted` to use `null` for clearing completion date. --- tasks/quickstart/quickstart.gs | 36 ++++++------ tasks/simpleTasks/simpleTasks.gs | 96 +++++++++++++++++++++----------- 2 files changed, 85 insertions(+), 47 deletions(-) diff --git a/tasks/quickstart/quickstart.gs b/tasks/quickstart/quickstart.gs index fae31d265..06c1bb94a 100644 --- a/tasks/quickstart/quickstart.gs +++ b/tasks/quickstart/quickstart.gs @@ -16,27 +16,31 @@ // [START tasks_quickstart] /** * Lists the user's tasks. + * @return {void} * @see https://developers.google.com/tasks/reference/rest/v1/tasklists/list */ function listTaskLists() { const optionalArgs = { - maxResults: 10 + maxResults: 10, }; - try { - // Returns all the authenticated user's task lists. - const response = Tasks.Tasklists.list(optionalArgs); - const taskLists = response.items; - // Print task list of user if available. - if (!taskLists || taskLists.length === 0) { - console.log('No task lists found.'); - return; - } - for (const taskList of taskLists) { - console.log('%s (%s)', taskList.title, taskList.id); - } - } catch (err) { - // TODO (developer) - Handle exception from Task API - console.log('Failed with error %s', err.message); + + if (!Tasks.Tasklists) { + // This check is necessary to prevent type errors. In a real application, + // the Tasks service should be enabled in the Apps Script project. + console.log( + 'Tasks API has an issue. Please enable it in your Apps Script project.'); + return; + } + // Returns all the authenticated user's task lists. + const response = Tasks.Tasklists.list(optionalArgs); + const taskLists = response.items; + // Print task list of user if available. + if (!taskLists || taskLists.length === 0) { + console.log('No task lists found.'); + return; + } + for (const taskList of taskLists) { + console.log('%s (%s)', taskList.title, taskList.id); } } // [END tasks_quickstart] diff --git a/tasks/simpleTasks/simpleTasks.gs b/tasks/simpleTasks/simpleTasks.gs index 1843ccc9c..278880429 100644 --- a/tasks/simpleTasks/simpleTasks.gs +++ b/tasks/simpleTasks/simpleTasks.gs @@ -14,6 +14,24 @@ * limitations under the License. */ +/** + * @typedef {Object} TaskInfo + * @property {string} id + * @property {string} title + * @property {string} notes + * @property {boolean} completed + */ + +/** + * @typedef {object} HtmlOutput + */ + +/** + * @typedef {Object} TaskListInfo + * @property {string} id + * @property {string} name + */ + /** * Special function that handles HTTP GET requests to the published web app. * @return {HtmlOutput} The HTML page to be served. @@ -25,66 +43,82 @@ function doGet() { /** * Returns the ID and name of every task list in the user's account. - * @return {Array.} The task list data. + * @return {TaskListInfo[]} The task list data. */ function getTaskLists() { - var taskLists = Tasks.Tasklists.list().getItems(); + if (!Tasks.Tasklists) { + return []; + } + const taskLists = Tasks.Tasklists.list().items; if (!taskLists) { return []; } - return taskLists.map(function(taskList) { - return { - id: taskList.getId(), - name: taskList.getTitle() - }; - }); + const result = []; + for (const taskList of taskLists) { + if (taskList.id && taskList.title) { + result.push({id: taskList.id, name: taskList.title}); + } + } + return result; } /** * Returns information about the tasks within a given task list. - * @param {String} taskListId The ID of the task list. - * @return {Array.} The task data. + * @param {string} taskListId The ID of the task list. + * @return {TaskInfo[]} The task data. */ function getTasks(taskListId) { - var tasks = Tasks.Tasks.list(taskListId).getItems(); + if (!Tasks.Tasks) { + return []; + } + const tasks = Tasks.Tasks.list(taskListId).items; if (!tasks) { return []; } - return tasks.map(function(task) { - return { - id: task.getId(), - title: task.getTitle(), - notes: task.getNotes(), - completed: Boolean(task.getCompleted()) - }; - }).filter(function(task) { - return task.title; - }); + const result = []; + for (const task of tasks) { + if (task.id && task.title) { + result.push({ + id: task.id, + title: task.title, + notes: task.notes ?? '', + completed: Boolean(task.completed), + }); + } + } + return result; } /** * Sets the completed status of a given task. - * @param {String} taskListId The ID of the task list. - * @param {String} taskId The ID of the task. - * @param {Boolean} completed True if the task should be marked as complete, false otherwise. + * @param {string} taskListId The ID of the task list. + * @param {string} taskId The ID of the task. + * @param {boolean} completed True if the task should be marked as complete, false otherwise. */ function setCompleted(taskListId, taskId, completed) { - var task = Tasks.newTask(); + const task = Tasks.newTask(); if (completed) { - task.setStatus('completed'); + task.status = 'completed'; } else { - task.setStatus('needsAction'); - task.setCompleted(null); + task.status = 'needsAction'; + (/** @type {any} */ (task)).completed = null; + } + if (!Tasks.Tasks) { + return; } Tasks.Tasks.patch(task, taskListId, taskId); } /** * Adds a new task to the task list. - * @param {String} taskListId The ID of the task list. - * @param {String} title The title of the new task. + * @param {string} taskListId The ID of the task list. + * @param {string} title The title of the new task. */ function addTask(taskListId, title) { - var task = Tasks.newTask().setTitle(title); + const task = Tasks.newTask(); + task.title = title; + if (!Tasks.Tasks) { + return; + } Tasks.Tasks.insert(task, taskListId); } From d834035d6f909c6ccf3c9d5fdfe6d47e3089475c Mon Sep 17 00:00:00 2001 From: Justin Poehnelt Date: Fri, 21 Nov 2025 16:39:33 -0700 Subject: [PATCH 2/3] feat: add reference documentation links to tasks/simpleTasks/simpleTasks.gs and update advanced service check in tasks/quickstart/quickstart.gs --- tasks/quickstart/quickstart.gs | 8 ++------ tasks/simpleTasks/simpleTasks.gs | 6 ++++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/tasks/quickstart/quickstart.gs b/tasks/quickstart/quickstart.gs index 06c1bb94a..0f3798d39 100644 --- a/tasks/quickstart/quickstart.gs +++ b/tasks/quickstart/quickstart.gs @@ -24,12 +24,8 @@ function listTaskLists() { maxResults: 10, }; - if (!Tasks.Tasklists) { - // This check is necessary to prevent type errors. In a real application, - // the Tasks service should be enabled in the Apps Script project. - console.log( - 'Tasks API has an issue. Please enable it in your Apps Script project.'); - return; + if (!Tasks) { + throw new Error('Enable the Tasks Advanced Service.'); } // Returns all the authenticated user's task lists. const response = Tasks.Tasklists.list(optionalArgs); diff --git a/tasks/simpleTasks/simpleTasks.gs b/tasks/simpleTasks/simpleTasks.gs index 278880429..4772200de 100644 --- a/tasks/simpleTasks/simpleTasks.gs +++ b/tasks/simpleTasks/simpleTasks.gs @@ -44,6 +44,7 @@ function doGet() { /** * Returns the ID and name of every task list in the user's account. * @return {TaskListInfo[]} The task list data. + * @see https://developers.google.com/workspace/tasks/reference/rest/v1/tasklists/list */ function getTaskLists() { if (!Tasks.Tasklists) { @@ -66,6 +67,7 @@ function getTaskLists() { * Returns information about the tasks within a given task list. * @param {string} taskListId The ID of the task list. * @return {TaskInfo[]} The task data. + * @see https://developers.google.com/workspace/tasks/reference/rest/v1/tasks/list */ function getTasks(taskListId) { if (!Tasks.Tasks) { @@ -94,6 +96,8 @@ function getTasks(taskListId) { * @param {string} taskListId The ID of the task list. * @param {string} taskId The ID of the task. * @param {boolean} completed True if the task should be marked as complete, false otherwise. + * @see https://developers.google.com/apps-script/advanced/tasks + * @see https://developers.google.com/workspace/tasks/reference/rest/v1/tasks/patch */ function setCompleted(taskListId, taskId, completed) { const task = Tasks.newTask(); @@ -113,6 +117,8 @@ function setCompleted(taskListId, taskId, completed) { * Adds a new task to the task list. * @param {string} taskListId The ID of the task list. * @param {string} title The title of the new task. + * @see https://developers.google.com/apps-script/advanced/tasks + * @see https://developers.google.com/workspace/tasks/reference/rest/v1/tasks/insert */ function addTask(taskListId, title) { const task = Tasks.newTask(); From 085587ab588104830f09001db3b2cc1a875a0b79 Mon Sep 17 00:00:00 2001 From: Justin Poehnelt Date: Fri, 21 Nov 2025 16:43:06 -0700 Subject: [PATCH 3/3] fix: improve Tasks service availability check to include `Tasks.Tasklists` --- tasks/quickstart/quickstart.gs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/quickstart/quickstart.gs b/tasks/quickstart/quickstart.gs index 0f3798d39..bb3c8d417 100644 --- a/tasks/quickstart/quickstart.gs +++ b/tasks/quickstart/quickstart.gs @@ -24,7 +24,7 @@ function listTaskLists() { maxResults: 10, }; - if (!Tasks) { + if (!Tasks || !Tasks.Tasklists) { throw new Error('Enable the Tasks Advanced Service.'); } // Returns all the authenticated user's task lists.