Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python GSPREAD查找google电子表格的第一个空行?_Python_Python 2.7_Gspread - Fatal编程技术网

如何使用python GSPREAD查找google电子表格的第一个空行?

如何使用python GSPREAD查找google电子表格的第一个空行?,python,python-2.7,gspread,Python,Python 2.7,Gspread,我正在努力编写代码,让我在谷歌工作表中找到第一个空行 我正在使用来自github.com/burnash/gspread的gspread包 如果有人能帮忙,我会很高兴:) 我目前刚刚导入模块并打开了工作表 scope = ['https://spreadsheets.google.com/feeds'] credentials = ServiceAccountCredentials.from_json_keyfile_name('ddddd-61d0b758772b.json', scope)

我正在努力编写代码,让我在谷歌工作表中找到第一个空行

我正在使用来自github.com/burnash/gspread的gspread包

如果有人能帮忙,我会很高兴:)

我目前刚刚导入模块并打开了工作表

scope = ['https://spreadsheets.google.com/feeds']

credentials = ServiceAccountCredentials.from_json_keyfile_name('ddddd-61d0b758772b.json', scope)

gc = gspread.authorize(credentials)

sheet = gc.open("Event Discovery")
ws = sheet.worksheet('Event Discovery')
我想查找第1158行,它是工作表中带有函数的第一个空行,这意味着每次填充旧的空行时,它都会找到下一个空行 我用以下方法解决了这个问题:

def next_available_row(worksheet):
    str_list = list(filter(None, worksheet.col_values(1)))
    return str(len(str_list)+1)

scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('auth.json', scope)
gc = gspread.authorize(credentials)
worksheet = gc.open("sheet name").sheet1
next_row = next_available_row(worksheet)

#insert on the next available row

worksheet.update_acell("A{}".format(next_row), somevar)
worksheet.update_acell("B{}".format(next_row), somevar2)
我用这个草率的函数来查找第一个空单元格。我找不到空行,因为其他列已经有值


哦,2.7和3.6之间的映射有一些问题,需要我将字母表转换成字符串

此替代方法通过考虑可能具有跳过值的行(例如文档中的花式标题部分)以及对前N列进行采样,解决了接受答案的问题:

def next_available__行(表,列至样本=2):
#根据出现在前N列中的值查找空行
cols=表范围(1,1,表行计数,cols到样本)
返回最大值([cell.row for cell in cols if cell.value])+1

如果您可以指望前面的所有行都被填写:

len(sheet.get_all_values())+1

我会给你第一排免费的

get_all_value
返回图纸数据的二维列表。每个嵌套列表都是一行,因此二维列表的长度是包含任何数据的行数

类似的问题是第一个自由柱:

from xlsxwriter.utility import xl_col_to_name
# Square 2D list, doesn't matter which row len you check
column_count = len(sheet.get_all_values()[0]) 
column = xl_col_to_name(column_count)
导入pygsheets
gc=pygsheets.authorize(服务文件='***************************.json')
ss=gc.open(“企业融资”)
ws=ss[0]
行计数=len(ws.get_all_records())+2
ws.set_数据帧(原始_输出,(行计数,1),复制_索引='TRUE',复制_头='TRUE')
ws.delete_行(行计数,数字=1)

让我们看看你到目前为止有什么。我不是这个意思。显示一些代码遍历行,并尝试检查是否有空的行。嗨,Alex,我不知道如何编写代码,因为这是我刚找到的一个新包,我还不太了解它,然后您将不得不查看一些文档。您链接的github页面看起来很有用。试试
get\u all\u values
方法。谢谢Alex,我会看看的!在Python 3中,将'list()'添加到'next\u available\u row'函数中:
str\u list=list(filter(None,sheet.col\u values(1))
@PedroLobito这是一个很好的解决方案!我已经用了好几个月了,但是突然它坏了:它现在在下一行之前插入了一行。。知道为什么会发生这种事吗?@jed这对我来说很好。但在您的情况下,为什么不将行号增加1呢?
from xlsxwriter.utility import xl_col_to_name
# Square 2D list, doesn't matter which row len you check
column_count = len(sheet.get_all_values()[0]) 
column = xl_col_to_name(column_count)