upsertTaskFromRow(listId, {title, notes, due, taskId}) creates a Google Task, or patches one when taskId is set, and returns the task id so you can write it back to the sheet. Enable the Tasks advanced service first. Google Tasks keeps only the date of a due date, not the time.
When to use
- Turning a to-do tab into Tasks without creating a duplicate every time you re-run
- Updating title, notes, or due date on a task you already created
- Writing the returned id into a Task ID column on the same row
How to use this snippet
In the Apps Script editor, add the Tasks service (Services → Tasks API). listId is the task list's id (Tasks.Tasklists.list() will show your lists). title is required. notes and due are optional. due can be a Date or a yyyy-MM-dd string; the helper sends midnight UTC on that civil date in the script timezone so an evening timestamp does not roll into the next day.
/**
* Create a task on listId, or patch it when opts.taskId is set.
* Returns the task id. title is required. notes and due are optional.
* Due is stored as a date only (Tasks drops the time). Enable the
* Tasks advanced service first. Throws if the service is missing,
* or if listId or title is empty.
*
* @param {string} listId
* @param {{title: string, notes?: string, due?: Date|string, taskId?: string}} opts
* @return {string}
*/
function upsertTaskFromRow(listId, opts) {
opts = opts || {};
var list = String(listId == null ? '' : listId).trim();
if (!list) throw new Error('upsertTaskFromRow: listId required');
if (typeof Tasks === 'undefined' || !Tasks || !Tasks.Tasks) {
throw new Error(
'upsertTaskFromRow: enable the Tasks advanced service (Services → Tasks API)'
);
}
var title = String(opts.title == null ? '' : opts.title).trim();
if (!title) throw new Error('upsertTaskFromRow: title required');
var resource = { title: title };
if (opts.notes != null && String(opts.notes).trim() !== '') {
resource.notes = String(opts.notes);
}
if (opts.due != null && opts.due !== '') {
resource.due = toTasksDue_(opts.due);
}
var taskId = String(opts.taskId == null ? '' : opts.taskId).trim();
var result;
if (taskId) {
result = Tasks.Tasks.patch(resource, list, taskId);
} else {
result = Tasks.Tasks.insert(resource, list);
}
return result && result.id ? result.id : '';
}
function toTasksDue_(due) {
// Tasks stores only the calendar date, not the time. Send YYYY-MM-DDT00:00:00.000Z
// using the civil date in the script timezone so a local evening Date does not
// roll over to the next UTC day.
var y, mo, d, s, m, dt;
if (Object.prototype.toString.call(due) === '[object Date]') {
if (isNaN(due.getTime())) throw new Error('upsertTaskFromRow: invalid due date');
y = due.getFullYear();
mo = due.getMonth() + 1;
d = due.getDate();
} else {
s = String(due).trim();
m = s.match(/^(\d{4})-(\d{2})-(\d{2})/);
if (m) {
y = Number(m[1]);
mo = Number(m[2]);
d = Number(m[3]);
} else {
dt = new Date(s);
if (isNaN(dt.getTime())) throw new Error('upsertTaskFromRow: invalid due date');
y = dt.getFullYear();
mo = dt.getMonth() + 1;
d = dt.getDate();
}
}
function pad_(n) {
return n < 10 ? '0' + n : String(n);
}
return y + '-' + pad_(mo) + '-' + pad_(d) + 'T00:00:00.000Z';
}
Example
function syncTodosToTasks() {
var listId = 'YOUR_TASKLIST_ID';
var sheet = SpreadsheetApp.getActive().getSheetByName('Todos');
var last = sheet.getLastRow();
if (last < 2) return;
// A title, B notes, C due, D taskId
var values = sheet.getRange(2, 1, last - 1, 4).getValues();
var ids = [];
for (var i = 0; i < values.length; i++) {
var title = String(values[i][0] || '').trim();
if (!title) {
ids.push([values[i][3] || '']);
continue;
}
var id = upsertTaskFromRow(listId, {
title: title,
notes: values[i][1],
due: values[i][2],
taskId: values[i][3]
});
ids.push([id]);
}
sheet.getRange(2, 4, ids.length, 1).setValues(ids);
}
Use getValues() for the due column so date cells arrive as Date objects. Re-running the function patches rows that already have an id and creates the rest.
Notes
- A missing Tasks service throws a clear error asking you to enable it. A bad
taskIdsurfaces as the API error — the helper does not silently create a second task. - The script needs the
https://www.googleapis.com/auth/tasksscope, which the advanced service adds when you enable it. - This helper does not complete, delete, or move tasks to another list.
Happy Coding!
