python 中是否有一种方法可以从 google sheet 中计算一系列行(从第一行数据到最后一行数据)?

Is there a way in python to count a range of rows (from the first row with data to the last row with data) from a google sheet?

我目前是一名实习生,正在研究从 google 电子表格中提取数据并从该数据创建 DITA 文件的代码。我发现的一个问题是 .row_count 方法计算电子表格中的所有行;我只需要计算电子表格中最后一个填充行(包括)的一系列行。

我尝试删除电子表格中多余的行,但这不是计算行数的可行解决方案。

rowCount = sheet.row_count

paragraph = ET.SubElement(conbody, "p")
table = ET.SubElement(conbody, "table")
tgroup = ET.SubElement(table, "tgroup", attrib={"cols": 
str(rowCount)})
tbody = ET.SubElement(tgroup, "tbody")

i = 0 
while i < rowCount:
    row = ET.SubElement(tbody,"row")
    i += 1

当我 运行 我的 while 循环时,我最终得到 1001 行,而我实际上需要一系列行,这些行不包含最后一个填充行之后的所有空行。例如,我有一个 google 表,其中第 1、2 和 4 行填充了数据,第 3 行是空的。但是,我需要计算从第一个填充数据的行到最后一个填充数据的行的行数,即使中间的行是空的。

我可能必须使用 if 语句创建一个函数,但我不知道从哪里开始使用 google 工作表 api。

我从@rdt0086 的 post 中找到了解决我的问题的方法。我的问题的解决方案是获取所有数据并统计它:

rowCount = len(sheet.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 

这似乎是做什么的,正是我正在寻找的:它计算从填充数据的第一行(header 行)到填充数据的最后一行的一系列行。