nithinmurali / pygsheets

Google Sheets Python API v4
https://pygsheets.readthedocs.io/en/latest
Other
1.51k stars 220 forks source link

Create in folder error #518

Closed farabolaza closed 1 year ago

farabolaza commented 2 years ago

Error when creating a spreadsheet in folder (either by id or name) Have used this feature in the past, but now I can't get it to work. If I omit folder destination info everything work as expected. May be some change made by google in their dirve API that's causing this not to work.

To Reproduce Steps to reproduce the behavior:

myspsheet = gc.create(title='sometitle',folder='1w-1TkvKzjmurc-ofpdb9z77SnUHB-GDy')

# or
myspsheet = gc.create(title='sometitle',folder_name='my_existing_folder_name')

# the error I get

~\Anaconda3\lib\site-packages\pygsheets\client.py in create(self, title, template, folder, folder_name, **kwargs)
    122         if folder:
    123             self.drive.move_file(result['spreadsheetId'],
--> 124                                  old_folder=self.drive.spreadsheet_metadata(fid=result['spreadsheetId'])[0].get('parents', [None])[0],
    125                                  new_folder=folder)
    126         return self.spreadsheet_cls(self, jsonsheet=result)

~\Anaconda3\lib\site-packages\pygsheets\drive.py in spreadsheet_metadata(self, query, only_team_drive, fid)
    144         """
    145         if fid:
--> 146             return self._execute_request(self.service.files().get(fileid=fid))
    147         return self._metadata_for_mime_type(self._spreadsheet_mime_type, query, only_team_drive)
    148 

~\Anaconda3\lib\site-packages\googleapiclient\discovery.py in method(self, **kwargs)
   1017         for name in kwargs:
   1018             if name not in parameters.argmap:
-> 1019                 raise TypeError('Got an unexpected keyword argument {}'.format(name))
   1020 
   1021         # Remove args that have a value of None.

TypeError: Got an unexpected keyword argument fileid

System Information

nithinmurali commented 2 years ago

Looks like the worksheet id is being sent wrong. as a workaround you can unlink the cell and call cell.update(wks.id) Will push a fix.

ltanedo commented 2 years ago

Love this package and really wanted this feature, so found the bugs

drive.py@146

return self._execute_request(self.service.files().get(fileId=fid)) #FIXME broken fileId

client@124

old_folder = self.drive.spreadsheet_metadata(fid=result['spreadsheetId']).get('parents', [None])[0]
nithinmurali commented 2 years ago

I think this PR fixes it https://github.com/nithinmurali/pygsheets/pull/549

ltanedo commented 2 years ago

Ah gotcha, thanks for the great work btw.

nithinmurali commented 1 year ago

Closing as this is fixed. Please repone if not.

ay-nickhuang commented 1 year ago

Hi, I encounter the same issue and I am using 2.0.6, but it would work when I use 2.0.5

my code:

import pygsheets

gc = pygsheets.authorize(service_account_file='auth.json')
gc.create(title="test", folder="<folder_id>")

error:

File "/usr/local/lib/python3.10/site-packages/pygsheets/client.py", line 124, in create
    old_folder=self.drive.spreadsheet_metadata(fid=result['spreadsheetId'])[0].get('parents', [None])[0],
KeyError: 0

my environment: macOS Ventura 13.1 python 3.10.9

nithinmurali commented 1 year ago

This is fixed in the staging version, please use it until the next release. https://github.com/nithinmurali/pygsheets#from-github-recommended