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

Question:

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.

Asked By: Cortex8938

||

Answers:

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()

# Get the values of the sheet
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
                                range="test!A1:G16").execute()

# Find the next blank row
values = result.get('values', [])
next_row = len(values) + 1

# Define the values to be added
specified = [[input("input info to add here:"), 4000],["4/4/2020", 3000]]

# Add the values to the sheet
request = sheet.values().append(spreadsheetId=SAMPLE_SPREADSHEET_ID,
                                range=f"test!A{next_row}:F{next_row}",
                                valueInputOption="USER_ENTERED",
                                body={"values": specified},
                                insertDataOption="INSERT_ROWS").execute()

The problem is that update and not append.

Answered By: Andreas
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.