Python - 显示电子表格的前 3 行

Python - Displaying the first 3 rows in a spreadsheet

所以现在我有一个 python 脚本可以在电子表格中显示所有内容。随着数据越来越大,很难一次查看所有内容。有没有办法让它每次输入“NEXT”时显示3行?

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

到目前为止,这就是我所拥有的,任何提示或建议都会有很大帮助!

首先,您必须将 currentIndex 保留为变量。然后,尝试使用 .get_all_values()` 方法将 sheet 的所有值作为列表获取。之后,您可以使用列表索引对值进行索引,但要注意处理索引范围错误。

示例代码:

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