[Python] How do I set the range to change automatically in Google Sheets API?

Discussão em 'Python' iniciado por Stack, Fevereiro 11, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to use the Google sheets API to input data to the next blank cell. For example if A1 is blank, I want the API to insert the given info into A1. The next time info is given to the API, it should automatically add it to A2 instead of overwriting A1. Please advise

    from __future__ import print_function

    import os.path

    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    from googleapiclient.errors import HttpError


    from google.oauth2 import service_account

    SERVICE_ACCOUNT_FILE = secret_code
    SCOPES = ['https://www.googleapis.com/auth/spreadsheets']


    creds = None
    creds = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)


    # The ID and range of a sample spreadsheet.
    SAMPLE_SPREADSHEET_ID = 'Spreadsheet ID goes here'


    service = build('sheets', 'v4', credentials=creds)

    # Call the Sheets API
    sheet = service.spreadsheets()
    result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
    range="test!A1:G16").execute()

    specified = [[input("input info to add here:"), 4000],["4/4/2020", 3000]]

    request = sheet.values().update(spreadsheetId=SAMPLE_SPREADSHEET_ID,
    range="test!A1:F37", valueInputOption="USER_ENTERED", body={"values": specified}).execute()


    I tried to use a formula such as

    x="A",1+1


    however when it is run, a space is placed between the "A" and the number following it.

    Continue reading...

Compartilhe esta Página