无法使用 Google Apps 脚本删除任务

问题描述 投票:0回答:1

我正在使用 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);
   }
}

有趣的是,当我使用 https://developers.google.com/tasks/reference/rest/v1/tasks/delete
有效
able to remove tasks from developer.google.com

对我可能做错了什么有什么想法吗?我知道这可能很简单,我对 javascript 还很陌生,这非常令人沮丧。任何帮助将不胜感激

我尝试使用以下代码来获取父列表ID和taskID。这似乎工作正常

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" and ID "%s" was found.', taskList.title, taskList.id);
      console.log(taskList);
    }
google-apps-script google-tasks-api google-tasks
1个回答
0
投票

修改要点:

  • 在您的显示脚本中,未声明
    taskListId
    。所以,我认为这可能是您当前问题的原因。
  • Tasks.Tasks.remove
    的参数分别是
    tasklist: string
    task: string
    。如果显示脚本中的
    taskid
    taskListId
    分别是任务 ID 和任务列表 ID 的值,则
    Tasks.Tasks.remove(taskid, taskListId);
    应为
    Tasks.Tasks.remove(taskListId, taskid);
    。我想这可能也是一个问题。

当这些要点反映在你的脚本中时,就会变成如下所示。

修改后的脚本:

function deleteTask() {
  const taskId = '###'; // Please set your task ID.
  const taskListId = '###'; // Please set your task list ID.
  try {
    Tasks.Tasks.remove(taskListId, taskId);
    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);
  }
}
  • 通过此修改,
    taskListId, taskId
    的任务被删除。

注:

  • 在此修改中,假设您的任务 ID 和任务列表 ID 是有效值。请注意这一点。

参考:

© www.soinside.com 2019 - 2024. All rights reserved.