You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Google Apps Script删除任务失败:Task not found错误排查

Google Tasks API 删除任务报错:Task not found

我在Google Apps Script编辑器(script.google.com)开发项目时,尝试删除任务列表中的任务,持续收到错误提示:

API call to tasks.tasks.delete failed with error: Task not found.

我的删除任务函数代码如下:

function deleteTask(){
taskid = 'ajM2UDZIZGctaXJnOTJjZg';
tasklist = 'MDk2MzE1ODgxNDkyNDE1NTI3MDA6MDow';
try {
      // Call insert method with taskDetails and taskListId to insert Task to specified tasklist.
      Tasks.Tasks.remove(taskid, taskListId);
      // Print the Task ID of created task.
      console.log('Task with ID "%s" was deleted.', taskid);
   } catch (err) {
      // TODO (developer) - Handle exception from Tasks.move() of Task API
     console.log('Failed to move task with an error: %s', err.message);
   }
}

奇怪的是,用官方API测试工具可以成功删除任务,但自己的代码就是不行。我是JavaScript新手,推测问题很简单但找不到原因,恳请帮助。另外,我用来获取任务列表ID和任务ID的代码运行正常:

const taskLists = Tasks.Tasklists.list();
    // If taskLists are available then print all tasklists.
    if (!taskLists.items) {
      console.log('No task lists found.');
      return;
    }
    // Print the tasklist title and tasklist id.
    for (let i = 0; i < taskLists.items.length; i++) {
      const taskList = taskLists.items[i];
      //console.log('Task list with title Tasks&quot; and ID &quot;%s&quot; was found.', taskList.title, taskList.id);
      console.log(taskList);
    }

问题原因及解决方法

你的代码有两个明显错误:

  1. 变量名大小写不匹配:你定义的任务列表ID变量是tasklist,但调用API时用的是TaskListId——JavaScript是区分大小写的,这会导致传入未定义的变量值。
  2. API参数顺序错误:Tasks.Tasks.remove()的正确参数顺序是(任务列表ID, 任务ID),你把任务ID和任务列表ID的顺序搞反了。

修正后的代码:

function deleteTask(){
  const taskid = 'ajM2UDZIZGctaXJnOTJjZg';
  const tasklistId = 'MDk2MzE1ODgxNDkyNDE1NTI3MDA6MDow'; // 统一变量名,用const声明更规范
  try {
    Tasks.Tasks.remove(tasklistId, taskid); // 调整参数顺序
    console.log('Task with ID "%s" was deleted.', taskid);
  } catch (err) {
    console.log('Failed to delete task with an error: %s', err.message); // 修正注释和错误提示文本
  }
}

另外建议用const声明变量,避免全局变量污染,代码更严谨。

内容的提问来源于stack exchange,提问作者Ryan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 10:46:24