Skip to content

Instantly share code, notes, and snippets.

@oojewale
Created May 13, 2020 07:25
Show Gist options
  • Save oojewale/9e714db19b3890bde8407e3d21adffc1 to your computer and use it in GitHub Desktop.
Save oojewale/9e714db19b3890bde8407e3d21adffc1 to your computer and use it in GitHub Desktop.
Update custom field on Asana task with information from Google sheet.
from __future__ import print_function
import pickle
import os.path
import string
import requests
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
# The ID and range of a sample spreadsheet.
SPREADSHEET_ID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
# starting cell & ending cell
RANGE_NAME = 'A2:B72'
# credentials
ASANA_API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
# Custom field
CUSTOM_FIELD_ID = 'XXXXXXXXXXXXXXXXXXXX'
def new_priority(value):
return 100 - int(value)
def update_on_asana(values = {}):
task_id = values['task_id']
# call asana update task api
url = "https://app.asana.com/api/1.0/tasks/{}".format(task_id)
print('Calling URL %s' % url)
resp = requests.put(
url,
json = {'data': values['fields']},
headers = {
'Content-Type':'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer {}'.format(ASANA_API_KEY)
}
)
print('Updated task_id %s and resp is %s' % (task_id, resp.content))
def update_priority(tasks = []):
for task in tasks:
update_on_asana({
'task_id': task['task_id'],
'fields': {
'custom_fields': {
CUSTOM_FIELD_ID: new_priority(task['priority'])
}
}
})
def main():
"""Shows basic usage of the Sheets API.
Prints values from a sample spreadsheet.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID,
range=RANGE_NAME).execute()
values = result.get('values', [])
if not values:
print('No data found.')
else:
tasks = []
for row in values:
# Grab columns A and B, which correspond to indices 0 and 1.
tasks.append({ 'task_id': row[0].replace("'", ''), 'priority': row[1] })
update_priority(tasks)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment