2

I want to add a line to the google sheet document with python. But for some reason it doesn't work I always find this error:


Traceback (most recent call last):
  File "D:\Utente\Desktop\BLS-Bonus-Nov-2019-v2\spreadsheet.py", line 27, in <module>
    sheet.add_rows(row,1)
TypeError: add_rows() takes 2 positional arguments but 3 were given

i am using this code:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint

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("GymBot_database.json", scope)

client = gspread.authorize(creds)

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

row = sheet.row_values(2)  # Get a specific row
insertRow = ["hello", 5, "red", "blue"]

sheet.add_rows(row,1)# Insert the list as a row at index 1

this code is not really mine I copied it from this link, to do some tests. But in my case it doesn't work and I don't know why.

  • I see a typo in your code, ```add_rows``` takes ```insertRow``` as input not ``row``. Check the link again. – sushanth May 26 '20 at 18:43

3 Answers3

1

If you look at the documentation (https://gspread.readthedocs.io/en/latest/api.html#gspread.models.Worksheet.add_rows)

add_rows(rows)

Adds rows to worksheet. Parameters: rows (int) – Number of new rows to add.

add_rows(rows) adds a specified number of empty rows at the bottom of the sheet. I think what you want instead is to insert values into the sheet. You can use append_row (https://gspread.readthedocs.io/en/latest/api.html#gspread.models.Worksheet.append_row).

e.g.

sheet.append_row(insertRow, table_range="A1")
Jason Chen
  • 194
  • 1
  • 5
1

Try this to append. sheet.append_row(insertRow)

ppxx
  • 177
  • 1
  • 11
0

You need to use insertRow as your parameter for the function add_rows. You used row as your parameter. Try changing that.

Jack Cummins
  • 41
  • 1
  • 1
  • 8