Google Sheets formula that includes values from another cell - json

In Google Sheets, I have a formula in a cell that connects to an API with a script and spits out the JSON results. For example, I can connect to the Youtube API with a formula that looks like this,
=ImportJSON("https://www.googleapis.com/youtube/v3/videos?id=mv-cj6mBkPk&key=API KEY&fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,statistics")
I'd like to make part of that formula get it's value from a separate cell. For example, the video ID above (mv-cj6mBkPk), rather than have it typed in the above formula, I'd like to pull in the value from a cell (i.e. B1). Then I can create multiple versions of this formula with other video IDs (i.e B2, B3, etc.)
I've tried combining various parts of the formula through CONCATENATE-ing a few cells, but that doesn't seem to 'RUN' the formula, it just shows it.
My skills aren't so advanced in this area, so any help would be great. Thanks!!

It is not obvious what your problem is exactly, or where you got the code for the ImportJSON() function (as this is not a built-in function),
but a variation of the below SHOULD work - if it does not, then post what error you are getting (exactly).
Also, I presume you have substituted "API KEY" for the actual key... I have used API_KEY, so that the formula does not get broken across multiple lines here.
A1 : mv-cj6mBkPk
A2 : =ImportJSON("https://www.googleapis.com/youtube/v3/videos?id="&A1&"&key=API_KEY&fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,statistics")

Related

Google sheets' IMPORTHTML() fails to keep the original format of the data

I want to use IMPORTHTML() to import data from a table at http://www.cophieu68.vn/atbottom.php; but IMPORTHTML() fails to keep the original format of the data, causing misleading information.
Specifically, I enter =IMPORTHTML("http://www.cophieu68.vn/atbottom.php";"table"; 2) into A1 cell of Google sheets. Google sheets successfully imports Table 2, but wrongly adds symbol * into my data. Consequently, SVD becomes starSVDstar (Please note cell B2 on my screenshot below) or 9.3 become star9.3star (Please note cell C2).
Moreover; 401,300 becomes 401,3 (Please note cell H2 ).
Besides, 29.1 is understood as 29/01/2021 (Please note my mouse click at cell K2 on my screenshot below)
Here is the screenshot of the original table that i want to import:
https://drive.google.com/file/d/1wgJcO4O-ivpsXk0XetXzwdhSs4d_8Cjy/view?usp=sharing
and here is the screenshot of the table which is imported into my Google sheets
https://drive.google.com/file/d/1DLipA3o85MTGo2ktumTU_0A45vhuLSA6/view?usp=sharing
Could anyone tell me what is wrong with my formula and how can I fix this error?
Thank you very much for your help.
Cao
Explanation:
The source page HTML is controlled by JavaScript, so the raw output of =IMPORTHTML cannot be changed whatsoever. However, you can use this formula on a new sheet to remove the asterisks from the original table.
If your IMPORTHTML is in Sheet1 you can use this:
=ARRAYFORMULA(SUBSTITUTE(Sheet1!A2:M,"*",""))
I suggest using regular expressions to solve the table import with a single formula:
=arrayformula(regexreplace(to_text(IMPORTHTML ("http://www.cophieu68.vn/atbottom.php ", "table", 2)),"\*(.*\/?)\*","$1"))
(I have added the function to_text to avoid losing the two zeros at the end of figures that are in units of thousands)

Remove characters from a cell with imported API data in Google Sheets - or format api import

I'll start by saying that my knowledge on using APIs is extremely limited. I'm impressed I've gotten as far as I have on this.
I've created a workbook in Google Sheets with imported data from the iexcloud API, which I'm using for data on stocks.
The requests have a cell reference in them so they update whenever a different symbol is selected.
So far, everything I've needed to request from it has the option to format as csv, so I can get cells with just the values.
However, this last thing I want doesn't have that option, so the whole response is wrapped in ["" ].
That really messes up what I need it for.
Here's an example
["PSA" CCI SHO ACC]
with each symbol being in its own cell.
I'm using the Peer Groups request.
A sample request:
> https://sandbox.iexapis.com/stable/stock/aapl/peers?token=Tsk_2b4c7c6fd98542f6a99f904cb7a3e721
Using Find and Replace doesn't work. I'm assuming because it's imported.
I need to use the cells with those symbols: PSA, CCI, SHO, ACC to reference in another request.
I recreated this in another Google Sheet that you can edit. The section in question in highlighted in blue
https://docs.google.com/spreadsheets/d/1BQ6FBD0S2YkDtDGZGIkDmQoKrQT4VmVDjuNsgV4mrXM/edit?usp=sharing
So I'm wondering if there's a way to have [ " ] automatically removed from any cells in that row, or if I copy and paste the values only, to have the values updated when the original cells are updated with new symbols (since I can have those characters removed in that row)
Or if there's a way I can format the response in sheets.
Any ideas?
I believe your goal as follows.
You want to achieve from ["CCI" SBAC CTL TDS RCI RCI-A-CT DTEGY] to CCI SBAC CTL TDS RCI RCI-A-CT DTEGY using the built-in functions of Google Spreadsheet.
Modified formula:
=ARRAYFORMULA(REGEXREPLACE(IMPORTDATA("https://cloud.iexapis.com/stable/stock/"&B3&"/peers?format=psv&token=###"),"[\[\]""]",""))
In this modified formula, [, ] and " are removed using REGEXREPLACE.
Please replace ### with your token at the above formula.
Result:
In this result, the values retrieved with =IMPORTDATA("https://cloud.iexapis.com/stable/stock/"&B3&"/peers?format=psv&token=###") are used. So the formula of cell "C9" is =ARRAYFORMULA(REGEXREPLACE(C6:I6,"[\[\]""]","")). But in this case, above modified formula can be used.
Note:
In this answer, I removed your token because I thought that it is your personal information.
Reference:
REGEXREPLACE

How to send cell data from one google spreadsheet to another, but only if the cell colour is correct?

I have a spreadsheet with cells coloured in two different colours. I know I can send all the cell data from one google spreadsheet to another using IMPORTRANGE function. However, I only want to send the cell data if it satisfies a specified cell colour.
For example, if spreadsheet A has 10x10 data with various colours, then spreadsheet B should contain all the data from cells in spreadsheet A that are either red or green (and also transfer the cell colours). All other cells with different colours from spreadsheet A should be transferred to spreadsheet B as blank colourless cells. The resulting spreadsheet should still contain 10x10 cell data, but with only red, green and blank cells.
I know it should be possible to write a function for this, but I have never written any custom functions before and have no Javascript experience. Any kind of help would be appreciated. Perhaps also the QUERY function could be of use?
Thanks in advance!
You should check about Google Apps Script. It gives you a set of tools that will allow you to create a script for doing what you want.
Custom Functions will help you to create a function that lets you get the values from your sheet and then set the conditions you are requiring.
The Class SpreadsheetApp has the tools for handling all data in your sheets. Check for example the method getBackgrounds(), which gets the color in a range of cells.
This another post, it is a little similar in some aspects to what you want to do.
It's best practice to create an additional column which stores the information regarding as to which condition (color) is applied 5o the particular row. Once you have done that, you can easily transport a table from one Spreadsheet to another using the QUERY formula within the IMPORTRANGE.
Image column a is the name of a city.
Imagine column b holds the information regarding the condition (color). This is an helper column.
Now we have col1 = New York City, col2= green
Then you could enter this into the new sheet.
QUERY( IMPORTRANGE(URL, range), "SELECT col1 WHERE col2="green" OR col2="yellow" OR col2="red")
Here is a great tutorial series I like to use.
https://youtu.be/_N5zhAipVn0

use JOIN to pull multiple IMPORTRANGES into SORT(ARRAYFORMULA({importrange1; importrange2; etc}

How can I use JOIN (and maybe VLOOKUP? FILTER?) to make a list of IMPORTRANGES, resulting in something like {IMPORTRANGE(C3,$E$1); IMPORTRANGE(C4,$E$1); IMPORTRANGE(C5,$E$1); IMPORTRANGE...}?
Currently, in a google sheet, I have a formula that looks like this: =SORT(ARRAYFORMULA({IMPORTRANGE(C3,$E$1);IMPORTRANGE(C4,$E$1);IMPORTRANGE(C5,$E$1);IMPORTRANGE..." where spreadsheet urls are in Col C and a range (same for every imported sheet) is in E1.
Typing it all in was fine when I only had about a dozen spreadsheets I was importing and they all already existed. But now I want to import many more spreadsheets (I heard that the limit of 50 importranges no longer applies) and they don't all exist yet. If I keep things as they are, every time I add another spreadsheet url to Column C, I'll also have to go in and edit my =SORT formula.
Then I found this thread, Fill ArrayFormula with dynamic ImportRange, which has a suggested answer listed as: ="=sort(ARRAYFORMULA({"&JOIN(";",ArrayFormula("IMPORTRANGE("""&VLOOKUP(FILTER(G2:G20,G2:G20<>""),Sheet3!$A$2:$B,2,0)&""","""&G1&"!A2:B"")"))&"}),1,True,2,True)"
The JOIN in there looks intriguing (I just recently learned about JOIN) but I don't understand all the syntax (like """) and I also can't access to original spreadsheet to see what the references point to.
So, I'm looking for help in how to input the C3, C4, C5, etc into the JOIN -- not sure how the VLOOKUP helps me -- and also if anyone knows why there are so many ="&=&"""s throughout the suggested formula...
One of the solutions below may do what you need. The first is what you requested. The second is the one which will probably work to combine multiple IMPORTRANGE formulas.
SOLUTION 1
="="&ArrayFormula(REGEXREPLACE(QUERY(UNIQUE(TRANSPOSE(SPLIT(CONCATENATE(IF(E3:E="","","IMPORTRANGE("&E3:E&", $E$1)"&":")),":"))),,9^99),"\)(.*?)I","\), I"))
SOLUTION 2
="=QUERY({"&ArrayFormula(REGEXREPLACE(QUERY(UNIQUE(TRANSPOSE(SPLIT(CONCATENATE(IF(E3:E="","","IMPORTRANGE("&E3:E&", $E$1)"&":")),":"))),,9^99),"\)(.*?)I","\); I")&"},""Select Col1 where Col1<>''"",0)")
You would then just copy the cell, select the destination cell and "paste as values" into the formula bar.
You can see the sheet in action HERE (just make a copy of it to use).

How to use a formula written as a string in another cell [evaluate for Google Spreadsheet] [duplicate]

This question already has answers here:
Is there a way to evaluate a formula that is stored in a cell?
(13 answers)
Closed last month.
I read several old posts about Google Spreadsheet missing the evaluate function.
There is any solution in 2016?
The easiest example.
'A1' contains the following string: UNIQUE(C1:C5)
'B1' I want to evaluate in it the unique formula written in 'A1'.
I've tried concatenating in this way: 'B1' containing ="="&A1 but the outcome is the string =UNIQUE(C1:C5).
I've also tried the indirect formula.
Any suggestion to break last hopes, please?
Additional note
The aim is to write formulas in a spreadsheet and use these formulas by several other spreadsheets. Therefore, any change has to be done in one place.
Short answer
Use a script that includes something like var formula = origin.getValue() to get the string and something like destination.setFormula(formula) to return the formula.
Explanation
As was already mentioned by the OP, Google Sheets doesn't have a EVALUATE() built-in function. A custom function can't be used because custom functions can only return one or multiple values but can't modify other cell properties.
A script triggered by a custom menu, events or from the Google Apps Script editor could be used to update the formulas of the specified cells.
Since the formulas will be kept as strings, it could be more easy to keep them in the script rather than in the spreadsheet itself.
Example
The following is a very simple script that adds the specified formula to the active range.
function addFormula() {
var formula = '=UNIQUE(C1:C5)';
var range = SpreadsheetApp.getActiveRange();
range.setFormula(formula);
}
I have a solution for my own use case. My investment broker exports data to its users in (badly-formatted) Excel. I do my own analysis in Google Sheets. I have found copy/pasting entire sheets of data to be accident-prone.
I have partially automated updating each tab of the records. In the sheet where I maintain all the records, the First tab is named "Summary"
Save the broker's .xlsx data to Google Sheets (File | Save as Google Sheets);
In the tab named Summary, enter into a cell, say "Summary!A1" the URL of this Google Sheet;
In cell A2 enter: =Char(34)&","&CHAR(34)&"Balances!A1:L5"&Char(34)&")"
In the next tab, enter in cell A1: ="IMPORTRANGE("&Char(34)&Summary!A1&Summary!A2
The leading double quote ensures that the entry is saved as a text string.
Select and copy this text string
in cell A3, type an initial "=" + Paste Special.
This will produce an importrange of the desired text, starting at cell A3