Skip to content

Instantly share code, notes, and snippets.

@matthewboman
Created February 13, 2019 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewboman/e61932178465c1e1a2327e9b0b707851 to your computer and use it in GitHub Desktop.
Save matthewboman/e61932178465c1e1a2327e9b0b707851 to your computer and use it in GitHub Desktop.
Add tasks from Google spreadsheet to Todoist
/**
* Assign column numbers according to your spreadsheet
*/
var clientCol = 0
var dueDateCol = 1
var typeCol = 2
var descriptionCol = 3
/**
* Todoist info
*
* Project's name must match "type" in spreadsheet. Create a Todoist project for each additional type
*/
var URL = "https://todoist.com/api/v7/sync"
var API_TOKEN = "" // Add your API key
var SYNC_TOKEN = "*" // not sure wtf this is but saw it on Stack Overflow
var projects = [
{ project_id: 2205495750, name: "design" }, // these IDs are examples
{ project_id: 2205504091, name: "SEO" } // get the ID from your project's URL
]
/**
* Creates Todoist task for last row in spreadsheet
*
* Assign `addLastRow` to a button in the sheet
*/
function addLastRow() {
// get data
var sheet = SpreadsheetApp.getActiveSheet()
var data = sheet.getDataRange().getValues()
var row = data[data.length - 1]
// format and POST row
var commands = rowToTask(row)
postToDo(commands)
}
/**
* Creates Todoist task for specified row in spreadsheet
*
* Assign `addSpecificRow` to a button in the spreadsheet.
* There isn't a way to pass parameters from a button, so it must be set w/in the function below.
*/
function addSpecificRow() {
var rowToPost = 1 // here is where you set the row
// get data
var sheet = SpreadsheetApp.getActiveSheet()
var data = sheet.getDataRange().getValues()
var row = data[rowToPost]
// format and POST row
var commands = rowToTask(row)
postToDo(commands)
}
/**
* POST todos to Todoist
*
* @param {Array) - the commands to call on Todoist
*/
function postToDo(commands) {
var payload = {
"token": API_TOKEN,
"sync_token": SYNC_TOKEN,
"resource_types": '["projects", "items"]',
"commands": commands
}
var options = {
"method": "POST",
"payload": payload
}
var response = UrlFetchApp.fetch(URL, options)
Logger.log(response)
}
/**
* Formats spreadsheet data to Todoist task
*
* @param {Array} - the row of the spreadsheet to be formatted
*/
function rowToTask(row) {
var client = row[clientCol]
var due_date_utc = new Date(row[dueDateCol])
var desc = row[descriptionCol]
var type = row[typeCol]
var project_id = projects.filter(function(project) { return project.name == type })[0].project_id
var task = {
"content": client + " - " + desc,
"due_date_utc": due_date_utc,
"project_id": project_id
}
// this must be array for Todoist
var command = [
{
"type": "item_add",
"temp_id": randomID(),
"uuid": randomID(),
"args": task
}
]
return JSON.stringify(command)
}
/**
* Generates random ID for "temp_id" && "uuid"
*/
function randomID() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1)
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment