11"""
22Google Sheets to MagTag example: Naughty or Nice?
3- Gets JSON spreadsheet from Google, displays names from one column or other.
3+ Gets tab-separated-value (TSV) spreadsheet from Google, displays names from
4+ one column or other.
45"Smart cursive" font by Thomas A. Fine, helvB12 from Xorg fonts.
56"""
67
1314
1415# CONFIGURABLE SETTINGS and ONE-TIME INITIALIZATION ------------------------
1516
16- JSON_URL = 'https://spreadsheets .google.com/feeds/cells/1Tk943egFNDV7TmXGL_VspYyWKELeJO8gguAmNSgLDbk/1/public/full?alt=json '
17+ TSV_URL = 'https://docs .google.com/spreadsheets/d/e/2PACX-1vTA8pXQodbEiz5idGT21YkL1Vy8waW0aAHM1uX7D4TqBq6DrUU8qXVlON1QVaWSlmoC3OBL4Iokyiyy/pub?output=tsv '
1718NICE = True # Use 'True' for nice list, 'False' for naughty
1819TWELVE_HOUR = True # If set, show 12-hour vs 24-hour (e.g. 3:00 vs 15:00)
1920DD_MM = False # If set, show DD/MM instead of MM/DD dates
@@ -85,9 +86,9 @@ def hh_mm(time_struct, twelve_hour=True):
8586 print (NOW )
8687
8788 print ('Updating names' )
88- RESPONSE = MAGTAG .network .fetch (JSON_URL )
89+ RESPONSE = MAGTAG .network .fetch (TSV_URL )
8990 if RESPONSE .status_code == 200 :
90- JSON_DATA = RESPONSE .json ()
91+ TSV_DATA = RESPONSE .text
9192 print ('OK' )
9293
9394 # Set the "Updated" date and time label
@@ -98,26 +99,26 @@ def hh_mm(time_struct, twelve_hour=True):
9899 MAGTAG .set_text ('Updated %s %s' % (DATE , hh_mm (NOW , TWELVE_HOUR )), 1 ,
99100 auto_refresh = False )
100101
101- ENTRIES = JSON_DATA ['feed' ]['entry' ] # List of cell data
102+ # Split text response into separate lines
103+ LINES = TSV_DATA .split ('\r \n ' )
102104
103105 # Scan cells in row #1 to find the column number for naughty vs nice.
104106 # This allows the order of columns in the spreadsheet to be changed,
105107 # though they still must have a "Naughty" or "Nice" heading at top.
106- for entry in ENTRIES :
107- cell = entry ['gs$cell' ]
108- if int (cell ['row' ]) == 1 : # Only look at top row
109- head = cell ['$t' ].lower () # Case-insensitive compare
110- if ((NICE and head == 'nice' ) or (not NICE and head == 'naughty' )):
111- NAME_COLUMN = int (cell ['col' ])
108+ cells = LINES [0 ].split ("\t " ) # Tab-separated values
109+ for column , entry in enumerate (cells ):
110+ head = entry .lower () # Case-insensitive compare
111+ if ((NICE and head == 'nice' ) or (not NICE and head == 'naughty' )):
112+ NAME_COLUMN = column
112113
113114 # Now that we know which column number contains the names we want,
114115 # a second pass is made through all the cells. Items where row > 1
115116 # and column is equal to NAME_COLUMN are joined in a string.
116117 NAME_LIST = '' # Clear name list
117- for entry in ENTRIES :
118- cell = entry [ 'gs$cell' ]
119- if int ( cell [ 'row' ] ) > 1 and int ( cell [ 'col' ]) is NAME_COLUMN :
120- NAME_LIST += cell [ '$t' ] + '\n ' # Name + newline character
118+ for line in LINES [ 1 :]: # Skip first line -- naughty/nice/notes in sheet
119+ cells = line . split ( " \t " ) # Tab-separated
120+ if len ( cells ) >= NAME_COLUMN and cells [ NAME_COLUMN ] != "" :
121+ NAME_LIST += cells [ NAME_COLUMN ] + '\n ' # Name + newline character
121122
122123 MAGTAG .set_text (NAME_LIST ) # Update list on the display
123124
0 commit comments