SpaceTurtle0
SpaceTurtle0

Reputation: 161

Python - Displaying the first 3 rows in a spreadsheet

so right now I have a python script that displays everything in a spreadsheet. As the data grew larger, it was hard to look at everything at once. Is there a way to make it display 3 rows every time I input "NEXT"?

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]

creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)

client = gspread.authorize(creds)

sheet = client.open("Discord Ban Data").sheet1

userinput = input("Go to the next rows?: ")
if userinput == "NEXT":
  #display next 3 rows here

So far this is what I have, any tips or suggestions would help greatly!

Upvotes: 0

Views: 86

Answers (1)

Harshana
Harshana

Reputation: 5486

First, you have to keep the currentIndex as a variable. Then, try getting all the values of the sheet as a list by using the .get_all_values()` method. After that, you can index the values by using the list indexes but be aware to handle about index range errors.

Sample code:

current_index = 0
values = sheet.get_all_values()

userinput = input("Go to the next rows?: ")
while userinput == "NEXT":
   print(values[current_index:current_index+3])
   current_index+=3

Upvotes: 1

Related Questions