Google spreadsheet API export update - google-sheets

We've previously been getting exports from the google spreadsheet api using the following url
https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=spreadsheetID&exportFormat=csv&gid=gid
When Google introduced the new Sheets this URL did not work for them, though did continue to work for old style spreadsheets. As a result we've been using the Google drive API to get the new style sheets, but this has some limitations as you must get the whole spreadsheet instead of an individual worksheet and it does not support CSV(your exports must be Excel).
Though after reading the following question : Is an API available for the new Google sheets (spreadsheets)? It looks like the new sheets are supported despite no information about it in the api documentation.
After digging through the response I found the new sheets have an export link included as field in the format:
https://docs.google.com/spreadsheets/d/spreadsheetID/export?format=csv&gid=gid
Which works great. The problem is that the old sheets don't work with the new URL andthere is no field specifying that it is a new sheet and it must be inferred from the existence of the field.
So I'm wondering if there is a better way to determine if I'm dealing with an old or new sheet and if this is a reliable way to get spreadsheet data as it doesn't seem to be documented.
Thanks!

Google spreadsheet API (GData style) works the same for both old and new style sheets.
Possible issues:
1) non public sheets require OAuth2
2) The sheet id is not the GID. You need the GData style sheet ID to get the sheet. So it needs an API call to get the mappings from sheet name to sheet id
3) The raw format of the data is XML

Related

Get the list of all Google excel sheets associated with Google account

I want to make a application where
User Will authenticate with there Google account
Then can see all the sheets list there
They can select to edit or update there sheets or they can create a new sheet and input there data
How can i do this ?
I tried with google sheets api v4
Your app description exactly matches the functionality already provided by the Sheets start page. Use that instead of writing your own replica of the same.

Specifying the Title of a new Google Sheet using a URL Parameter (https://sheet.new?title=XXXXXXX)

Is there any way to specify the title of a new Google Sheet document, using ONLY URL parameters?
Example:
The following URL will open a new, blank Google Doc. Where "XXX"
is, you can specify what you want the title of your new document to
be:
https://docs.google.com/document/create?title=XXX
The same goes for creating a new Google Slides document:
https://docs.google.com/presentation/create?title="XXX"
However, if you apply the same pattern to a new Google Sheet, such as:
https://docs.google.com/spreadsheets/create?title="XXX"
This link WILL open a new Google Spreadsheet, but the title is blank.
So it seems this option is not working like the examples that you have provided.
A workaround that you could use is using the REST sheets API. Take a look at create method.
You could integrate this HTTP request in a lot of environment to simulate your behavior.
Alternatively you could report this behavior inside google Sheets itself. This seems like a reasonable request you could make.

How to update google spreadsheet via sheets API v4

I'm facing an issue within updating of existing spreadsheet on google drive, without using a batch update (right now I'm not sure whether it is even possible)
I already have existing spreadsheet with some data, then I retrieve file from google drive via
Drive drive = new Drive.Builder(TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
drive.files().list().execute().getItems()
I easily match my file by the name and then I just want to update the spreadsheet in the founded file.
Sheets service = new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
Spreadsheet spreadsheet = service.spreadsheets().get(file.getId()).execute().setSheets(new ArrayList<>());
Here I get spreadsheet from file which match the name and then I just want to put there empy sheet (for example).
There is first point, where I'm not sure how to correctly "update" the spreadsheet on drive, because I cannot use
service.spreadsheets().create(spreadsheet).execute();
and I don't want to use batch update, because I already have set the spreadsheet content via setSheet() method.
I think it should be finalized with following:
Permission permission = drive.permissions().get(file.getId(), permissionId).execute().setType("anyone");
drive.permissions().update(
spreadsheet.getSpreadsheetId(),
permissionId,
permission
).execute();
I was checking the google API here:
https://developers.google.com/drive/v2/reference/permissions/update
and here:
https://developers.google.com/sheets/api/guides/batchupdate
but they are using batch update.
What I am curious is why there is setSheets() method, which seems to be easily to use for updates, when I already created my sheets, but it seems that I just cannot simply update spreadsheet in existing file.
What is also interesting that every time I use the drive.perm.update, it creates a new file(with spreadsheet that I wanted to update) which actually doesn't make much sense to me.
So is there any chance to simply update file on drive and just use the spreadsheet method?
This statement by Jay Lee in this related SO post will give a clear insight:
The Google Drive API is meant for generic file / folder interactions. For directly modifying Google Spreadsheet data, the Google Sheets API would be the better choice.
Furthermore, to update Google spreadsheet, you may want to check Reading & Writing Cell Values which describes the basics of using the spreadsheets.values collection. It was mentioned that,
If you need to update formatting or other properties in a sheet, you will need to use the spreadsheets collection, which is described in Updating Spreadsheets.
For more information, please see Batch update operations and examples are shown here.

Has the Google Sheets Published URL Suddenly Changed to a Different Format?

Normally, when I use the Google Sheets API, I get a very predictable URL structure from the "Publish Sheet" menu option, that I use to extract the Spreadsheet ID with a regular expression and use it for other tasks on the Google Sheets API.
This has worked for years and is the way that Google's documentation recommends getting the Spreadsheet ID - from the URL.
e.g.
https://docs.google.com/spreadsheets/d/{MYSPREADSHEETID}/pubhtml
However, as of today, when publishing a spreadsheet, I now get a URL like this:
https://docs.google.com/spreadsheets/d/e/2PACX{BUNCH OF RANDOM CHARACTERS}/pubhtml
This breaks my code as the bunch of random characters that appears with 2PAC is not the spreadsheet ID and does not work with the API.
Does anyone know if this is an unannounced change to Google's URL structure or some kind of bug?
I have no idea when or why Google has decided to change their URL structure. The Google Sheets API Documentation states to pull the spreadsheet ID from the editing URL. Google Sheets API Documentation It seems unlikely to me that this is a bug of some kind, since this has been going on for a while, and to me, seems permanent.
The solution to this problem would be to pull the spreadsheet ID from the editing (or the sharing URL) URL itself instead of using the URL of the published sheet.
I hope Google fixes this issue as this affects consistency across their URLs but for now, the only way to retrieve the spreadsheet ID is to get it from the editing or sharing URLs.
Hope this helps! :)

Google Docs Publish to the Web Data parsing

I have a trouble with parsing of data from google spreadsheet api. I have a spreadsheet with 3 pages and I need to get data from there and parse them to array where key is cell position and value is value of cell.
When I go to File->Publish to the web with option "All sheets" and target format is CSV seperated with comas I get a link which is targeting data with just values of cells and just from first page.
Here is any way how to get data from all sheets and with information where are placed in document (cell name (code))?
Thank you all!
You can use Google Spreadsheet API, for example using its Java Library
or you can use Google Apps Script and its spreadsheet methods
both of them are giving you the ability to have your data live in Google spreadsheet and at the same time access it from other clients or sites. You don't need to export it and lose the ability to have it updated across all your clients.

Resources