简体   繁体   English

如何使用python GSPREAD找到谷歌电子表格的第一行空行?

[英]How to find the first empty row of a google spread sheet using python GSPREAD?

I am struggling to write codes that find me the first empty row of a google sheet.我正在努力编写代码,让我找到谷歌工作表的第一行空行。

I am using gspread package from github.com/burnash/gspread我正在使用来自 github.com/burnash/gspread 的 gspread 包

I would be glad if someone can help :)如果有人可以提供帮助,我会很高兴:)

I currently have just imported modules and opened the worksheet我目前刚刚导入了模块并打开了工作表

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')

I want to find row 1158 which is the first empty row of the worksheet with a function, which means everytime the old empty row is filled, it will find the next empty row See here我想找到第 1158 行,它是带有函数的工作表的第一个空行,这意味着每次填充旧的空行时,它都会找到下一个空行见这里

I solved this using:我使用以下方法解决了这个问题:

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)

This alternative method resolves issues with the accepted answer by accounting for rows that may have skipped values (such as fancy header sections in a document) as well as sampling the first N columns:这种替代方法通过考虑可能跳过值的行(例如文档中的花哨标题部分)以及对前N列进行采样来解决已接受答案的问题:

def next_available_row(sheet, cols_to_sample=2):
  # looks for empty row based on values appearing in 1st N columns
  cols = sheet.range(1, 1, sheet.row_count, cols_to_sample)
  return max([cell.row for cell in cols if cell.value]) + 1

If you can count on all of your previous rows being filled in:如果您可以指望之前填写的所有行:

len(sheet.get_all_values()) + 1

will give you the first free row会给你第一个免费行

get_all_values returns a 2D list of the sheet's data. get_all_values返回工作表数据的二维列表。 Each nested list is a row, so the length of the 2D list is the number of rows that has any data.每个嵌套列表都是一行,因此 2D 列表的长度是包含任何数据的行数。

Similar problem is first free column:类似的问题是第一个空闲列:

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)
def find_empty_cell():
    alphabet = list(map(chr, range(65, 91)))
    for letter in alphabet[0:1]: #look only at column A and B
        for x in range(1, 1000):
            cell_coord = letter+ str(x)
            if wks.acell(cell_coord).value == "":
                return(cell_coord)

I use this kinda sloppy function to find the first empty cell.我使用这个有点草率的函数来查找第一个空单元格。 I can't find an empty row because the other columns already have values.我找不到空行,因为其他列已经有了值。

Oh, and there are some issues between 2.7 and 3.6 with mapping that required me to turn the alphabet into a string.哦,在 2.7 和 3.6 之间存在一些映射问题,需要我将字母表转换为字符串。

import pygsheets        
gc = pygsheets.authorize(service_file='************************.json')
ss = gc.open('enterprise_finance')
ws = ss[0]
row_count = len(ws.get_all_records()) + 2
ws.set_dataframe(raw_output,(row_count,1), copy_index = 'TRUE', copy_head = 'TRUE')
ws.delete_rows(row_count , number=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM