2

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

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

I'm 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 spreadsheet

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.

1
  • I see a typo in your code, add_rows takes insertRow as input not row. Check the link again. Commented May 26, 2020 at 18:43

3 Answers 3

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")
Sign up to request clarification or add additional context in comments.

1 Comment

This is outdated now
1

Try this to append. sheet.append_row(insertRow)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.