nithinmurali / pygsheets

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

unlink simply doesn't work for shared drive sheets #567

Open BenCurtisD opened 1 year ago

BenCurtisD commented 1 year ago

Note, if this is a usage question, please ask a question in stackoverflow with pygsheets tag.

Describe the bug When trying to unlink a sheet in a shared drive the actual api call doesn't include the header to allow for this and the file isn't found

To Reproduce Steps to reproduce the behavior:

load sheet from a shared drive and unlink, in my case:
master = client.open_by_key('10cHHZMd6wPWPx0m5UEomiGK5ikHJui1BfXjOPz-SBPQ')
...
destination = master.add_worksheet(sheetName, src_worksheet=template)
...
destination.unlink()
# see error
Traceback (most recent call last):
  File "D:\Documents\Github\SBFO-Kanoptima-Software\tester.py", line 10, in <module>        
    html = CPIProcessor.singleCPI("AMEDOU01A", "2023-01-12", driver)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Documents\Github\SBFO-Kanoptima-Software\CPIProcessor.py", line 183, in singleCPI
    destination.unlink()
  File "C:\Users\ben\AppData\Local\Programs\Python\Python311\Lib\site-packages\pygsheets\worksheet.py", line 225, in unlink
    self._update_grid()
  File "C:\Users\ben\AppData\Local\Programs\Python\Python311\Lib\site-packages\pygsheets\worksheet.py", line 185, in _update_grid
    updated = datetime.datetime.strptime(self.spreadsheet.updated, '%Y-%m-%dT%H:%M:%S.%fZ')
                                         ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ben\AppData\Local\Programs\Python\Python311\Lib\site-packages\pygsheets\spreadsheet.py", line 105, in updated
    return self.client.drive.get_update_time(self.id)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ben\AppData\Local\Programs\Python\Python311\Lib\site-packages\pygsheets\drive.py", line 78, in get_update_time
    return self._execute_request(self.service.files().get(fileId=file_id, fields='modifiedTime',
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ben\AppData\Local\Programs\Python\Python311\Lib\site-packages\pygsheets\drive.py", line 428, in _execute_request
    return request.execute(num_retries=self.retries)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ben\AppData\Local\Programs\Python\Python311\Lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ben\AppData\Local\Programs\Python\Python311\Lib\site-packages\googleapiclient\http.py", line 938, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/drive/v3/files/10cHHZMd6wPWPx0m5UEomiGK5ikHJui1BfXjOPz-SBPQ?fields=modifiedTime&supportsAllDrives=false&alt=json returned "File not found: 10cHHZMd6wPWPx0m5UEomiGK5ikHJui1BfXjOPz-SBPQ.". Details: "[{'message': 'File not found: 10cHHZMd6wPWPx0m5UEomiGK5ikHJui1BfXjOPz-SBPQ.', 'domain': 'global', 'reason': 'notFound', 'location': 'fileId', 'locationType': 'parameter'}]">

System Information

BenCurtisD commented 1 year ago

Update: I identified the source of the issue, def get_update_time(self, file_id): """Returns the time this file was last modified in RFC 3339 format.""" return self._execute_request(self.service.files().get(fileId=file_id, fields='modifiedTime', supportsAllDrives=self.is_team_drive()))['modifiedTime']

self.is_team_drive() was returning False in my case, so for my version I simply replaced it with True, not entirely sure why it doesn't work but it doesn't.

BenCurtisD commented 1 year ago

bigger problem, after fixing the unlink, the link() function simply returns a httpError 500 even when nothing is changed. destination.unlink() destination.link()

Traceback (most recent call last): File "D:\Documents\Github\SBFO-Kanoptima-Software\tester.py", line 10, in html = CPIProcessor.singleCPI("AMEDOU01A", "2023-01-12", driver) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Documents\Github\SBFO-Kanoptima-Software\CPIProcessor.py", line 189, in singleCPI destination.link() File "C:\Users\ben\AppData\Local\Programs\Python\Python311\Lib\site-packages\pygsheets\worksheet.py", line 201, in link self.client.sheet.update_sheet_properties_request(self.spreadsheet.id, self.jsonSheet['properties'], '') File "C:\Users\ben\AppData\Local\Programs\Python\Python311\Lib\site-packages\pygsheets\sheet.py", line 188, in update_sheet_properties_request return self.batch_update(spreadsheet_id, request) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ben\AppData\Local\Programs\Python\Python311\Lib\site-packages\pygsheets\sheet.py", line 120, in batch_update return self._execute_requests(request) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ben\AppData\Local\Programs\Python\Python311\Lib\site-packages\pygsheets\sheet.py", line 496, in _execute_requests response = request.execute(num_retries=self.retries) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ben\AppData\Local\Programs\Python\Python311\Lib\site-packages\googleapiclient_helpers.py", line 130, in positional_wrapper return wrapped(args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ben\AppData\Local\Programs\Python\Python311\Lib\site-packages\googleapiclient\http.py", line 938, in execute raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 500 when requesting https://sheets.googleapis.com/v4/spreadsheets/10cHHZMd6wPWPx0m5UEomiGK5ikHJui1BfXjOPz-SBPQ:batchUpdate?fields=%2A&alt=json returned "Internal error encountered.". Details: "Internal error encountered.">