2016-11-24 41 views
5

我正在努力編寫能找到我的第一個谷歌表單空行的代碼。如何使用python GSPREAD找到谷歌電子表格的第一個空行?

我使用gspread包從github.com/burnash/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,它是工作表中第一個帶有函數的空行,這意味着每次填充舊的空行時,都會找到下一個空行 See here

+0

向我們展示你到目前爲止所擁有的。 –

+0

這不是我的意思。顯示一些遍歷行的代碼並嘗試檢查一個空行。 –

+0

嗨亞歷克斯,我不知道如何編寫代碼,因爲這是一個新的包,我剛剛發現,我不知道它很好 –

回答

8

我設法實現這一目標的方法是:

def next_available_row(worksheet): 
    str_list = filter(None, worksheet.col_values(1)) # fastest 
    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) 
+1

這是一個很好的解決方案,謝謝佩德羅! –

+0

不客氣@Thành –

+2

在Python 3中,爲'next_available_row'的函數添加'list()':'''str_list = list(filter(None,sheet.col_values(1)))'''' – Hoenie

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

我用這個有點馬虎功能找到第一個空單元格。我找不到空行,因爲其他列已經有值。

哦,並且2.7和3.6之間有一些問題,需要我將字母轉換爲字符串。

相關問題