Copy cells full and append to other page - python

I have a google sheet connect to a google form. The form is compiled by team leaders with his members and some informations that are reported in a row by google.
I need to make another sheet with all data from members in a single column.
I won't copy and paste 'cause there are more than 50 leaders and thousand of members and the real problem is that some rows are full and some, considering that not all teams are made up of the same number of members, are half empty.
What is the fastest way to complite the sheet?
I need something like:
Rows from google sheet
[Team1; Bob, data; Rob, data]
[Team2; Rose, data; Mark, data; Jenny, data]
Result that I want:
[
[Bob, data],
[Rob, data],
[Rose, data],
[Mark, data],
[Jenny, data],
]
If there's no way to do it internally to Google Sheet can I use python and think to google sheet like a matrix?

You CAN do it internally using Google Apps Script. This allows users to make custom functions and code in JavaScript which can make changes to sheets in Google Sheets or get info based on what code you write.
Here is the link for the Apps Script documentation which is quite well written.
You will basically need to create 2 sheets, write a function to extract the cells you want from each row, and then input that data into the second sheet. You do not need to be very well versed in JavaScript to do this, I myself am not adept at Javascript however I am able to make functions as per my need.
Also some advice, please test it with a sample sheet first so that you do not delete data or make errors.

Related

Parsing Excel sheet based on date, number of visitors, and printing email

I am trying to parse through an Excel sheet that has columns for the website name (column A), the number of visitors (F), a contact at that website's first name (B), one for last name (C), for email (E), and date it was last modified (L).
I want to write a python script that goes through the sheet and looks at sites that have been modified in the last 3 months and prints out the name of the website and an email.
It is pretty straightforward to do this. I think a little bit of googling can help you a lot. But in short, you need to use a library called Pandas which is a really powerful tool for handling spreadsheets, datasets, and table-based files.
Pandas documentation is very well written. You can use the tutorials provided within the documentation to work your way through the problem easily. However, I'll give you a brief overview of what you should do.
First open the spreadsheet (excel file) inside python using Pandas and load it into a data frame (read the docs and you'll understand).
Second Using one of the methods provided by pandas called where (actually there are a couple of methods) you can easily set a condition (like if date is older than some data) and get the masked data frame (which represents your spreadsheet) back from the method.

Python gdata Spreadsheet - save data to cell in SpreadsheetsListFeed

I'm using a SpreadsheetsListFeed to read data from a Google Spreadsheet.
The sheet contains a table of customer data, including firstname, lastname, email, phone, etc. The column headers are in the top row of the sheet, and the data is in the subsequent rows. I figured a SpreadsheetsListFeed would be the best option since the data is in a tabular format.
However, now I need to use the python app to generate a customerid for each customer, and write it to the spreadsheet (only if it doesn't already exist in the sheet). I'm wondering if there is a way to do this through the SpreadsheetsListFeed or if I need to use a SpreadsheetCellsFeed and calculate the row/column values based on the existing SpreadsheetsListFeed.
The documentation for the python gdata library is a little sparse, so any help would be appreciated. Thanks!
That's the reason why I wrote a small wrapper lib for the spreadsheet API. It lets you work with a spreadsheet like you would with a dict.

Any way to create an excel sheet with data from a url with python

Sorry if the title is confusing. Basically what I am trying to do is create an excel sheet with data that is in a url that I have.
The url is a search API for twitter that retrieves the past 100 tweets with a given keyword of my choice. I am trying to to create an excel sheet that stores each tweet in it's own row. Essentially it will only be 1 column but will be 100 rows.
I have looked online but haven't really seen a way to do exactly what I need so if anyone knows a tutorial i should look at or could show me how to get started that would be great.
Thanks!
There will probably not be a tutorial on exactly how to do this. you need to put a couple different concepts together
Get the data from the url. This can be as simple as urllib.urlopen
Turn that data (string) into a usable format. Twitter will probably return json. Turn that into a python dict
Open a file for writing
Loop through twitter data and write to ouput file
You only need to create a .csv file. It will work great with excel. For one column file you just need to write the header then write each line of data. Python provides everything you need to create well formed csv files in the csv module in the standard library

Google Spreadsheets API Column Titles

I am using the Google Spreadsheets API in python, and I can't get the column headers for the ListView of a worksheet.
Does anyone know how to get those titles? I can only find the underscore preceded hashes.
Take a look at a this library I created, its a simple wrapper to the spreadsheet API designed to make cases like the one you are facing much simpler to deal with.

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