|
@@ -0,0 +1,168 @@
|
|
|
|
|
+import json
|
|
|
|
|
+
|
|
|
|
|
+import gspread
|
|
|
|
|
+from gspread.models import Cell
|
|
|
|
|
+
|
|
|
|
|
+from oauth2client.service_account import ServiceAccountCredentials
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# docs: https://github.com/burnash/gspread
|
|
|
|
|
+# https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/sheets#top_of_page
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def get_sheet(key, require_write):
|
|
|
|
|
+ scopes = [
|
|
|
|
|
+ 'https://spreadsheets.google.com/feeds',
|
|
|
|
|
+ 'https://www.googleapis.com/auth/drive',
|
|
|
|
|
+ ]
|
|
|
|
|
+ auth = ServiceAccountCredentials.from_json_keyfile_name('gcp-auth.json', scopes)
|
|
|
|
|
+ gc = gspread.authorize(auth)
|
|
|
|
|
+ sheet = gc.open_by_key(key)
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ permissions = sheet.list_permissions()
|
|
|
|
|
+ if require_write and not [p for p in permissions if p.get('emailAddress') == _get_gcp_user() and p['role'] == 'writer']:
|
|
|
|
|
+ print 'Access control: Bad role. Please ensure that %s has regular write access.' % _get_gcp_user()
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
|
|
+ return sheet
|
|
|
|
|
+ except gspread.exceptions.APIError, e:
|
|
|
|
|
+ error = json.loads(str(e))['error']
|
|
|
|
|
+ if error['status'] == 'PERMISSION_DENIED':
|
|
|
|
|
+ print 'Access control: Please share the document with %s (write permissions).' % _get_gcp_user()
|
|
|
|
|
+ return None
|
|
|
|
|
+ else:
|
|
|
|
|
+ raise
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def _get_gcp_user():
|
|
|
|
|
+ with open('gcp-auth.json') as f:
|
|
|
|
|
+ return json.loads(f.read())['client_email']
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def validate_sheet(sheet):
|
|
|
|
|
+ try:
|
|
|
|
|
+ sheet.worksheet('RT config')
|
|
|
|
|
+ sheet.worksheet('RT eval')
|
|
|
|
|
+ except gspread.exceptions.WorksheetNotFound, e:
|
|
|
|
|
+ print 'Sheet not found: "%s"' % e
|
|
|
|
|
+ return False
|
|
|
|
|
+
|
|
|
|
|
+ return True
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def get_worksheets(sheet):
|
|
|
|
|
+ return (
|
|
|
|
|
+ sheet.worksheet('RT config'),
|
|
|
|
|
+ sheet.worksheet('RT eval'),
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def _color(r, g, b):
|
|
|
|
|
+ return {
|
|
|
|
|
+ 'red': r,
|
|
|
|
|
+ 'green': g,
|
|
|
|
|
+ 'blue': b,
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def _test():
|
|
|
|
|
+ sheet = get_sheet('1JMP9QsXdPbvIVL01LFRAMqkMQZRL17gs_ZjKFBJjI8E', True)
|
|
|
|
|
+ if not sheet:
|
|
|
|
|
+ return
|
|
|
|
|
+
|
|
|
|
|
+ # print 'valid: %s' % validate_sheet(sheet)
|
|
|
|
|
+
|
|
|
|
|
+ ws_config, ws_eval = get_worksheets(sheet)
|
|
|
|
|
+
|
|
|
|
|
+ # data = ws_config.get_all_values()
|
|
|
|
|
+
|
|
|
|
|
+ # cells = []
|
|
|
|
|
+ # for i, row in enumerate(data):
|
|
|
|
|
+ # for j, value in enumerate(row):
|
|
|
|
|
+ # if value:
|
|
|
|
|
+ # cells.append(gspread.models.Cell(i+1, j+1, value))
|
|
|
|
|
+
|
|
|
|
|
+ # ws_eval.update_cells(cells[:10])
|
|
|
|
|
+
|
|
|
|
|
+ cells = []
|
|
|
|
|
+ cells.append(Cell(1, 1, 'SOME TEXT'))
|
|
|
|
|
+ for i in range(50):
|
|
|
|
|
+ cells.append(gspread.models.Cell(3, i+1, i))
|
|
|
|
|
+ ws_eval.update_cells(cells)
|
|
|
|
|
+
|
|
|
|
|
+ print sheet.batch_update({
|
|
|
|
|
+ 'requests': [{
|
|
|
|
|
+ 'updateDimensionProperties': {
|
|
|
|
|
+ 'range': {
|
|
|
|
|
+ 'sheetId': ws_eval.id,
|
|
|
|
|
+ 'dimension': 'COLUMNS',
|
|
|
|
|
+ 'startIndex': 0,
|
|
|
|
|
+ 'endIndex': 1,
|
|
|
|
|
+ },
|
|
|
|
|
+ 'properties': {
|
|
|
|
|
+ 'pixelSize': 200,
|
|
|
|
|
+ },
|
|
|
|
|
+ 'fields': 'pixelSize',
|
|
|
|
|
+ },
|
|
|
|
|
+ }, {
|
|
|
|
|
+ 'updateDimensionProperties': {
|
|
|
|
|
+ 'range': {
|
|
|
|
|
+ 'sheetId': ws_eval.id,
|
|
|
|
|
+ 'dimension': 'COLUMNS',
|
|
|
|
|
+ 'startIndex': 1,
|
|
|
|
|
+ 'endIndex': 50,
|
|
|
|
|
+ },
|
|
|
|
|
+ 'properties': {
|
|
|
|
|
+ 'pixelSize': 21,
|
|
|
|
|
+ },
|
|
|
|
|
+ 'fields': 'pixelSize',
|
|
|
|
|
+ },
|
|
|
|
|
+ }],
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ print sheet.batch_update({
|
|
|
|
|
+ 'requests': [{
|
|
|
|
|
+ 'updateDimensionProperties': {
|
|
|
|
|
+ 'range': {
|
|
|
|
|
+ 'sheetId': ws_eval.id,
|
|
|
|
|
+ 'dimension': 'ROWS',
|
|
|
|
|
+ 'startIndex': 0,
|
|
|
|
|
+ 'endIndex': 1000,
|
|
|
|
|
+ },
|
|
|
|
|
+ 'properties': {
|
|
|
|
|
+ 'pixelSize': 21,
|
|
|
|
|
+ },
|
|
|
|
|
+ 'fields': 'pixelSize',
|
|
|
|
|
+ },
|
|
|
|
|
+ }],
|
|
|
|
|
+ })
|
|
|
|
|
+ import random
|
|
|
|
|
+
|
|
|
|
|
+ xs = []
|
|
|
|
|
+ for i in range(50):
|
|
|
|
|
+ xs.append({
|
|
|
|
|
+ 'userEnteredFormat': {
|
|
|
|
|
+ 'backgroundColor': _color(1, i/50., i/50.)
|
|
|
|
|
+ # 'backgroundColor': _color(random.random(), random.random(), random.random())
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ print sheet.batch_update({
|
|
|
|
|
+ 'requests': [{
|
|
|
|
|
+ 'updateCells': {
|
|
|
|
|
+ 'rows': [{
|
|
|
|
|
+ 'values': xs,
|
|
|
|
|
+ }],
|
|
|
|
|
+ 'fields': 'userEnteredFormat.backgroundColor',
|
|
|
|
|
+ 'start': {
|
|
|
|
|
+ 'sheetId': ws_eval.id,
|
|
|
|
|
+ 'rowIndex': 0,
|
|
|
|
|
+ 'columnIndex': 0,
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }]
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+# _test()
|