-2

I'm writing a Tkinter with gspread app. I believe the connections to the spreadsheets are made properly because i can read data off it. I'm using python 2.7.15 and gspread 0.6.2. I get the error. If i leave out the 'RAW' argument at the end of the function call, I no longer get any errors, but nothing gets written to the spreadsheet.

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1550, in __call__
    return self.func(*args)
  File "app.py", line 22, in clicked
    sheet.insert_row(insertRow,index,'RAW')
TypeError: insert_row() takes at most 3 arguments (4 given)

import gspread
from oauth2client.service_account import ServiceAccountCredentials
from Tkinter import *

scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]

creds = ServiceAccountCredentials.from_json_keyfile_name("client_secret.json", scope)

client = gspread.authorize(creds)

sheet = client.open("This").sheet1  # Open the spreadhseet

#data = sheet.get_all_records()  # Get a list of all records

#row = sheet.row_values(3)  # Get a specific row
#col = sheet.col_values(3)  # Get a specific column
#cell = sheet.cell(1,2).value  # Get the value of a specific cell
def clicked():
    index = sheet.row_count
    index+=1
    insertRow = [nametxt.get(),placetxt.get(), phonetxt.get()]
    sheet.insert_row(insertRow,index,'RAW')
window = Tk()
window.title("Registration application")
window.geometry('700x700')
namelbl = Label(window, text="Name",font=("Ubuntu",20))
placelbl = Label(window, text="Place", font=("Ubuntu",20))
phonelbl = Label(window,text="Phone No", font=("Ubuntu",20))
placetxt = Entry(window,width = 20)
nametxt = Entry(window,width=20)
phonetxt = Entry(window,width = 20)
namelbl.grid(column=1, row=1,)
nametxt.grid(column=2, row=1)
placelbl.grid(column=1, row=2)
placetxt.grid(column=2,row=2)
phonelbl.grid(column =1, row=3)
phonetxt.grid(column = 2,row=3)
button = Button(window, text="submit",command=clicked)
button.grid(column=2, row=5)
window.mainloop()

#sheet.update_cell(2,2, "CHANGED")  # Update one cell
  • 5
    `sheet` is one of the arguments (its implicitly given as the first argument). Double check the documention of that method to see what arguments you should be giving it. – Carcigenicate Jan 06 '20 at 15:22
  • Looking at the [documentation](https://gspread.readthedocs.io/en/latest/api.html#gspread.models.Spreadsheet), it's not clear what the problem here is. `Worksheet.insert_row` is defined with 3 parameters. – chepner Jan 06 '20 at 15:34
  • Maybe it's an outdated installed library? Try updating gspread. – Carcigenicate Jan 06 '20 at 15:37
  • Try `sheet.insert_row(insertRow, index, value_input_option='RAW')`. – acw1668 Jan 06 '20 at 15:40
  • @acw1668 that just gives me an error saying value_input_option is an undefined identifier – Arun Parolikkal Jan 06 '20 at 15:41
  • @Carcigenicate I know this is an old version of gspread, but that's because thenew version has a bug. It doesn't always connect to the spreadsheet. Shows an authentication error. https://stackoverflow.com/questions/49258566/gspread-authentication-throwing-insufficient-permission?rq=1 only the third answer solved that problem for me – Arun Parolikkal Jan 06 '20 at 15:45
  • And is the down vote really necessary, people? – Arun Parolikkal Jan 06 '20 at 15:46
  • 1
    @ArunParolikkal: From the [source `0.6.2`](https://github.com/burnash/gspread/blob/v0.6.2/gspread/models.py#634): `def insert_row(self, values, index=1):`. Therefore you have to use: `sheet.insert_row(insertRow, index)` – stovfl Jan 06 '20 at 16:31
  • @ArunParolikkal: Relevant [append-row-with-gspread](https://stackoverflow.com/questions/31248734/insert-row-or-append-row-with-gspread-python) – stovfl Jan 06 '20 at 17:31

1 Answers1

0

You're using an old version of gspread. In the version you're using the definition of insert_row looks like this:

def insert_row(self, values, index=1):

Notice how it takes only three arguments: self (automatically passed when called on an instance), along with values and index. It doesn't accept any other parameters. You need to remove the RAW argument for your code to work with this version of the library.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • This gets rid if the error but doesn't add columns in the spreadsheet. the spreadsheet does say,though that an edit was made a few seconds ago by anonymous – Arun Parolikkal Jan 06 '20 at 17:15