Gspread update value without getting it first / addition to cell directly - python

Currently using gspread and python. Currently to update values on my spreadsheet I have to grab values from sheet, update them and then write back.
What i am looking for is...say I have a cell with a value of 5, is there a way to make an api call that says add +5 to "x" cell? I would no longer need to grab the values first, which even saves an api call. I don't know if this command is available or anything similar is, but would appreciate any help here!

Poring over both the documentation for gspread and the Sheets API I'm fairly confident that this cannot be done. All the methods that update values on a sheet require you to specify a range and the list of values, but you are unable to refer to the current values within the range without first retrieving them.
Your best way to reduce API calls is to just retrieve and update the data in batches as much as possible using batch_get() and batch_update().
If you'd like to submit feedback to Google to implement something like this in a future API update you can check out their issue tracker.

Related

Using smartsheet-python-sdk to put data back into Smartsheet

I used the smartsheet-python-sdk (with a unique API key from Smartsheet) to automatically pull data from Smartsheet into my Python script along with other data sources (from Tableau) to create new feature-engineered columns. I now want to put these new columns I've created back into the same Smartsheet file I initially pulled from. Is there an automatic way to put these new columns I created back into the same Smartsheet I initially pulled data from using the smartsheet-python-sdk? Thank you!
Yes, the REST API documentation shows how to do it via the Python API in the examples on the right sidebar.
I find the Python API woefully under-documented, but a) it closely parallels the REST API with a few differences, like parameters being in Pythonic snake_case instead of JS camelCase; and b) the examples are usually enough to get you there.

gspread: Retrieve Filtered Data from GoogleSheets

I have a google sheet with a filter/query that only shows the data that verifies the filter's conditions. To retrieve the data for python I use gspread, but hiddenrows are appearing too (as if there was no filter at all).
How can I differentiate the rows selected from the ones who weren't?
I don't understand if this can work without adding more functions to gspread, or if I need to create a new function. If so, what should the function be?
I found that function fetch_sheet_metadata() which is inside class spreadsheet of gspread gives out the filters and hiddenValues for each filter, which is enough to solve my problem.

Google Sheets API - How do do a batchUpdateRequest with append requests?

I'm running into a bit of a wall here.
I'm pulling in some data that I pull in via an API, which I transform and then append it to the bottom of a sheet in Google Sheets. For each line of data that I pull, I currently use an append() request.
I'm trying to reduce the amount of calls I make, and batchUpdate seems like a good start. However, out of the available options for batchUpdate, append doesn't seem to be present, or I'm misreading it.
My end goal is that I can get a bunch of data and append them to the bottom of a spreadsheet, instead of continuously calling the append endpoint.
It looks like you're just looking at the Values collection. It doesn't make much sense to do a batchAppend because all you'd be doing is appending to the end of the prior one.. so you might as well just make a single append call with all the data you'd like to append.
However, if you'd like to intermix updating various other aspects of the spreadsheet (not just the values), then you can use spreadsheets.batchUpdate. One of the kinds of requests that can do is an AppendCellsRequest.
However, it's not clear why you need to batch in the first place. With batchUpdate it makes sense because each update is going to an exact place in the sheet so you can bundle a lot of updates together. OTOH, "append" doesn't specify the location, it just says "go after the existing data", so there's no point to batching them up, you can just as easily make a single call w/ all the data you'd like to append.

Batch updating specific google spreadsheet cells using python gdata

I found some information here about updating several cells at once using the python gdata library.
However, the example code refers to cells based on a single index, for instance updating only the first entry of the spreadsheet:
batchRequest = gdata.spreadsheet.SpreadsheetsCellsFeed()
cells.entry[0].cell.inputValue = 'x'
batchRequest.AddUpdate(cells.entry[0])
Suppose I want to update specific cells knowing their location, e.g. R1C3 and R2C2. How would I go about doing this? In other words, what do I replace cells.entry[0] with to access a specific row and column?
This related answer would be promising, except all the links are dead.

Batch inserts into spreadsheet using python-gdata

I'm trying to use python-gdata to populate a worksheet in a spreadsheet. The problem is, updating individual cells is woefully slow. (By doing them one at a time, each request takes about 500ms!) Thus, I'm attempting to use the batch mechanism built into gdata to speed things up.
The problem is, I can't seem to insert new cells. I've scoured the web for examples, but I couldn't find any. This is my code, which I've adapted from an example in the documentation. (The documentation does not actually say how to insert cells, but it does show how to update cells. Since this is a new worksheet, it has no cells.)
Furthermore, with debugging enabled I can see that my requests returns HTTP 200 OK.
import time
import gdata.spreadsheet
import gdata.spreadsheet.service
import gdata.spreadsheets.data
email = '<snip>'
password = '<snip>'
spreadsheet_key = '<snip>'
worksheet_id = 'od6'
spr_client = gdata.spreadsheet.service.SpreadsheetsService()
spr_client.email = email
spr_client.password = password
spr_client.source = 'Example Spreadsheet Writing Application'
spr_client.ProgrammaticLogin()
# create a cells feed and batch request
cells = spr_client.GetCellsFeed(spreadsheet_key, worksheet_id)
batchRequest = gdata.spreadsheet.SpreadsheetsCellsFeed()
# create a cell entry
cell_entry = gdata.spreadsheet.SpreadsheetsCell()
cell_entry.cell = gdata.spreadsheet.Cell(inputValue="foo", text="bar", row='1', col='1')
# add the cell entry to the batch request
batchRequest.AddInsert(cell_entry)
# submit the batch request
updated = spr_client.ExecuteBatch(batchRequest, cells.GetBatchLink().href)
My hunch is that I'm simply misunderstanding the API, and that this should work with changes. Any help is much appreciated.
I recently ran across this as well (when trying to delete) but per the docs here it doesn't appear that batch insert or delete operations are supported:
A number of batch operations can be combined into a single request.
The two types of batch operations supported are query and update.
insert and delete are not supported because the cells feed cannot be
used to insert or delete cells. Remember that the worksheets feed must
be used to do that.
I'm not sure of your use case, but would using the ListFeed help at all? It still won't let you batch operations, so there will be the associated latency, but it may be more tolerable than what you're dealing with now (or were at the time).
As of Google I/O 2016, the latest Google Sheets API supports batch cell updates (and reads). Be aware however, that GData is now deprecated, along with most GData-based APIs, including your sample above as the new API is not GData. Also putting email addresses and passwords in plain text in code is a security risk, so new(er) Google APIs use OAuth2 for authorization. You need to get the latest Google APIs Client Library for Python. It's as easy as pip install -U google-api-python-client [or pip3 for Python 3].
As far as batch insert goes, here's a simple code sample. Assume you have multiple rows of data in rows. To mass-inject this into a Sheet, say with file ID SHEET_ID & starting at the upper-left in cell A1, you'd make one call like this:
SHEETS.spreadsheets().values().update(spreadsheetId=SHEET_ID, range='A1',
body={'values': rows}, valueInputOption='RAW').execute()
If you want a longer example, see the first video below where those rows are read out of a relational database. For those new to this API, here's one code sample from the official docs to help get you kickstarted. For slightly longer, more "real-world" examples, see these videos & blog posts:
Migrating SQL data to a Sheet plus code deep dive post
Formatting text using the Sheets API plus code deep dive post
Generating slides from spreadsheet data plus code deep dive post
The latest Sheets API provides features not available in older releases, namely giving developers programmatic document-oriented access to a Sheet as if you were using the user interface (create frozen rows, perform cell formatting, resizing rows/columns, adding pivot tables, creating charts, etc.)
However, to perform file-level access on Sheets, such as import/export, copy, move, rename, etc., you'd use the Google Drive API. Examples of using the Drive API:
Exporting a Google Sheet as CSV (blogpost)
"Poor man's plain text to PDF" converter (blogpost) (*)
(*) - TL;DR: upload plain text file to Drive, import/convert to Google Docs format, then export that Doc as PDF. Post above uses Drive API v2; this follow-up post describes migrating it to Drive API v3, and here's a developer video combining both "poor man's converter" posts.

Categories

Resources