I am new to coding and have put together some code to reference Google Sheet values to create Google Tasks. However, when I run the script it doesn't seem to check the existing task title and dueDate and creates duplicate tasks. I don't know if I coded the task IF statement wrong or missed something in the section of the code with my comment.
/// This code doesn't seem to validate Task Title and dueDate but creates duplicate tasks
if (tasks[i].getTitle() == 'taskTitle' && tasks[i].getdue() == 'startDate') {
return;
}
function CreateTask() {
const ID = '1MkimspZROYFPFApQECWHETIwMkWfQ5aizj0Da17cVhs';
const sheet = SpreadsheetApp.openById(ID).getSheetByName('Database');
var TASK_LIST_ID = '@default';
var index = 2; // This indicates start looking at row 2
var LastRow = sheet.getLastRow();
for (;index<=LastRow; index++){
var taskTitle = sheet.getRange(index,2,1,1).getValue();
var startDate = sheet.getRange(index,1,1,1).getValue();
var checkOnMe = sheet.getRange(index,7,1,1).getValue();
var status = sheet.getRange(index,8,1,1).getValue();
if (checkOnMe == "Yes" && taskTitle && startDate && status != "Complete")
{
/// This code doesn't seem to validate Task Title and dueDate but creates a
duplicate tasks
var tasks = Tasks.Tasks.list('MDk4MjA3ODcxNzU1OTc3NjQxOTk6MDow').getItems();
for (var i = 0; i < tasks.length; i++) {
if (tasks[i].getTitle() == 'taskTitle' && tasks[i].getdue() == 'startDate') {
return;
}
}
var task = Tasks.newTask();{
task.title = sheet.getRange(index,2,1,1).getValue();
task.notes = 'Checking In';
}
var dueDate = sheet.getRange(index,1,1,1).getValue();
dueDate.setDate(dueDate.getDate() + 0);
task.due = dueDate.toISOString();
var newTask = Tasks.Tasks.insert(task, TASK_LIST_ID);
console.log('Task with title = %s, id = %s and notes = %s was created. ' +
'Task is due on %s.',
newTask.title, newTask.id, newTask.notes, newTask.due);
}
}
}