当我在gspread中使用col_count时,我得到了Google表格的总列数,即使只有三列包含数据,也包含行。如果我可以获得包含数据的列数和行数,那将会更有用。有没有办法做到这一点?
答案 0 :(得分:1)
这对我来说似乎也是一个问题。 但对我来说,这些效果很好
gc=gspread.authorize(cred)
sh=gc.open('test').sheet1
a=len(sh.row_values(1))
print('column count',end=' ')
print(a)
答案 1 :(得分:1)
row_count和col_count函数将捕获所有活动的行/列,无论它们是否保存数据。
两个解决方法: 1.建立您自己的功能来检查是否存在上述用户user222216的数据(我没有检查它是否起作用)
获取所有数据并进行计数例如:
max_rows = lens(worksheet.get_all_values()) #this is a list of list of all data and the length is equal to the number of rows including header row if it exists in data set
max_cols = len(worksheet.get_all_values()[0]) #this will count the items in the first row, and assumes the first row is either header or has no empty cells
答案 2 :(得分:0)
ADutta为单行提供了解决方案,它在该答案上进行了扩展,以显示整个非空白工作表的大小(假设连续有非空白行)。
gc = gspread.authorize(credentials)
wks = gc.open('test1').sheet1
print ('total rows in worksheet',wks.row_count) # worksheet rows including empty rows
print ('total cols in worksheet',wks.col_count) # worksheet cols including empty cols
max_cols = 0
for non_empty_row_num in range(1,wks.row_count): # step thru non-empty rows
cols_in_row = len(wks.row_values(non_empty_row_num)) # number of cols in this non-empty row
# print (non_empty_row_num,cols_in_row)
if cols_in_row > max_cols: max_cols = cols_in_row
if cols_in_row == 0: # only process if not empty
print (non_empty_row_num,max_cols)
break # stop getting new rows at first empty row
print (len(wks.get_all_values())) # just the non-empty row count