Giter VIP home page Giter VIP logo

pygsheets's Introduction

pygsheets - Google Spreadsheets Python API v4

Build Status PyPI version Documentation Status Gitpod ready-to-code

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

Features:

  • Open, create, delete and share spreadsheets using title or key
  • Intuitive models - spreadsheet, worksheet, cell, datarange
  • Control permissions of spreadsheets.
  • Set cell format, text format, color, write notes
  • Named and Protected Ranges Support
  • Work with range of cells easily with DataRange and Gridrange
  • Data validation support. checkboxes, drop-downs etc.
  • Conditional formatting support
  • get multiple ranges with get_values_batch and update wit update_values_batch

Updates

Installation

From PyPi (Stable)

pip install pygsheets

If you are installing from pypi please see the docs here.

From GitHub (Recommended)

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

If you are installing from github please see the docs here.

Basic Usage

Basic features are shown here, for complete set of features see the full documentation here.

  1. Obtain OAuth2 credentials from Google Developers Console for google spreadsheet api and drive api and save the file as client_secret.json in same directory as project. read more here.

  2. Start using pygsheets:

Sample scenario : you want to share a numpy array with your remote friend

import pygsheets
import numpy as np

gc = pygsheets.authorize()

# Open spreadsheet and then worksheet
sh = gc.open('my new sheet')
wks = sh.sheet1

# Update a cell with value (just to let him know values is updated ;) )
wks.update_value('A1', "Hey yank this numpy array")
my_nparray = np.random.randint(10, size=(3, 4))

# update the sheet with array
wks.update_values('A2', my_nparray.tolist())

# share the sheet with your friend
sh.share("[email protected]")

Sample Scenario: you want to fill height values of students

## import pygsheets and open the sheet as given above

header = wks.cell('A1')
header.value = 'Names'
header.text_format['bold'] = True # make the header bold
header.update()

# or achive the same in oneliner
wks.cell('B1').set_text_format('bold', True).value = 'heights'

# set the names
wks.update_values('A2:A5',[['name1'],['name2'],['name3'],['name4']])

# set the heights
heights = wks.range('B2:B5', returnas='range')  # get the range as DataRange object
heights.name = "heights"  # name the range
heights.update_values([[50],[60],[67],[66]]) # update the values
wks.update_value('B6','=average(heights)') # set the avg value of heights using named range

More Examples

Opening a Spreadsheet

# You can open a spreadsheet by its title as it appears in Google Docs 
sh = gc.open("pygsheetTest")

# If you want to be specific, use a key
sht1 = gc.open_by_key('1mwA-NmvjDqd3A65c8hsxOpqdfdggPR0fgfg5nXRKScZAuM')

# create a spreadsheet in a folder (by id)
sht2 = gc.create("new sheet", folder_name="my worksheets")

# open enable TeamDrive support
gc.drive.enable_team_drive("Dqd3A65c8hsxOpqdfdggPR0fgfg")

Operations on Spreadsheet doc

show code
import pygsheets
c = pygsheets.authorize()
sh = c.open('spreadsheet')

# create a new sheet with 50 rows and 60 colums
wks = sh.add_worksheet("new sheet",rows=50,cols=60)

# create a new sheet with 50 rows and 60 colums at the begin of worksheets
wks = sh.add_worksheet("new sheet",rows=50,cols=60,index=0)

# or copy from another worksheet
wks = sh.add_worksheet("new sheet", src_worksheet='<other worksheet instance>')

# delete this wroksheet
sh.del_worksheet(wks)

# unshare the sheet
sh.remove_permissions("[email protected]")

Selecting a Worksheet

show code
import pygsheets
c = pygsheets.authorize()
sh = c.open('spreadsheet')

# Select worksheet by id, index, title.
wks = sh.worksheet_by_title("my test sheet")

# By any property
wks = sh.worksheet('index', 0)

# Get a list of all worksheets
wks_list = sh.worksheets()

# Or just
wks = sh[0]

Operations on Worksheet doc

show code
# 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')

# update a range of values with a cell list or matrix
wks.update_values(crange='A1:E10', values=values_mat)

# update multiple ranges with bath update
wks.update_values_batch(['A1:A2', 'B1:B2'], [[[1],[2]], [[3],[4]]])

# Insert 2 rows after 20th row and fill with values
wks.insert_rows(row=20, number=2, values=values_list)

# resize by changing rows and colums
wks.rows=30

# use the worksheet as a csv
for row in wks:
    print(row)

# get values by indexes
 A1_value = wks[0][0]

# clear all values
wks.clear()

# Search for a table in the worksheet and append a row to it
wks.append_table(values=[1,2,3,4])

# export a worksheet as csv
wks.export(pygsheets.ExportType.CSV)

# Find/Replace cells with string value
cell_list = worksheet.find("query string")

# Find/Replace cells with regexp
filter_re = re.compile(r'(small|big) house')
cell_list = worksheet.find(filter_re, searchByRegex=True)
cell_list = worksheet.replace(filter_re, 'some house', searchByRegex=True)

# Move a worksheet in the same spreadsheet (update index)
wks.index = 2 # index start at 1 , not 0

# Update title
wks.title = "NewTitle"

# Update hidden state
wks.hidden = False

# working with named ranges
wks.create_named_range('A1', 'A10', 'prices')
wks.get_named_range('prices')
wks.get_named_ranges()  # will return a list of DataRange objects
wks.delete_named_range('prices')

# apply format
wks.apply_format(['A1:B1', 'D:E'], ["NUMBER", "TEXT"])

# Plot a chart/graph
wks.add_chart(('A1', 'A6'), [('B1', 'B6')], 'Health Trend')

# create drop-downs
wks.set_data_validation(start='C4', end='E7', condition_type='NUMBER_BETWEEN', condition_values=[2,10], strict=True, inputMessage="inut between 2 and 10")

# set a formula and extend it to more cells
cell = wks.cell('C1')
cell.value = '=A1+B1'
wks.apply_format('C1:C10', cell, 'userEnteredValue.formulaValue')

Pandas integration

If you work with pandas, you can directly use the dataframes

#set the values of a pandas dataframe to sheet
wks.set_dataframe(df,(1,1))

#you can also get the values of sheet as dataframe
df = wks.get_as_df()

Cell Object doc

Each cell has a value and cordinates (row, col, label) properties.

Getting cell objects

show code
c1 = Cell('A1',"hello")  # create a unlinked cell
c1 = worksheet.cell('A1')  # creates a linked cell whose changes syncs instantanously
cl.value  # Getting cell value
c1.value_unformatted #Getting cell unformatted value
c1.formula # Getting cell formula if any
c1.note # any notes on the cell
c1.address # address object with cell position

cell_list = worksheet.range('A1:C7')  # get a range of cells 
cell_list = worksheet.col(5, returnas='cell')  # return all cells in 5th column(E)

Most of the functions has returnas param, if whose value is cell it will return a list of cell objects. Also you can use label or (row,col) tuple interchangbly as a cell adress

Cell Operations

Each cell is directly linked with its cell in spreadsheet, hence changing the value of cell object will update the corresponding cell in spreadsheet unless you explictly unlink it Also not that bu default only the value of cell is fetched, so if you are directly accessing any cell properties call cell.fetch() beforehand.

Different ways of updating Cells

show code
# using linked cells
c1 = worksheet.cell('B1') # created from worksheet, so linked cell
c1.col = 5  # Now c1 correponds to E1
c1.value = "hoho"  # will change the value of E1

# Or onliner
worksheet.update_value('B1', 'hehe')

# get a range of cells
cell_list = worksheet.range('A1:C7')
cell_list = worksheet.get_values(start='A1', end='C7', returnas='cells')
cell_list = worksheet.get_row(2, returnas='cells')


# add formula
c1.formula = 'A1+C2'
c1.formula # '=A1+C2'

# get neighbouring cells
c2 = c1.neighbour('topright') # you can also specify relative position as tuple eg (1,1)

# set cell format
c1.set_number_format(pygsheets.FormatType.NUMBER, '00.0000')

# write notes on cell
c1.note = "yo mom"

# set cell color
c1.color = (1.0, 1.0, 1.0, 1.0) # Red, Green, Blue, Alpha

# set text format
c1.text_format['fontSize'] = 14
c1.set_text_format('bold', True)

# sync the changes
 c1.update()

# you can unlink a cell and set all required properties and then link it
# So yu could create a model cell and update multiple sheets
c.unlink()
c.note = "offine note"
c.link(wks1, True)
c.link(wks2, True)

DataRange Object doc

The DataRange is used to represent a range of cells in a worksheet. They can be named or protected. Almost all get_ functions has a returnas param, set it to range to get a range object.

show code
# Getting a Range object
rng = wks.get_values('A1', 'C5', returnas='range')
rng.start_addr = 'A' # make the range unbounded on rows <Datarange Sheet1!A:B>
drange.end_addr = None # make the range unbounded on both axes <Datarange Sheet1>

# Named ranges
rng.name = 'pricesRange'  # will make this range a named range
rng = wks.get_named_ranges('commodityCount') # directly get a named range
rng.name = ''  # will delete this named range

#Protected ranges
rng.protected = True
rng.editors = ('users', '[email protected]')

# Setting Format
 # first create a model cell with required properties
model_cell = Cell('A1')
model_cell.color = (1.0,0,1.0,1.0) # rose color cell
model_cell.format = (pygsheets.FormatType.PERCENT, '')

 # Setting format to multiple cells in one go
rng.apply_format(model_cell)  # will make all cell in this range rose color and percent format
# Or if you just want to apply format, you can skip fetching data while creating datarange
Datarange('A1','A10', worksheet=wks).apply_format(model_cell)

# get cells in range
cell = rng[0][1]

Batching calls

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.

gc.set_batch_mode(True)
wks.merge_cells("A1", "A2")
wks.merge_cells("B1", "B2")
Datarange("D1", "D5", wks).apply_format(cell)
gc.run_batch() # All the above requests are executed here
gc.set_batch_mode(False)

Batching also happens when you unlink worksheet. But in that case the requests are not merged.

How to Contribute

This library is still in development phase.

  • Follow the Contributing to Open Source Guide.
  • Branch off of the staging branch, and submit Pull Requests back to that branch. Note that the master branch is used for version bumps and hotfixes only.
  • For quick testing the changes you have made to source, run the file tests/manual_testing.py. It will give you an IPython shell with lastest code loaded.

Report Issues/Features

  • Please report bugs and suggest features via the GitHub Issues.
  • Before opening an issue, search the tracker for possible duplicates.
  • If you have any usage questions, ask a question on stackoverflow with pygsheets Tag

Run Tests

  • install pip install -r requirements-dev.txt
  • run make test

Now that you have scrolled all the way down, finding this library useful? Buy Me A Coffee

pygsheets's People

Contributors

abdoukarim avatar awalker88 avatar benm-brainlabs avatar case avatar colwynlegitscript avatar dankrause avatar daverck avatar elliott-beach avatar elyase avatar felipemaion avatar jalavosus avatar jmny avatar johnbarton27 avatar jzohrab avatar katsuhisa91 avatar kordishal avatar ktpss95112 avatar lmaximov2013 avatar meteozond avatar mqzhang avatar nithin-codility avatar nithinmurali avatar pedrorojasg avatar rmccorm4 avatar ryfi avatar scmadar avatar shenganzhang avatar ssg2526 avatar walterpixowl avatar wgordon17 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pygsheets's Issues

[feature request] easy ways to append columns

An append_rows method exists, but a corresponding append_cols does not. I think it would be a great addition to the framework.

Similarly, it would be nice if update_cells could automatically add columns and rows to the workspace if needed.

Doesn't work with Service Account

I get credentials form Service Account:
credentials = ServiceAccountCredentials.from_json_keyfile_name(key_file, scopes=scopes)
but then when I use them:
gc = pygsheets.authorize(credentials)
I get this:

Traceback (most recent call last):
File "/home/jacek/workspace/ibis2/branches/161204/py/tests/gsheet.py", line 29, in
gc = pygsheets.authorize(credentials)
File "build/bdist.linux-x86_64/egg/pygsheets/client.py", line 492, in authorize
File "build/bdist.linux-x86_64/egg/pygsheets/client.py", line 434, in get_outh_credentials
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 407, in get
return self.locked_get()
File "/usr/local/lib/python2.7/dist-packages/oauth2client/file.py", line 45, in locked_get
_helpers.validate_file(self._filename)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/_helpers.py", line 250, in validate_file
if os.path.islink(filename):
File "/usr/lib/python2.7/posixpath.py", line 135, in islink
st = os.lstat(path)
TypeError: coercing to Unicode: need string or buffer, ServiceAccountCredentials found

Any ideas? way around.
-- jacek

The calls to append_row in the README is wrong

Hi,

first of all thank you for your library, great work!

This command in the README is wrong:
wks.append_row([1,2,3,4])

This is the correct command:
wks.append_row(values=[1,2,3,4])

Can the export method take an argument to redirect file output

When using the spreadsheet or worksheet export method it outputs the data to a static filename. In the case of worksheet that looks like worksheet[worksheet ID] (maybe). Is it possible for the user to specify the filename for the output instead of the static name created by the method?

Add python3 to CI

Travis.yml doesn't agree with the versions offered in the readme

language: python
python:
  - "2.6"
  - "2.7"

Should we add testing for python3?

HttpError from pygsheets.authorize()

Thanks for creating pygsheets. It looks really useful.

I'm having trouble getting started. I created a service account per the docs, but googleapiclient/http.py raises an HttpError when I try to authorize. The error message suggests waiting a few minutes. That didn't help.

Here's a summary of the error:

In [19]: gc = pygsheets.authorize(service_file='client_secret.json')
service_email : [email protected]
...
/Users/mellis/anaconda3/lib/python3.5/site-packages/googleapiclient/http.py in execute(self, http, num_retries)
    838       callback(resp)
    839     if resp.status >= 300:
--> 840       raise HttpError(resp, content, uri=self.uri)
    841     return self.postproc(resp, content)
    842 

HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files?fields=files%28id%2C+name%29&q=mimeType%3D%27application%2Fvnd.google-apps.spreadsheet%27&pageSize=500&alt=json&corpus=user returned "Project 671397702967 is not found and cannot be used for API calls. If it is recently created, enable Drive API by visiting https://console.developers.google.com/apis/api/drive.googleapis.com/overview?project=671397702967 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.">

Here's an expurgated version of my client_secrets file.

In [20]: cat client_secret.json
{
  "type": "service_account",
  "project_id": "testpygsheets",
  "private_key_id": "88d6...",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEv...==\n-----END PRIVATE KEY-----\n",
  "client_email": "[email protected]",
  "client_id": "107...",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/testpygsheets%40testpygsheets.iam.gserviceaccount.com"
}

What do I need to do to resolve the problem?

Thanks!

spreadsheet updated method returns error

Trying to determine the last time a spreadsheet was updated using the spreadsheet updated method. When I open a known spreadsheet via:

ss= gc.open(ssheet)

and then try to print the update information via;

updated = ss.updated()
print (updated)

This is my result:

Traceback (most recent call last):
File "./get_updated_data.py", line 26, in
updated = ss.updated()
File "/usr/lib/python2.7/site-packages/pygsheets-v1.1.1-py2.7.egg/pygsheets/spreadsheet.py", line 317, in updated
response = self.client._execute_request(self.id, request, False)
File "/usr/lib/python2.7/site-packages/pygsheets-v1.1.1-py2.7.egg/pygsheets/client.py", line 381, in _execute_request
response = request.execute()
File "/usr/lib/python2.7/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/googleapiclient/http.py", line 840, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/drive/v3/files/1rmb1_DGMmkvPbe6JFuMeSbhdAdwTJ_enSBlBaYlSHGc?fields=modifiedTime&alt=json returned "File not found: 1rmb1_DGMmkvPbe6JFuMeSbhdAdwTJ_enSBlBaYlSHGc.">

Inserting multiple rows with single request

This library is a bit painful to use for my use case because inserting multiple rows needs to be done with one request per row. I think Google even cut me off because I was running too many requests too fast. I got the error below after inserting about 150 rows.

Traceback (most recent call last):
  File "/home/mjs7231/Sync/Scripts/bank-load-transactions.py", line 117, in <module>
    save_new_transactions(worksheet, new)
  File "/home/mjs7231/Sync/Scripts/bank-load-transactions.py", line 108, in save_new_transactions
    worksheet.update_row(i+1, newrows[i])
  File "/usr/local/lib/python3.5/dist-packages/pygsheets/worksheet.py", line 376, in update_row
    self.update_cells(crange=colrange, values=[values], majordim='ROWS')
  File "/usr/local/lib/python3.5/dist-packages/pygsheets/worksheet.py", line 362, in update_cells
    self.client.sh_update_range(self.spreadsheet.id, body, self.spreadsheet.batch_mode)
  File "/usr/local/lib/python3.5/dist-packages/pygsheets/client.py", line 329, in sh_update_range
    self._execute_request(spreadsheet_id, final_request, batch)
  File "/usr/local/lib/python3.5/dist-packages/pygsheets/client.py", line 373, in _execute_request
    response = request.execute()
  File "/usr/local/lib/python3.5/dist-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/googleapiclient/http.py", line 838, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 429 when requesting https://sheets.googleapis.com/v4/spreadsheets/1L1rXyVs4UsBXcTisbZ8W57-f3fCYcO8Wgd9XtxUfsW0/values/Transactions%21A160%3AG160?alt=json&valueInputOption=USER_ENTERED
returned "Insufficient tokens for quota 'WriteGroup' and limit 'USER-100s' of service 'sheets.googleapis.com' for consumer 'project_number:1045577942644'.">

Deleting a spreadsheet by id doesn't work

I found 2 issues when doing client.delete(id='long-id-str').

  1. It struggles with the reserved keyword id. The list is always empty in this check.
    Replacing id with sheet_id solves this issue.

  2. After replacing id it raises a NameError on title here because it's not set.

I'll create a PR with my fixes

EOF when reading a line

I got the below error.

File "/home/gae-backup/lib/oauth2client/tools.py", line 239, in run_flow     
code = input('Enter verification code: ').strip() EOFError: 

Worksheet.update_cells() does not update cells

How to replicate:

# Authorize gc with your own credentials
sh = gc.open_by_url('https://docs.google.com/spreadsheets/d/1NrB3gwsvF-zQFVS5ADVaJY8OUGYwhGSue3gd3CCOfl8/edit')
# Open worksheet titled `Sheet1`
ws = sh.worksheet_by_title('Sheet1')
# Say I want to insert "TEST" to cells from B2 to C3:
ws.update_cells('B2:C3', ["TEST"]*4)

I get invalid value error.

<HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1NrB3gwsvF-zQFVS5ADVaJY8OUGYwhGSue3gd3CCOfl8/values/Sheet1%21B2%3AC3?alt=json&valueInputOption=USER_ENTERED returned "Invalid value at 'data.values[0]' (type.googleapis.com/google.protobuf.ListValue), "TEST"
Invalid value at 'data.values[1]' (type.googleapis.com/google.protobuf.ListValue), "TEST"
Invalid value at 'data.values[2]' (type.googleapis.com/google.protobuf.ListValue), "TEST"
Invalid value at 'data.values[3]' (type.googleapis.com/google.protobuf.ListValue), "TEST"">

What's the right syntax for this?

Requesting Enhancement : "get_all_records()"

If "get_all_records" will be implemented it will be great. Actually get_all_values returns list of list.
And I need is dictionary of the data in key value pairs of headers and values.

importError: No module named 'apiclient'

I believe the problem here is that google has dropped the alias of apiclient for googleapiclient. Calls to apiclient now error with the following
File "/home/ubuntu/workspace/testPygsheet.py", line 23, in <module> import pygsheets
File "/usr/local/lib/python3.4/dist-packages/pygsheets/__init__.py", line 15, in <module> from .client import Client, authorize
File "/usr/local/lib/python3.4/dist-packages/pygsheets/client.py", line 23, in <module> from apiclient import discovery
ImportError: No module named 'apiclient'

see http://stackoverflow.com/a/30811628

Not the actual instruction, the creating wks, a problem with the wks.

In the instruction no sample for create Worksheet. Then I tried sh = gc.create('my new ssheet') , and run sample from instruction:

gc = pygsheets.authorize(outh_file=CREDENTIALS_FILE)
sh = gc.create('my new ssheet')
wks = sh.sheet1

# or achive the same in oneliner
wks.cell('B1').set_text_format('bold', True).value = 'heights' 
Traceback (most recent call last):
  File "/home/vladislav/workspace/python_tests/google_sheets.py", line 49, in <module>
    wks.cell('B1').set_text_format('bold', True).value = 'heights'
  File "/home/vladislav/miniconda3/lib/python3.6/site-packages/pygsheets/cell.py", line 186, in set_text_format
    self.fetch()
  File "/home/vladislav/miniconda3/lib/python3.6/site-packages/pygsheets/cell.py", line 276, in fetch
    result = result['sheets'][0]['data'][0]['rowData'][0]['values'][0]
KeyError: 'rowData'

No error if instead the string above use:

header = wks.cell('A1')
header.value = 'Names'
header.text_format['bold'] = True # make the header bold
header.update()

But on strings:

# set the heights
heights = wks.range('B2:B5')  # get the range
heights.name = "heights"  # name the range

follow error:

    heights.name = "heights"  # name the range
AttributeError: 'list' object has no attribute 'name'

Does pygsheets Support Access to Spreadsheets on Team Drives?

I am trying to access a Spreadsheet on a Team Drive using gspread. It is not working. I was wondering if gsheets has the new capability available in Google Drive API v3 to open spreadsheets on Team Drives. If so, how do I specify the fact I want to open a spreadsheet on a Google Team Drive and not my own Google drive? If not, when will that functionality be available? Thanks!

wks.range function doesn't work

Traceback (most recent call last):
File "C:\Users\jason\Google Drive\python\custom3.py ", line 11, in
cell_list = wks.range('A1:C7')
File "C:\Python27\lib\site-packages\pygsheets\worksheet.py", line 177, in range
return self.get_values(startcell, endcell, returnas='cell')
File "C:\Python27\lib\site-packages\pygsheets\worksheet.py", line 212, in get_values
values = self.client.get_range(self.spreadsheet.id, self._get_range(start, end), majdim.upper())
File "C:\Python27\lib\site-packages\pygsheets\client.py", line 308, in get_range
result = self._execute_request(spreadsheet_id, request, False)
File "C:\Python27\lib\site-packages\pygsheets\client.py", line 378, in _execute_request
raise RequestError
RequestError

get content of 2 rows

Hello,

I have tried the examples and have made the API working, and its pretty impressive^^ (great job :) )
But I would like to get the content of 2 rows and save it in a variable, to save it in a file, which I proceed with a other program.
Is this possible and if yes, how I can get the content?

NaN written to google sheet on empty cell value

I noticed when reading an XLSX files to a DF and then writing that DF to google sheets using the set_dataframe method, NaN is written to cells of the google sheet if the cell value was empty. I'm thinking it would be best to just leave the cell empty as opposed to writing a NaN in the google sheet? If you agree, please update. thx.

global name 'flags' is not defined

I got an error
"global name 'flags' is not defined" when I try
gc = pygsheets.authorize()
I guess its from get_credential() method from client.py
I am using python 2.7
can u help with these ?

Worksheet.Updated

Nithin,

I'd like to implement the worksheet.updated method. Were you thinking of using the modified time attribute to get the last time the spreadsheet was modified?

RequestError: Timeout

I just started using pygsheet and am try a few basic operations.

Here is my flow:

  1. read 2 DFs from sheets (pass)
  2. write the 2 DFs to 2 csv files and 1 xlsx w 2 tabs using pandas (pass)
  3. read the 2 csv files using pandas (pass)
  4. cleanup the csv files by replacing NaN w/ zero. (note: before NaN cleanup, i received "HttpError: <HttpError 400 when requesting" .... "Invalid JSON payload received. Unexpected token." After cleanup no longer seeing the 400 error.
  5. writing both DFs to sheets using 'set_dataframe'
    1st DF (3767x124) => fails w/ below error
    2nd DF (32x18) => pass no error, written correctly

ERROR: "RequestError: Timeout"

Is there a limit on the DF size either by Goggle Sheets API V4 or pygsheets?

I hope my explanation above is clear. Any help or guidance will be appreciated.

thank-you,
--Rajeev

set_dataframe fails when index is type int

Hi,

tool is great but I think I found a little bug. When attempting at writing a DataFrame with int type index the code breaks returning

"Object of type 'int64' is not JSON serializable"

Here below my dataframe,

a b c d foo
0 -0.239328 -0.607873 0.939999 0.748753 =2*B2
1 0.806135 1.144164 -1.027552 1.181200 =2*B2
2 0.222877 -1.098671 -0.177695 -1.250602 =2*B2
3 1.554680 0.013423 1.269516 2.295749 =2*B2
4 0.962001 0.689026 0.020453 1.875726 =2*B2
5 -0.164626 -1.161630 0.736164 0.119247 =2*B2
6 -0.214339 1.506794 -0.117659 -2.283946 =2*B2
7 -0.208547 0.841456 -1.053574 -0.898233 =2*B2
8 -2.227894 -0.141375 -0.196319 1.287689 =2*B2
9 -0.137098 0.535600 -0.157131 0.756394 =2*B2
10 0.203198 -0.074702 0.020814 1.172994 =2*B2
11 0.544010 0.644651 -0.902793 -0.554629 =2*B2
12 -0.003178 0.961123 -0.346739 0.355867 =2*B2
13 0.401297 0.155124 0.968819 0.040641 =2*B2
14 0.359993 1.101107 0.149783 -0.019702 =2*B2
15 -0.574370 -0.224871 0.358658 0.129561 =2*B2
16 3.596789 -0.083012 -0.071461 0.058319 =2*B2
17 0.596427 0.258036 0.940905 0.005854 =2*B2
18 1.080344 0.295176 1.781685 0.557361 =2*B2
19 1.223156 -2.268100 0.662699 0.028708 =2*B2
20 0.157471 -0.439025 -0.659790 -0.834769 =2*B2
21 -1.181479 0.260105 -2.195900 -1.110619 =2*B2
22 0.028138 1.403058 0.657363 -0.901635 =2*B2
23 0.158918 0.890882 -2.372174 0.679946 =2*B2
24 0.208674 -1.309359 -1.134994 -0.151899 =2*B2
25 0.513415 0.907455 0.846958 -2.056828 =2*B2
26 0.227088 0.965421 -0.897194 -0.842636 =2*B2
27 -0.043356 0.946692 -0.430520 1.457925 =2*B2
28 -0.199506 0.232850 -2.670447 -0.371889 =2*B2
29 0.522627 -0.715304 0.894553 -0.824867 =2*B2

Thanks,
Umberto

Keep Signon Token Longer

After developing my script for a bit, walking away to get lunch and then returning, my script needed to re-authenticate. This happened again this afternoon as well. Is there a way to keep the token valid forever?

Please close this if I am just missing how this can be done.

'dict_items' object does not support indexing

In setting a value into a cell, I get the the error "TypeError: 'dict_items' object does not support index"
running python 3.4.2 I should note that the action completes properly, it merely crashes afterwards. Wrapping a try around the last line handles the error but doesn't seem like it should happen.

Here's the code that triggers the error.
a1=wks.cell('A1')
a1.col=int(colrow[0])
a1.row=int(colrow[1])
newval=entities[0]['value']
a1.value=newval

TrackBack:
File "/home/pi/WatsonSyllabusBot/syllabusbot.py", line 423, in getAttendance a1.value=newval
File "/usr/local/lib/python3.4/dist-packages/pygsheets/cell.py", line 88, in value self.fetch()
File "/usr/local/lib/python3.4/dist-packages/pygsheets/cell.py", line 201, in fetch self._unformated_value = result['effectiveValue'].items()[0][1]
TypeError: 'dict_items' object does not support indexing

Virutalwrapper and/or dir struct causing client to fail?

Hello,

I'm trying to give this module a try and it looks as though it's looking for a data_path in the wrong place.

Here the client tries to construct a path:
def __init__(self, auth): self.auth = auth http = auth.authorize(httplib2.Http(cache="/tmp/.pygsheets_cache", timeout=10)) data_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "data")
Where is this path actually supposed to point? I can't find a data file/folder anywhere that seems relevant. This is causing pygsheets to not be able to locate sheets_discovery.json.

I have a feeling this is caused due to the use of __file__.

Insufficient tokens for quota 'ReadGroup'

Hi,
Can you shed some light on this error:
Error: <class 'googleapiclient.errors.HttpError'> <HttpError 429 when requesting https://sheets.googleapis.com/v4/spreadsheets/1YSo5QBlo6eagKruQIVCGmg-0q9emCKp3e892sR1o430/values/Case%20Units%21B1%3AB16?valueRenderOption=FORMATTED_VALUE&alt=json&majorDimension=COLUMNS returned "Insufficient tokens for quota 'ReadGroup' and limit 'USER-100s' of service 'sheets.googleapis.com' for consumer 'project_number:247898894239'.">
Is there way to increse this limit?
-- jacek

Using set_dataframe - HttpError 400

I'm trying to set a pandas dataframe directly into a sheet and I'm getting the error
<HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/sheetidremoved forthisrequest/values/billing_data%21B1%3AT105?valueInputOption=USER_ENTERED&alt=json returned "Invalid values[0][0]: list_value {
values {
string_value: "Timestamp"
}
values {
string_value: ""
}
}">

I've set the Timestamp to a str and set all nan's to 0

I'm using wks.set_dataframe(df,(1,2),copy_head=True, fit=True)

If I don't convert the 'Timestamp' to string I get "TypeError: Timestamp('2017-02-12 00:00:00') is not JSON serializable" error

Can you please let me know why this is happening?

Thanks

How to use find?

Hi

I don't understand, how to use find function. Can you explain?

sh = gc.open("google_spr")
wks = sh.worksheet('title', 'inbox')
to_find = 'hello'
wks.find(to_find) ???

Worksheet.insert_rows() can't insert new rows to a single-row sheet

I discovered this issue while writing an app that automates the creation of a new worksheet report in Google Sheets for my company.

How to replicate:

I use a public spreadsheet to replicate this error. You can access it as follows:

# Authorize gc with your own credentials
sh = gc.open_by_url('https://docs.google.com/spreadsheets/d/1NrB3gwsvF-zQFVS5ADVaJY8OUGYwhGSue3gd3CCOfl8/edit')
# Open worksheet titled `Single-row sheet`
ws = sh.worksheet_by_title('Single-row sheet')
# Insert two rows after row 1
ws.insert_rows(1, 2)

I get this error:

HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1NrB3gwsvF-zQFVS5ADVaJY8OUGYwhGSue3gd3CCOfl8:batchUpdate?alt=json returned "Invalid requests[0].insertDimension: range.startIndex must be less than the grid size (1) if inheritFromBefore is false.">

My guess is that you set inheritFromBefore to False. Is there a way for you to either set inheritFromBefore to True, or expose that parameter through insert_rows() method for users to decide? Thanks in advance.

On update, "The caller does not have permission", even though the client_email has access

Thank you very much for publishing this module!

This is probably more of a support request than a bug report.

I created a service account, and got a JSON authentication file. I shared the spreadsheet that I need with the email address that appears in the "client_email" field in the file, and gave it Edit permission.

I am able to read the cells, but I cannot update a cell. I get the following error:

Traceback (most recent call last):
  File "sheet.py", line 29, in <module>
    wikipedia_sheet.update_cell('JV2', "Hallo!")
  File "/Users/aaharoni/Library/Python/2.7/lib/python/site-packages/pygsheets/worksheet.py", line 324, in update_cell
    self.client.sh_update_range(self.spreadsheet.id, body, self.spreadsheet.batch_mode, parse)
  File "/Users/aaharoni/Library/Python/2.7/lib/python/site-packages/pygsheets/client.py", line 329, in sh_update_range
    self._execute_request(spreadsheet_id, final_request, batch)
  File "/Users/aaharoni/Library/Python/2.7/lib/python/site-packages/pygsheets/client.py", line 373, in _execute_request
    response = request.execute()
  File "/Users/aaharoni/Library/Python/2.7/lib/python/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Users/aaharoni/Library/Python/2.7/lib/python/site-packages/googleapiclient/http.py", line 840, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/1ay77iIBXD1wZhYOiqXpiGeZ5RKcSGxmVnzRF5Y9dZk4/values/Wikipedia%21JV2%3AJV2?alt=json&valueInputOption=USER_ENTERED returned "The caller does not have permission">

Do you have an idea of what am I doing incorrectly?

Thank you!

worksheet.values include_empty option bug?

In the worksheet.values function, the include_empty option is supposed to
to remove trailing, empty cells. But, in this example, cells are deleted which are not empty, which I believe is a bug.

import pygsheets

SHEET_NAME='Example'
client = pygsheets.authorize()
worksheet = client.create(SHEET_NAME).sheet1

# Fill sheet with some example numbers
counter = 0
for i in range(4):
    for j in range(4):
        worksheet[i][j] = counter
        counter += 1

print worksheet.values((1, 2), (3, 3), include_empty=False)
# [ [2,3], [6,7], [10,11] ]
print worksheet.values((1, 2), (3, 3), include_empty=True)
# [ [2,6,10]
# The second column of the data is deleted.

As there are no empty cells in the range specified, I don't see why the option should result in different output.

set_dataframe and numerize

When I use set_dataframe with numbers as string (for example "01"), I receive numbers in google spreadsheet (like "1").

Any ideas to fix it?

Clear all rows in sheet except for the first

It would be really useful to be able to clear a sheet rather than have to delete and recreate it. Ideally I'd love to have a helper function that would auto-delete all except the first row.

I suppose I could select all of the rows, and then iterate and update manually, but I see that as painful.

Thoughts?

Team Drive Access is Working But not for Team Drives I don't own

Hi Nithan,

The Team Drive functionality is working for Team Drives I own. However, I want to use the same creds (i.e., the email address) on a Team Drive I do not own. When I run the same exact test using that Team Drive ID for an existing spreadsheet, this is the result:

C:\Users\xxx\Downloads\Nithin\test>python Chris_team_drive.py
service_email : [email protected]
Traceback (most recent call last):
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygsheets-1.1.1-py3.6.egg\pygsheets\client.py", line 154, in open
ssheet_id = [x['id'] for x in self._spreadsheeets if x["name"] == title][0]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "Chris_team_drive.py", line 15, in
ss = gc.open('Plug-in Costs')
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygsheets-1.1.1-py3.6.egg\pygsheets\client.py", line 157, in open
self._fetch_sheets()
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygsheets-1.1.1-py3.6.egg\pygsheets\client.py", line 86, in _fetch_sheets
results = self._execute_request(None, request, False)
File "C:\Users\salech01\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygsheets-1.1.1-py3.6.egg\pygsheets\client.py", line 381, in _execute_request
response = request.execute()
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\oauth2client-4.1.0-py3.6.egg\oauth2client_helpers.py", line 133, in positional_wrapper
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\google_api_python_client-1.6.2-py3.6.egg\googleapiclient\http.py", line 840, in execute
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/drive/v3/files?corpora=teamDrive&pageSize=500&fields=files%28id%2C+name%29&teamDriveId=xxx%60&q=mimeType%3D%27application%2Fvnd.google-apps.spreadsheet%27&supportsTeamDrives=true&includeTeamDriveItems=true&alt=json returned "Team Drive not found: xxx`">

C:\Users\xxx\Downloads\Nithin\test>

Are there other attributes that can be provided that would make this work for Team Drive I do not own?

A few inconsistency in README.md

wks.update_cells(range='A1:E10', values=values_mat)
I think para range should be crange according to source

in cell operation,

cell_list = worksheet.range('A1:C7')
for cell in cell_list:
cell.value = 'O_0'

worksheet.range('A1:C7') gives a list of lists, then each cell in cell_list would be a list of cells, therefore cell would not have value member

Pip install problem

Hi,

I have a problem with dependencies when i install pygsheets via pip. The 'enum' package cannot be installed

pip install pygsheets  
Collecting pygsheets
  Using cached pygsheets-0.2.1.tar.gz
Requirement already satisfied: google-api-python-client in /media/dane/sandbox/research/venv/lib/python3.5/site-packages (from pygsheets)
Collecting enum (from pygsheets)
  Using cached enum-0.4.6.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/media/dane/sandbox/research/venv/lib/python3.5/site-packages/setuptools/__init__.py", line 12, in <module>
        import setuptools.version
      File "/media/dane/sandbox/research/venv/lib/python3.5/site-packages/setuptools/version.py", line 1, in <module>
        import pkg_resources
      File "/media/dane/sandbox/research/venv/lib/python3.5/site-packages/pkg_resources/__init__.py", line 33, in <module>
        import platform
      File "/usr/lib/python3.5/platform.py", line 117, in <module>
        import sys, os, re, subprocess
      File "/usr/lib/python3.5/subprocess.py", line 364, in <module>
        import signal
      File "/usr/lib/python3.5/signal.py", line 4, in <module>
        from enum import IntEnum as _IntEnum
    ImportError: cannot import name 'IntEnum'
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-ehvht6nd/enum/

I tried install it manually and when I remove 'enum' from the requirements in setup.py, the installation process passed but I cannot import pygsheets:

Python 3.5.1+ (default, Mar 30 2016, 22:46:26) 
[GCC 5.3.1 20160330] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygsheets
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/media/dane/sandbox/research/venv/lib/python3.5/site-packages/pygsheets-0.2.1-py3.5.egg/pygsheets/__init__.py", line 15, in <module>
  File "/media/dane/sandbox/research/venv/lib/python3.5/site-packages/pygsheets-0.2.1-py3.5.egg/pygsheets/client.py", line 14, in <module>
  File "/media/dane/sandbox/research/venv/lib/python3.5/site-packages/pygsheets-0.2.1-py3.5.egg/pygsheets/models.py", line 17, in <module>
ImportError: No module named 'custom_types'

As you can see I'm using Python and virtual-env.

Different Users Access error

Hi,
I have two processes accessing one Google sheet - when second process is accessing the sheet (diffrent user) I am getting this error:
Unexpected error: <type 'exceptions.IOError'> [Errno 13] Permission denied: '/tmp/
.pygsheets_cache/sheets.googleapis.com,v4,spreadsheets,1YSo5QBlo6eagKruQIVCGmg-Oq9emCKp3e892sR1o430,fields=properties%2Csheets%2Fproperties%2Cspr
eadsheetId%2CnamedRanges&alt=json&includeGridData=false,ded17f3e63d32f48ef171491c49e4a63'

-- Jacek

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.