def test_backup(): print('setup_function: Retrieve OAuth2.0 credentials.') creds = sheetsync.ia_credentials_helper( CLIENT_ID, CLIENT_SECRET, credentials_cache_file='credentials.json', cache_key='default') print('Open a spreadsheet, back it up to named file.') target = sheetsync.Sheet(credentials=creds, document_key=SHEET_TO_BE_BACKED_UP, worksheet_name='Simpsons', key_column_headers=['Character'], header_row_ix=1) backup_name = 'backup test: %s' % int(time.time()) backup_key = target.backup(backup_name, folder_name="sheetsync backups") backup_sheet = sheetsync.Sheet(credentials=creds, document_key=backup_key, worksheet_name='Simpsons', key_column_headers=['Character'], header_row_ix=1) backup_data = backup_sheet.data() assert "Bart Simpson" in backup_data assert backup_data["Bart Simpson"]["Voice actor"] == "Nancy Cartwright" print('teardown_function Delete test spreadsheet') backup_sheet.drive_service.files().delete( fileId=backup_sheet.document_key).execute()
def test_the_kartik_test(): # The most basic usage of creating a new sheet and adding data to it. # From April, Google defaults to using new-style sheets which requires # workarounds right now. creds = sheetsync.ia_credentials_helper( CLIENT_ID, CLIENT_SECRET, credentials_cache_file='credentials.json', cache_key='default') new_doc_name = '%s-%s-%s' % (__name__, sys._getframe().f_code.co_name, int(time.time())) target = sheetsync.Sheet(credentials=creds, document_name=new_doc_name) # Check we can sync data to the newly created sheet. data = { "1": { "name": "Gordon", "color": "Green" }, "2": { "name": "Thomas", "color": "Blue" } } target.sync(data) retrieved_data = target.data() assert "1" in retrieved_data assert retrieved_data["1"]["name"] == "Gordon" assert "2" in retrieved_data assert retrieved_data["2"]["color"] == "Blue" assert retrieved_data["2"]["Key"] == "2" # Try opening the doc with a new instance (thereby guessing key columns) test_read = sheetsync.Sheet(credentials=creds, document_name=new_doc_name) retrieved_data_2 = test_read.data() assert "1" in retrieved_data_2 assert "2" in retrieved_data_2 assert retrieved_data["2"]["color"] == "Blue" # Delete the doc target.drive_service.files().delete(fileId=target.document_key).execute()
def test_move_to_folder_by_name(): creds = sheetsync.ia_credentials_helper( CLIENT_ID, CLIENT_SECRET, credentials_cache_file='credentials.json', cache_key='default') new_doc_name = '%s-%s-%s' % (__name__, sys._getframe().f_code.co_name, int(time.time())) target = sheetsync.Sheet(credentials=creds, document_name=new_doc_name, worksheet_name='Sheet1', folder_name=TESTS_FOLDER_NAME) # Delete the doc target.drive_service.files().delete(fileId=target.document_key).execute()
def test_move_to_new_folder_by_name(): creds = sheetsync.ia_credentials_helper( CLIENT_ID, CLIENT_SECRET, credentials_cache_file='credentials.json', cache_key='default') new_doc_name = '%s-%s-%s' % (__name__, sys._getframe().f_code.co_name, int(time.time())) new_folder_name = 'sheetsync testrun %s' % int(time.time()) target = sheetsync.Sheet(credentials=creds, document_name=new_doc_name, worksheet_name='Sheet1', folder_name=new_folder_name) # Delete the doc target.drive_service.files().delete(fileId=target.document_key).execute() # Delete the new folder too.. assert new_folder_name == target.folder['title'] target.drive_service.files().delete(fileId=target.folder['id']).execute()
def setup_function(function): global target print('setup_function: Retrieve OAuth2.0 credentials.') creds = sheetsync.ia_credentials_helper( CLIENT_ID, CLIENT_SECRET, credentials_cache_file='credentials.json', cache_key='default') print('setup_function: Create test spreadsheet.') # Copy the template spreadsheet into the prescribed folder. new_doc_name = '%s %s' % (__name__, int(time.time())) target = sheetsync.Sheet(creds, document_name=new_doc_name, worksheet_name="Arsenal", folder_key=TESTS_FOLDER, template_key=TEMPLATE_DOC, key_column_headers=["No."], header_row_ix=2, formula_ref_row_ix=1)
start = calaisresults.find('--><!--') + 7 end = calaisresults.find('--><Open', start) calaistrimmed = calaisresults[start:end] print( "----------- Calais processing completed; results returned and trimmed" ) ############################# Begin the SheetSync process (https://sheetsync.readthedocs.io/en/latest/) print("Starting SheetSync injection to Google Spreadsheet") import sheetsync gUsername = "******" ### enter your gmail/Google Sheets email address before running this script key = ntpath.basename(filePath) target = sheetsync.Sheet( username=gUsername, password= "******", ### enter your gmail/Google Sheets password before running this script (this may need to a an application-specific password if two-factor authentication is enabled on the Google account your's using) document_name=spreadsheet_name, key_column_headers=["File Name"]) ### creates 'key' variable for use later, when it must be added to create a "dictionary within a dictionary" key = '{"' + key + '": ' ### removing spaces, adding quotes, and line breaks sNospace = '":'.join(calaisresults[start:end].split(": ", -1)) sCollon = ': "'.join(sNospace.split(":", -1)) sComma = ', '.join(sCollon.split(",", -1)) sRquotespace = '"'.join(sComma.split(' "', -1)) sLastquote = """"},}""".join(sRquotespace.rsplit(',', 1)) sLbracket = '\n{"'.join(sLastquote.split("\n", 1)) sEndquote = '" \n, '.join(sLbracket.split(", \n", -1)) sBeginquote = ', "'.join(sEndquote.split("\n, ", -1)) sFormatted = sBeginquote