Skip to content

Commit

Permalink
some cleanup, and fixes nithinmurali#169
Browse files Browse the repository at this point in the history
  • Loading branch information
nithinmurali committed May 1, 2020
1 parent 39f4af5 commit 10a2f5b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
8 changes: 8 additions & 0 deletions pygsheets/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def col(self):
"""Column of the address"""
return self._value[1]

@row.setter
def row(self, value):
self._value = value, self._value[1]

@col.setter
def col(self, value):
self._value = self._value[0], value

@property
def index(self):
"""Current Address in tuple format. Both axes starts at 1."""
Expand Down
3 changes: 1 addition & 2 deletions pygsheets/authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os
import json
import warnings
# import pickle

from google.oauth2 import service_account
from google.oauth2.credentials import Credentials
Expand Down Expand Up @@ -126,7 +125,7 @@ def authorize(client_secret='client_secret.json',
elif service_account_env_var is not None:
service_account_info = json.loads(os.environ[service_account_env_var])
credentials = service_account.Credentials.from_service_account_info(
service_account_info, scopes=scopes)
service_account_info, scopes=scopes)
elif service_account_file is not None:
credentials = service_account.Credentials.from_service_account_file(service_account_file, scopes=scopes)
else:
Expand Down
8 changes: 2 additions & 6 deletions pygsheets/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ class Cell(object):
def __init__(self, pos, val='', worksheet=None, cell_data=None):
self._worksheet = worksheet

# if type(pos) == str:
# pos = format_addr(pos, 'tuple')
# self._row, self._col = pos
# self._label = format_addr(pos, 'label')
self._address = Address(pos, False)

self._value = val # formatted value
Expand Down Expand Up @@ -402,9 +398,9 @@ def neighbour(self, position):
"""
if not self._linked:
return False
addr = [self.row, self.col]
addr = self._address
if type(position) == tuple:
addr = (addr[0] + position[0], addr[1] + position[1])
addr = addr + position
# TODO: this does not work if position is a list...
elif type(position) == str:
if "right" in position:
Expand Down
9 changes: 9 additions & 0 deletions pygsheets/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ def create(self, title, template=None, folder=None, **kwargs):
:param kwargs: Standard parameters (see reference for details).
:return: :class:`~pygsheets.Spreadsheet`
"""

if isinstance(template, str):
result = self.drive.copy_file(template, title, folder)
return self.open_by_key(result['id'])

if isinstance(template, Spreadsheet):
result = self.drive.copy_file(template.id, title, folder)
return self.open_by_key(result['id'])

result = self.sheet.create(title, template=template, **kwargs)
if folder:
self.drive.move_file(result['spreadsheetId'],
Expand Down
14 changes: 6 additions & 8 deletions pygsheets/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ def set_dataframe(self, df, start, copy_index=False, copy_head=True, extend=Fals
return False
nan = kwargs.get('nan', "NaN")

start = format_addr(start, 'tuple')
start = Address(start)
for col in df.select_dtypes('Int64'):
df[col] = df[col].astype('unicode').replace('<NA>', nan)
df = df.fillna(nan)
Expand Down Expand Up @@ -1337,7 +1337,7 @@ def set_dataframe(self, df, start, copy_index=False, copy_head=True, extend=Fals
values.insert(0, head)
df_rows += 1

end = format_addr(tuple([start[0]+df_rows, start[1]+df_cols]))
end = start + (df_rows, df_cols)

if fit == extend is not False:
raise InvalidArgumentValue("fit should not be same with extend")
Expand All @@ -1358,13 +1358,11 @@ def set_dataframe(self, df, start, copy_index=False, copy_head=True, extend=Fals
if extend == "row":
self.rows = max(self.rows, start[0] - 1 + df_rows)

# @TODO optimize this
if escape_formulae:
for row in values:
for i in range(len(row)):
if type(row[i]) == str and (row[i].startswith('=') or row[i].startswith('+')):
row[i] = "'" + str(row[i])
crange = format_addr(start) + ':' + end
values = list(map(lambda row: list(map(lambda cell: "'" + cell if type(cell) == str
and (cell.startswith('=') or cell.startswith('+')) else cell, row)), values))

crange = start.label + ':' + end.label
self.update_values(crange=crange, values=values)

def get_as_df(self, has_header=True, index_column=None, start=None, end=None, numerize=True,
Expand Down

0 comments on commit 10a2f5b

Please sign in to comment.