Error when trying to import a CSV file to a live GoogleSheets: “DeprecationWarning: The order of arguments in worksheet.update() has changed”

This is my query:

import gspread
from google.oauth2.service_account import Credentials
import csv

# Path to your service account key file
SERVICE_ACCOUNT_FILE = '/Users/xxx/Desktop/projects/importcsv.json'

# Scopes for the API
SCOPES = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']

# Authorize the client
credentials = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
client = gspread.authorize(credentials)

# Name of the Google Sheets document
spreadsheet_name = 'test csv import'

# Open or create the Google Sheets document
try:
    spreadsheet = client.open(spreadsheet_name)
except gspread.exceptions.SpreadsheetNotFound:
    spreadsheet = client.create(spreadsheet_name)
    spreadsheet.share('xxxxx', perm_type='user', role='writer')

# Open the first sheet
sheet = spreadsheet.sheet1

# Clear the sheet before writing new data
sheet.clear()

# Path to your local CSV file
csv_file_path = '/Users/xxxxxx/Desktop/projects/test csv import - Sheet1.csv'

# Read the CSV file and upload to Google Sheets
with open(csv_file_path, mode='r') as file:
    reader = csv.reader(file)
    csv_data = list(reader)

# Convert the list of lists (csv_data) to the format expected by gspread (two-dimensional list)
values = [[cell] for row in csv_data for cell in row]

# Update the sheet with the CSV data starting from cell A1
sheet.update('A1', values)

print(f"CSV data has been uploaded to Google Sheets: {spreadsheet_name}")

The problem starts with line 40, “sheet.update(‘A1’, values)” where even tho the script seems to execute as I get the print “CSV data has been uploaded to Google Sheets” I get this error message

DeprecationWarning: The order of arguments in worksheet.update() has changed. Please pass values first and range_name secondor used named arguments (range_name=, values=)
  sheet.update('A1', values)

I’ve worked out how the file is read by the function but no luck, also I’ve formatted as per requested by the error message, but doing so gives me another error where it says

TypeError: update() got an unexpected keyword argument ‘range’`

And by eliminating this and only leaving the values section, then it says

TypeError: update() got multiple values for argument ‘values’`

Unsure if my approach is wrong altogether at the very end of the script, I think the other bits of connecting to the googlesheets, credentials keys and accesses are all correct, which conformts me, but at the same time im annoyed this update bit is blocking me altogether.

Any suggestions would be appreciated

Thanks

1

Either swap the order of the arguments, changing

sheet.update('A1', values)

to

sheet.update(values, 'A1')

or use keywords:

sheet.update(range_name='A1', values=values)

Your second error is because you used range= instead of range_name=.

1

Your script for uploading a CSV file to Google Sheets looks mostly correct, but there’s an issue with how you are formatting the data to update the sheet. heres coreectd code.

New contributor

Ravindu Yasanka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật