Skip to content

Commit 4e891b4

Browse files
google-labs-jules[bot]jpoehnelt
authored andcommitted
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.
1 parent 83b1049 commit 4e891b4

File tree

2 files changed

+85
-47
lines changed

2 files changed

+85
-47
lines changed

tasks/quickstart/quickstart.gs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,31 @@
1616
// [START tasks_quickstart]
1717
/**
1818
* Lists the user's tasks.
19+
* @return {void}
1920
* @see https://developers.google.com/tasks/reference/rest/v1/tasklists/list
2021
*/
2122
function listTaskLists() {
2223
const optionalArgs = {
23-
maxResults: 10
24+
maxResults: 10,
2425
};
25-
try {
26-
// Returns all the authenticated user's task lists.
27-
const response = Tasks.Tasklists.list(optionalArgs);
28-
const taskLists = response.items;
29-
// Print task list of user if available.
30-
if (!taskLists || taskLists.length === 0) {
31-
console.log('No task lists found.');
32-
return;
33-
}
34-
for (const taskList of taskLists) {
35-
console.log('%s (%s)', taskList.title, taskList.id);
36-
}
37-
} catch (err) {
38-
// TODO (developer) - Handle exception from Task API
39-
console.log('Failed with error %s', err.message);
26+
27+
if (!Tasks.Tasklists) {
28+
// This check is necessary to prevent type errors. In a real application,
29+
// the Tasks service should be enabled in the Apps Script project.
30+
console.log(
31+
'Tasks API has an issue. Please enable it in your Apps Script project.');
32+
return;
33+
}
34+
// Returns all the authenticated user's task lists.
35+
const response = Tasks.Tasklists.list(optionalArgs);
36+
const taskLists = response.items;
37+
// Print task list of user if available.
38+
if (!taskLists || taskLists.length === 0) {
39+
console.log('No task lists found.');
40+
return;
41+
}
42+
for (const taskList of taskLists) {
43+
console.log('%s (%s)', taskList.title, taskList.id);
4044
}
4145
}
4246
// [END tasks_quickstart]

tasks/simpleTasks/simpleTasks.gs

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@
1414
* limitations under the License.
1515
*/
1616

17+
/**
18+
* @typedef {Object} TaskInfo
19+
* @property {string} id
20+
* @property {string} title
21+
* @property {string} notes
22+
* @property {boolean} completed
23+
*/
24+
25+
/**
26+
* @typedef {object} HtmlOutput
27+
*/
28+
29+
/**
30+
* @typedef {Object} TaskListInfo
31+
* @property {string} id
32+
* @property {string} name
33+
*/
34+
1735
/**
1836
* Special function that handles HTTP GET requests to the published web app.
1937
* @return {HtmlOutput} The HTML page to be served.
@@ -25,66 +43,82 @@ function doGet() {
2543

2644
/**
2745
* Returns the ID and name of every task list in the user's account.
28-
* @return {Array.<Object>} The task list data.
46+
* @return {TaskListInfo[]} The task list data.
2947
*/
3048
function getTaskLists() {
31-
var taskLists = Tasks.Tasklists.list().getItems();
49+
if (!Tasks.Tasklists) {
50+
return [];
51+
}
52+
const taskLists = Tasks.Tasklists.list().items;
3253
if (!taskLists) {
3354
return [];
3455
}
35-
return taskLists.map(function(taskList) {
36-
return {
37-
id: taskList.getId(),
38-
name: taskList.getTitle()
39-
};
40-
});
56+
const result = [];
57+
for (const taskList of taskLists) {
58+
if (taskList.id && taskList.title) {
59+
result.push({id: taskList.id, name: taskList.title});
60+
}
61+
}
62+
return result;
4163
}
4264

4365
/**
4466
* Returns information about the tasks within a given task list.
45-
* @param {String} taskListId The ID of the task list.
46-
* @return {Array.<Object>} The task data.
67+
* @param {string} taskListId The ID of the task list.
68+
* @return {TaskInfo[]} The task data.
4769
*/
4870
function getTasks(taskListId) {
49-
var tasks = Tasks.Tasks.list(taskListId).getItems();
71+
if (!Tasks.Tasks) {
72+
return [];
73+
}
74+
const tasks = Tasks.Tasks.list(taskListId).items;
5075
if (!tasks) {
5176
return [];
5277
}
53-
return tasks.map(function(task) {
54-
return {
55-
id: task.getId(),
56-
title: task.getTitle(),
57-
notes: task.getNotes(),
58-
completed: Boolean(task.getCompleted())
59-
};
60-
}).filter(function(task) {
61-
return task.title;
62-
});
78+
const result = [];
79+
for (const task of tasks) {
80+
if (task.id && task.title) {
81+
result.push({
82+
id: task.id,
83+
title: task.title,
84+
notes: task.notes ?? '',
85+
completed: Boolean(task.completed),
86+
});
87+
}
88+
}
89+
return result;
6390
}
6491

6592
/**
6693
* Sets the completed status of a given task.
67-
* @param {String} taskListId The ID of the task list.
68-
* @param {String} taskId The ID of the task.
69-
* @param {Boolean} completed True if the task should be marked as complete, false otherwise.
94+
* @param {string} taskListId The ID of the task list.
95+
* @param {string} taskId The ID of the task.
96+
* @param {boolean} completed True if the task should be marked as complete, false otherwise.
7097
*/
7198
function setCompleted(taskListId, taskId, completed) {
72-
var task = Tasks.newTask();
99+
const task = Tasks.newTask();
73100
if (completed) {
74-
task.setStatus('completed');
101+
task.status = 'completed';
75102
} else {
76-
task.setStatus('needsAction');
77-
task.setCompleted(null);
103+
task.status = 'needsAction';
104+
(/** @type {any} */ (task)).completed = null;
105+
}
106+
if (!Tasks.Tasks) {
107+
return;
78108
}
79109
Tasks.Tasks.patch(task, taskListId, taskId);
80110
}
81111

82112
/**
83113
* Adds a new task to the task list.
84-
* @param {String} taskListId The ID of the task list.
85-
* @param {String} title The title of the new task.
114+
* @param {string} taskListId The ID of the task list.
115+
* @param {string} title The title of the new task.
86116
*/
87117
function addTask(taskListId, title) {
88-
var task = Tasks.newTask().setTitle(title);
118+
const task = Tasks.newTask();
119+
task.title = title;
120+
if (!Tasks.Tasks) {
121+
return;
122+
}
89123
Tasks.Tasks.insert(task, taskListId);
90124
}

0 commit comments

Comments
 (0)