toggl-zei-py/toggl/toggl2gsuite.py

81 lines
2.8 KiB
Python
Raw Normal View History

2020-08-12 03:24:23 +02:00
import os
import unittest
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from toggl.TogglPy import Toggl
2020-08-16 00:00:08 +02:00
# this test demonstrates how to link up the toggl API into a google sheet
# in order to do this, you'll need to first setup your google account developer environment
# to do this, you can follow the instructions here: http://tinaja.computer/2017/10/27/gspread.html
# additional information about the spread API here: https://github.com/burnash/gspread
# as such, to run this test you'll need to define the following env variables
# TOGGL_API_KEY : your toggl api key
# WORKSPACE_ID: a workspace id that you'd like to dump data for
# KEYFILE: the full path to your google suite keyfile (keep this secret/safe!)
# SHEET_URL: the url of the google sheet you are writing to
2020-08-12 03:24:23 +02:00
class Toggl2GSuiteTest(unittest.TestCase):
def setUp(self):
2020-08-16 00:00:08 +02:00
self.api_key = os.environ["TOGGL_API_KEY"]
2020-08-12 03:24:23 +02:00
self.toggl = Toggl()
self.toggl.setAPIKey(self.api_key)
# see https://stackoverflow.com/questions/19153462/get-excel-style-column-names-from-column-number
2020-08-16 00:00:08 +02:00
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2020-08-12 03:24:23 +02:00
@staticmethod
def excel_style(row, col):
""" Convert given row and column number to an Excel-style cell name. """
result = []
while col:
col, rem = divmod(col - 1, 26)
result[:0] = Toggl2GSuiteTest.LETTERS[rem]
2020-08-16 00:00:08 +02:00
return "".join(result) + str(row)
2020-08-12 03:24:23 +02:00
def test_toggl2gsuite(self):
# have to do this year by year
data = {
2020-08-16 00:00:08 +02:00
"workspace_id": os.environ["WORKSPACE_ID"],
2020-08-12 03:24:23 +02:00
}
y = self.toggl.getDetailedReport(data)
credentials = ServiceAccountCredentials.from_json_keyfile_name(
2020-08-16 00:00:08 +02:00
os.environ["KEYFILE"], ["https://spreadsheets.google.com/feeds"]
)
2020-08-12 03:24:23 +02:00
client = gspread.authorize(credentials)
2020-08-16 00:00:08 +02:00
sheet = client.open_by_url(os.environ["SHEET_URL"])
2020-08-12 03:24:23 +02:00
worksheet = sheet.get_worksheet(0)
wrote_header = False
2020-08-16 00:00:08 +02:00
columns_to_write = [
"user",
"updated",
"start",
"end",
"client",
"project",
"description",
"is_billable",
"billable",
]
2020-08-12 03:24:23 +02:00
cell_row = 0
2020-08-16 00:00:08 +02:00
for row_idx, rec in enumerate(y["data"]):
2020-08-12 03:24:23 +02:00
if wrote_header == False:
for col_idx, header in enumerate(columns_to_write):
2020-08-16 00:00:08 +02:00
worksheet.update_acell(
Toggl2GSuiteTest.excel_style(row_idx + 1, col_idx + 1), header
)
2020-08-12 03:24:23 +02:00
wrote_header = True
for col_idx, header in enumerate(columns_to_write):
2020-08-16 00:00:08 +02:00
worksheet.update_acell(
Toggl2GSuiteTest.excel_style(row_idx + 2, col_idx + 1), rec[header]
)
2020-08-12 03:24:23 +02:00
2020-08-16 00:00:08 +02:00
if __name__ == "__main__":
2020-08-12 03:24:23 +02:00
unittest.main()