How to retrieve specific column value in string format through google sheets API? - api

We have multiple columns in our google excel sheet as below,
image1
We are trying following API call,
https://sheets.googleapis.com/v4/spreadsheets/7dj1kU54fgrhQk46uMrqq-W-QMlBq45kjiuEdhfjm5zE5gNvUoLk/values/'sheet1'!A2:O200?dateTimeRenderOption=SERIAL_NUMBER&valueRenderOption=UNFORMATTED_VALUE&majorDimension=ROWS
By hitting the above call, we get the output as below,
image2
But, we want the value of column Currency same as available in the sheet. That is $667 where as we just get 667 by the above API call.
If we just replace the value of valueRenderOption with FORMATTED_VALUE, we get the output as below,
image3
But, then it returns the value of all columns as formatted. We just want to get the value particular column as FORMATTED_VALUE. Is there any by which we can do this?

Unfortunately not - you need to make two API calls if you want different cells to have different ValueRenderOptions. This is a limitation of the API itself, as the specified ValueRenderOption must be the same for the whole range in a single call.

Related

Google Sheet API 4: How to return cell string value with leading single quotes as-is?

Having a cell in Google Sheets with a value of '0123 and fetching it via the API it returns 0123 and omits the leading single quote.
Tried to use any of the available ValueRenderOption but always returns the cell value without single quote.
I would expect to get the single quote using the UNFORMATTED_VALUE or FORMULA value, but this is not the case.
Therefore: How to get the raw cell value, including leading single quotes?
I think this would be an expected behavior given the fact that the feature just makes the system recognize the values after the apostrophe as text, so it is more like the cell formatting feature rather than an actual formula but without changing the format of the whole cell and just the values next to the apostrophe.
After making some testing in the API explorer from the Google Sheets API I found out that if you remove the apostrophe the API will definitely recognize the values as numbers when using either UNFORMATTED_VALUE or FORMULA in the ValueRenderOption parameter, so the API will return number values in the response, however if you do it using '0123, what you get in the response instead of numbers is text (see screenshots below).
Number value
Text value
With all this we can determine that this would be expected and that adding the apostrophe symbol is an exception to the ValueRenderOption parameter due to how the API works. I have also submitted feedback in the documentation page to see of Google can add this information to the documentation since there is no mention of this in that page.
References:
Using Apostrophe to Add Leading Zero
Format numbers in a spreadsheet

API coingecko data - doNotDelete

I use Coingecko's importJSON code to load data into a Google Sheet. The last parameter of the url (see below) is called 'doNotDelete!$A$!'. If I run the importJSON function in my sheet a numeric value in cell A1 of the tab doNotDelete shows up. What does this number mean? Is it the number of seconds that past since last execution? There is no explanation about it in the Coingecko's article about their API ImportJSON function for Google Sheets.
=ImportJSON("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=250&page=2&sparkline=false","/name,/current_price,/market_cap,/price_change,/total_volume,/high_24h,/low_24h","noTruncate,noHeaders",doNotDelete!$A$1)
When the value of this cell changes ImportJSON gets updated values from the API. It doesn't have any meaningful value, your autorefresh script should change that to a random value whenever it is run.
It's not coingecko's script, it is from here and it is currently unmaintained, so if google changes the behavior of any of the used functions it can stop working.

Updating a cell value with unknown range (Google SpreadSheets API v4)

So, i've made an application that fetches data from Spreadsheet with a rest api call.
This data is being mapped from the ValueRange object to a list of custom object and cached in the application for 10 minutes.
I'd like to be able to update a single cell of a specified row of which I do not know the range value (for example 'E9') of.
Is there a good way to loop through the items in the spreadsheet and update a cell value where another field value matches?!
public static void UpdateQuantity(string nameToMatch, int quantity)
{
// find row where column A matches nameToMatch
// update value of column B with quantity
// finally clear cache to get the new data
CacheHelper.ClearCache();
}
If else, when iterating through rows in spreadsheet, how do I know the current row number so I would do a REST call to update the specific cell?
Thanks in advance.

Google Script Sheets API - default number format

It looks like google sheets is making the same mistake as Excel, by "thinking ahead" and converting the value "1.1.1" to 2001.01.01 when doing sheet.appendRow. I have tried to set the number format of the column in charge to "#" (which should be plain text) before inserting rows - but looks ineffective. On the other hand doing the same after inserts is also ineffective, as the content is already "date".
Adding ' before is working, but it is not what I need.
Is there any way to give a default format or to disable such automatic conversion (from google script)?
I have a cell in which a form deposits a variable number of values seperated by a comma such as: " 1, 2, 4, 6 " etc - when there are only three answers, Google "helps" me by converting the value into a date object. But it's supposed to be a list of choices...
It's not pretty, but I've managed a workaround by using .getDisplayValue instead of .getValue - it does change the cell value into a string, so if you need to do further manipulations that are dependent on the value being a number or something, obviously, this fails.
I overwrite the value for the problem cell in my array before passing it to .appendRow
//getting the values
var values = s.getRange(row,1,1,lastCol).getValues()[0];
//brute force crushing of problem value
values[5] = s.getRange(row,6).getDisplayValue();

Finding last written row in Google Spreadsheet API

Is there any way to find the last row you have written in a google spreadsheet in Java?
I tried to do that by having a variable which I keep in another file and I update that every time I do another writing. Is there any other way?
Finding last written row in Google Spreadsheet API
I'll give you a concept first using REST calls but you'll have to apply that in Java.
Exceed the range of cells where you're writing.
So to find last written row between cells A1:A10, use the range A1:A11 or A1:B (using B means all the rows in cell A will be traversed).
Fetch a range of cells, using GET. Parse the response result. And get the length of the parsed values. The length will always indicate the last row since Sheetsv4 ignores blank/empty cells.
So example I have a range of cells between A1:A10. I wrote "hello" in A10. Following the concepts above I do this:
..
xhr.open('GET', 'https://sheets.googleapis.com/v4/spreadsheets/'+myspreadsheetId+'/values/Sheet1!A1:A10);
..
xhr.onload = function (oEvent) {
arrayBuffer = xhr.response;
myArray = JSON.parse(arrayBuffer);
console.log("last row is " + myArray.values.length)
.. //output is 'last row is 10'
This part of my XHR request returns 10. Now I know that the last written row in my sheet is A10.
To do this in Java use the solution based on this SO thread.
mService.spreadsheets().values().get("ID_SHEET", "Sheet1!A1:B").execute();
Also check this thread about writing on data cells using Java. I think it offers additional insight as well.