Взаимодействие с Google Таблицами с использованием примеров кода

Для программного взаимодействия с Google Sheets вы можете использовать Google Sheets API. Вот несколько методов, которые вы можете использовать вместе с примерами кода для выполнения различных операций в Google Sheet:

  1. Чтение данных из листа:

    import gspread
    from oauth2client.service_account import ServiceAccountCredentials
    # Set up credentials
    scope = ['https://spreadsheets.google.com/feeds']
    credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
    client = gspread.authorize(credentials)
    # Open the spreadsheet and select a worksheet
    spreadsheet = client.open('Your Spreadsheet')
    worksheet = spreadsheet.worksheet('Sheet1')
    # Read all values from a range
    values = worksheet.get_all_values()
    print(values)
    # Read a specific cell value
    cell_value = worksheet.acell('A1').value
    print(cell_value)
  2. Запись данных на лист:

    import gspread
    from oauth2client.service_account import ServiceAccountCredentials
    # Set up credentials (same as above)
    # Open the spreadsheet and select a worksheet
    spreadsheet = client.open('Your Spreadsheet')
    worksheet = spreadsheet.worksheet('Sheet1')
    # Update a single cell
    worksheet.update('A1', 'New Value')
    # Update a range of cells
    cell_list = worksheet.range('A1:C3')
    for cell in cell_list:
    cell.value = 'New Value'
    worksheet.update_cells(cell_list)
  3. Добавление нового листа:

    import gspread
    from oauth2client.service_account import ServiceAccountCredentials
    # Set up credentials (same as above)
    # Open the spreadsheet and add a new sheet
    spreadsheet = client.open('Your Spreadsheet')
    new_worksheet = spreadsheet.add_worksheet(title='New Sheet', rows='100', cols='20')
  4. Удаление листа:

    import gspread
    from oauth2client.service_account import ServiceAccountCredentials
    # Set up credentials (same as above)
    # Open the spreadsheet and delete a sheet
    spreadsheet = client.open('Your Spreadsheet')
    worksheet = spreadsheet.worksheet('Sheet1')
    spreadsheet.del_worksheet(worksheet)
  5. Форматирование ячеек:

    import gspread
    from oauth2client.service_account import ServiceAccountCredentials
    # Set up credentials (same as above)
    # Open the spreadsheet and select a worksheet
    spreadsheet = client.open('Your Spreadsheet')
    worksheet = spreadsheet.worksheet('Sheet1')
    # Apply bold formatting to a range of cells
    cell_format = {
    "textFormat": {
    "bold": True
    }
    }
    worksheet.format('A1:B2', cell_format)