From c7daa1efde60c2b4f44eab3b95f72184af9fe792 Mon Sep 17 00:00:00 2001 From: Daniel Quandt Date: Mon, 16 Nov 2020 14:32:16 -0300 Subject: [PATCH] Added capability to optionally move files by folder ID in addition to using the folder name --- gspread_pandas/client.py | 22 +++++++++++++--------- gspread_pandas/spread.py | 4 ++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/gspread_pandas/client.py b/gspread_pandas/client.py index a188d15..d00762a 100644 --- a/gspread_pandas/client.py +++ b/gspread_pandas/client.py @@ -384,7 +384,7 @@ def create_folder(self, path, parents=True): self.refresh_directories() return parent - def move_file(self, file_id, path, create=False): + def move_file(self, file_id, path, id_string, create=False): """ Move a file to the given path. @@ -400,16 +400,20 @@ def move_file(self, file_id, path, create=False): Returns ------- """ - if path == "/": - folder_id = "root" + if id_string: + folder_id = id_string + else: - parent, missing = folders_to_create(path, self._get_dirs(False)) - if missing: - if not create: - raise Exception("Folder does not exist") + if path == "/": + folder_id = "root" + else: + parent, missing = folders_to_create(path, self._get_dirs(False)) + if missing: + if not create: + raise Exception("Folder does not exist") - parent = self.create_folder(path) - folder_id = parent["id"] + parent = self.create_folder(path) + folder_id = parent["id"] old_parents = self._drive_request( "get", file_id, params={"fields": "parents"} diff --git a/gspread_pandas/spread.py b/gspread_pandas/spread.py index b8da8e5..5a5ea0c 100644 --- a/gspread_pandas/spread.py +++ b/gspread_pandas/spread.py @@ -1012,7 +1012,7 @@ def list_permissions(self): """ return self.client.list_permissions(self.spread.id) - def move(self, path="/", create=True): + def move(self, path="/", id_string=None, create=True): """ Move the current spreadsheet to the specified path in your Google drive. If the file is not currently in you drive, it will be added. @@ -1027,4 +1027,4 @@ def move(self, path="/", create=True): Returns ------- """ - self.client.move_file(self.spread.id, path, create) + self.client.move_file(self.spread.id, path, id_string, create)