Skip to content

Commit

Permalink
Merge branch 'staging'
Browse files Browse the repository at this point in the history
  • Loading branch information
Nithin Murali committed Jan 18, 2021
2 parents f957854 + fa227b8 commit 5c1c3c0
Show file tree
Hide file tree
Showing 21 changed files with 7,475 additions and 3,345 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/pypi-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This workflows will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

name: Publish Python Package

on: [workflow_dispatch]

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist
python setup.py bdist_wheel --universal
twine upload dist/*
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Run Tests

on: [workflow_dispatch]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['2.7', '3.5', '3.6', '3.7', '3.8']

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest pytest-cov
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Test with pytest
run: |
make test
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
language: python
python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
- "3.7"
- "3.8"
- "3.9"

# command to install dependencies
install: "pip install ."
# command to run tests
Expand Down
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# pygsheets - Google Spreadsheets Python API v4
[![Build Status](https://travis-ci.org/nithinmurali/pygsheets.svg?branch=master)](https://travis-ci.org/nithinmurali/pygsheets) [![PyPI version](https://badge.fury.io/py/pygsheets.svg)](https://badge.fury.io/py/pygsheets) [![Documentation Status](https://readthedocs.org/projects/pygsheets/badge/?version=latest)](http://pygsheets.readthedocs.io/en/latest/?badge=latest)
[![Build Status](https://travis-ci.org/nithinmurali/pygsheets.svg?branch=staging)](https://travis-ci.org/nithinmurali/pygsheets) [![PyPI version](https://badge.fury.io/py/pygsheets.svg)](https://badge.fury.io/py/pygsheets) [![Documentation Status](https://readthedocs.org/projects/pygsheets/badge/?version=latest)](http://pygsheets.readthedocs.io/en/latest/?badge=latest)

A simple, intutive library for google sheets which gets most of your work done.
A simple, intuitive library for google sheets which gets your work done.

Features:

Expand All @@ -14,10 +14,10 @@ Features:
* Data validation support. checkboxes, drop-downs etc.
* Conditional formatting support
* Offline calls batching support
* get multiple ranges with get_values_batch

## Updates
* version [2.0.3](https://github.com/nithinmurali/pygsheets/releases/tag/2.0.3) released
* hotfix [2.0.3.1](https://github.com/nithinmurali/pygsheets/releases/tag/2.0.3.1) released
* version [2.0.4](https://github.com/nithinmurali/pygsheets/releases/tag/2.0.4) released

## Installation

Expand All @@ -34,7 +34,7 @@ If you are installing from pypi please see the docs [here](https://pygsheets.rea
#### From GitHub (Recommended)

```sh
pip install https://github.com/nithinmurali/pygsheets/archive/master.zip
pip install https://github.com/nithinmurali/pygsheets/archive/staging.zip

```

Expand Down Expand Up @@ -166,6 +166,9 @@ wks = sh[0]
# Get values as 2d array('matrix') which can easily be converted to an numpy aray or as 'cell' list
values_mat = wks.get_values(start=(1,1), end=(20,20), returnas='matrix')

# Get values of - rows A1 to B10, column C, 1st row, 10th row
wks.get_values_batch(['A1:B10', 'C', '1', (10, None)])

# Get all values of sheet as 2d list of cells
cell_matrix = wks.get_all_values(returnas='matrix')

Expand Down Expand Up @@ -350,7 +353,7 @@ cell = rng[0][1]

### Batching calls

If you are calling a lot of spreadsheet modfication functions (non value update). you can merge them into a single call.
If you are calling a lot of spreadsheet modification functions (non value update). you can merge them into a single call.
By doing so all the requests will be merged into a single call.

```python
Expand All @@ -367,7 +370,7 @@ Batching also happens when you unlink worksheet. But in that case the requests a

## How to Contribute

This library is still in development phase. So there is a lot of work to be done.
This library is still in development phase.

* Follow the [Contributing to Open Source](https://guides.github.com/activities/contributing-to-open-source/) Guide.
* Branch off of the `staging` branch, and submit Pull Requests back to
Expand Down
39 changes: 0 additions & 39 deletions TODO.md

This file was deleted.

2 changes: 1 addition & 1 deletion pygsheets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""

__version__ = '2.0.3.1'
__version__ = '2.0.4'
__author__ = 'Nithin Murali'

from pygsheets.authorization import authorize
Expand Down
33 changes: 32 additions & 1 deletion pygsheets/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class Address(object):
<Address B2>
>>> a == (2, 2)
True
>>> a = Address((None, 1), True)
<Address A>
>>> a = Address('2', True)
<Address 2>
"""

_MAGIC_NUMBER = 64
Expand Down Expand Up @@ -330,6 +334,8 @@ def worksheet_title(self):

@worksheet_title.setter
def worksheet_title(self, value):
if not value:
return
if self._worksheet:
if self._worksheet.title == value:
return
Expand All @@ -338,6 +344,29 @@ def worksheet_title(self, value):
self._worksheet_title = value
self._calculate_label()

@staticmethod
def create(data, wks=None):
"""
create a Gridrange from various type of data
:param data: can be string in A format,tuple or list, dict in GridRange format, GridRange object
:param wks: worksheet to link to (optional)
:return: GridRange object
"""
if isinstance(data, GridRange):
grange = data
elif isinstance(data, str):
grange = GridRange(label=data, worksheet=wks)
elif isinstance(data, tuple) or isinstance(data, list):
if len(data) < 2: raise InvalidArgumentValue("start and end required")
grange = GridRange(start=data[0], end=data[1], worksheet=wks)
elif isinstance(data, dict):
grange = GridRange(propertiesjson=data, worksheet=wks)
else:
raise InvalidArgumentValue(data)
if wks:
grange.set_worksheet(wks)
return grange

def set_worksheet(self, value):
""" set the worksheet of this grid range. """
self._worksheet = value
Expand Down Expand Up @@ -399,15 +428,17 @@ def _calculate_label(self):

def _calculate_addresses(self, label):
""" update values from label """
self.worksheet_title = label.split('!')[0]
self._start, self._end = Address(None, True), Address(None, True)
if len(label.split('!')) > 1:
self.worksheet_title = label.split('!')[0]
rem = label.split('!')[1]
if ":" in rem:
self._start = Address(rem.split(":")[0], allow_non_single=True)
self._end = Address(rem.split(":")[1], allow_non_single=True)
else:
self._start = Address(rem, allow_non_single=True)
else:
pass
self._apply_index_constraints()

def to_json(self):
Expand Down
4 changes: 3 additions & 1 deletion pygsheets/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def neighbour(self, position):
"""
if not self._linked:
return False
addr = self._address
addr = Address(self._address)
if type(position) == tuple:
addr = addr + position
# TODO: this does not work if position is a list...
Expand Down Expand Up @@ -553,6 +553,8 @@ def set_json(self, cell_data):
self._vertical_alignment = \
VerticalAlignment[nvertical_alignment] if nvertical_alignment is not None else None

self.hyperlink = cell_data.get('hyperlink', '')

def __setattr__(self, key, value):
if key not in ['_linked', '_worksheet']:
self.__dict__['is_dirty'] = True
Expand Down
37 changes: 27 additions & 10 deletions pygsheets/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def spreadsheet_titles(self, query=None):
"""Get a list of all spreadsheet titles present in the Google Drive or TeamDrive accessed."""
return [x['name'] for x in self.drive.spreadsheet_metadata(query)]

def create(self, title, template=None, folder=None, **kwargs):
def create(self, title, template=None, folder=None, folder_name=None, **kwargs):
"""Create a new spreadsheet.
The title will always be set to the given value (even overwriting the templates title). The template
Expand All @@ -102,6 +102,7 @@ def create(self, title, template=None, folder=None, **kwargs):
:param title: Title of the new spreadsheet.
:param template: A template to create the new spreadsheet from.
:param folder: The Id of the folder this sheet will be stored in.
:param folder_name: The Name of the folder this sheet will be stored in.
:param kwargs: Standard parameters (see reference for details).
:return: :class:`~pygsheets.Spreadsheet`
"""
Expand All @@ -114,10 +115,13 @@ def create(self, title, template=None, folder=None, **kwargs):
result = self.drive.copy_file(template.id, title, folder)
return self.open_by_key(result['id'])

if folder_name and not folder:
folder = self.drive.get_folder_id(folder_name)

result = self.sheet.create(title, template=template, **kwargs)
if folder:
self.drive.move_file(result['spreadsheetId'],
old_folder=self.drive.spreadsheet_metadata(query="name = '" + title + "'")[0]['parents'][0],
old_folder=self.drive.spreadsheet_metadata(query="name = '" + title + "'")[0].get('parents', [None])[0],
new_folder=folder)
return self.spreadsheet_cls(self, jsonsheet=result)

Expand Down Expand Up @@ -201,16 +205,18 @@ def open_as_json(self, key):
includeGridData=False)

def get_range(self, spreadsheet_id,
value_range,
value_range=None,
major_dimension='ROWS',
value_render_option=ValueRenderOption.FORMATTED_VALUE,
date_time_render_option=DateTimeRenderOption.SERIAL_NUMBER):
date_time_render_option=DateTimeRenderOption.SERIAL_NUMBER,
value_ranges=None):
"""Returns a range of values from a spreadsheet. The caller must specify the spreadsheet ID and a range.
Reference: `request <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get>`__
:param spreadsheet_id: The ID of the spreadsheet to retrieve data from.
:param value_range: The A1 notation of the values to retrieve.
:param value_ranges: The list of A1 notation of the values to retrieve.
:param major_dimension: The major dimension that results should use.
For example, if the spreadsheet data is: A1=1,B1=2,A2=3,B2=4, then
requesting range=A1:B2,majorDimension=ROWS will return [[1,2],[3,4]],
Expand All @@ -224,9 +230,20 @@ def get_range(self, spreadsheet_id,
:return: An array of arrays with the values fetched. Returns an empty array if no
values were fetched. Values are dynamically typed as int, float or string.
"""
result = self.sheet.values_get(spreadsheet_id, value_range, major_dimension, value_render_option,
date_time_render_option)
try:
return result['values']
except KeyError:
return [['']]
if value_range:
result = self.sheet.values_get(spreadsheet_id, value_range, major_dimension, value_render_option,
date_time_render_option)
try:
return result['values']
except KeyError:
return [['']]
elif value_ranges:
results = self.sheet.values_batch_get(spreadsheet_id, value_ranges, major_dimension, value_render_option,
date_time_render_option)
values = []
for result in results:
try:
values.append(result['values'])
except KeyError:
values.append([['']])
return values
Loading

0 comments on commit 5c1c3c0

Please sign in to comment.