Tabulate JSON into Sheets - json

I've been trying to get a readable database of a JSON file from a URL.
I've used fastfedora's script on Github, https://github.com/fastfedora/google-docs/blob/master/scripts/ImportJSON/Code.gs, to import JSON from the URL to Sheets. I'm using the basic:
=TRANSPOSE(ImportJSON("https://rsbuddy.com/exchange/summary.json"))
I used transpose as it was easier to work with two long columns rather than two long rows.
The data that's been imported however, is very messy: https://docs.google.com/spreadsheets/d/1mKnRQmshbi1YFG9HHg7-mKlZZzpgDME6-eGjDJKzbRY/edit?usp=sharing. It's basically 1 long column of descriptive data, (name, id, price etc.) and another column of the variable (the actual name of the item and it's price in digits).
Is it possible to manipulate the resultant Sheets page so that the common factors in the first column can be lined up with the pseudo-table beside two initial columns? E.g. for the first item, the ID will be '2', the name will be 'Cannonball', the Sp will be '5' etc.
Thanks in advance. Do forgive me for my ignorance.

Example
Simple formula
I think, faster way to get IDs:
=QUERY(QUERY(A2:B,"select B where A <> '' offset 4"),"skipping 7")
and if you want Names:
=QUERY(QUERY(A2:B,"select B where A <> '' offset 1"),"skipping 7")
when you change offset from 0 to 6, you get different columns
outputs.
7 is the number of columns in Data.
The result is autocompleted column with Data.
Hard formula
Also possible to get the whole result with one formula:
paste =COUNTA(A:A) in cell E2
paste 7 in E3, this is the number of columns in Data
=E2/E3 in E4
And then in cell G2 or somewhere on right paste the formula:
=ArrayFormula(vlookup(if(COLUMN(OFFSET(A1,,,1,E3)),
(row(OFFSET(A1,,,E4))-1)*E3+COLUMN(OFFSET(A1,,,1,E3))),
{row(OFFSET(A1,,,E2)),OFFSET(B2,,,E2)},2,0))
It works slow, but gives the whole table.
or Script?
I've also tried to use script UDF function. Here's test formula:
=ConvertTo2D(TRANSPOSE(R3:R16),7)
where R3:R16 is small range which was splited into table with 7 columns. The script is pretty short:
function ConvertTo2D(Arr, index) {
var newArr = [];
while(Arr[0].length) newArr.push(Arr[0].splice(0,index));
return newArr;
}
Sounds good. But! It is ve-e-e-e-ery slow. So This solution is good only for quick test.

If the data is structured and every object will always have the same structure you can use a simple offset to do this:
=OFFSET($B$2,
(ROW($B2) - 2) * 7 +
COLUMN(D$1) - 4,
0)
Put that in D2 and drag to the right and down.
It is possible to immediately return the data in this fashion but for that you need to meddle with the script.

Related

I need to clean up and split words from a mess of data into their own cells in a row. How can I accomplish this?

I am attempting to create documentation from an export of data that gives me a jumbled mess all in one cell that I need to clean up and extract certain bits from.
Here is an example:
[{"label":"Native Invoice","value":"native_invoice","displayOrder":0,"hidden":false,"readOnly":false},{"label":"Data Sync","value":"data_sync","displayOrder":1,"hidden":false,"readOnly":false}]
All of this is in one cell, and I need to have only the following information in their own individual rows:
Native Invoice
Data Sync
This example only has 2 values, but some that I am working on have hundreds, and it is taking far too long to manually copy and paste the values I need into their own cells.
Note: I am working in Google Sheets exclusively.
If I'm understanding you correctly, you want to pull anything after "label": without quotes. If that's the case, and if you are open to a formula instead of a script, supposing that your raw-data block were in A1, place this in B1:
=ArrayFormula(IFERROR(QUERY(FLATTEN(REGEXREPLACE(IF(NOT(REGEXMATCH(SPLIT(REGEXREPLACE(A1,"label.:.([^"&CHAR(34)&"]+)","~|$1~"),"~"),"\|")),,SPLIT(REGEXREPLACE(A1,"label.:.([^"&CHAR(34)&"]+)","~|$1~"),"~")),"\|","")),"WHERE Col1 Is Not Null")))
Here is how a custom function can look like:
function parse(txt) {
var jsn = JSON.parse(txt);
return [jsn[0].label, jsn[1].label];
}
Here is how it works:
You put the data into cell A1, put the formula =parse(A1) into the cell B1, and get the results in cells B1 and B2.
Update
If you want to get labels from all objects of the data, here is another variant of the function:
function get_labels(txt) {
return JSON.parse(txt).map(x => x.label); // get 'label' from all the objects
}
It works about the same way:

Sum by products horizontally into vertical data Google Sheets

I'm using Google Sheets to collect data of daily arrivals of products. My data looks as shown below:
What I'm trying to achieve is a sum by product:
I've tried something with a Script but with not much luck because I'm a little lost. Is there a way to do this through formulas? Or would it be easier using Scripts/Macros?
Thanks for any help you may provide!
This can be done with native google sheet formula's. Change ALL the A1:D3 to your range. Leave out the headers (row 1) and the ID (column A). So start at B2. I don't understand the ID column. Does not seem to do something?
The formula:
=QUERY({
FILTER(FLATTEN(A1:D3),ISTEXT(FLATTEN(A1:D3))),
FILTER(FLATTEN(A1:D3),ISNUMBER(FLATTEN(A1:D3)))},
"SELECT Col1, SUM(Col2) GROUP BY Col1 LABEL Col1 'Products', SUM(Col2) 'KG'",0)
So FLATTEN will create a list like this:
Snack
4
General
6
We filter out all the text and all the number, then this is the input data for the query. TIP: Copy the formula's out the main formula and see what it does.
If I'm understanding your post correctly, you have an ID column followed by 40 columns which alternate "product ... kg ... product ... kg ..." for a total of 41 columns running from A to AO.
I don't know the name of your source-data sheet. So I'll just call it "Source Sheet" in my formula. Change that throughout to your actual source-data sheet name.
You will want this formula in a separate sheet: not the source-data sheet:
=ArrayFormula({"ID","PROD","KG";QUERY(SPLIT(FLATTEN(FILTER('Source Sheet'!A2:A,'Source Sheet'!A2:A<>"")&"|"&FILTER('Source Sheet'!B2:AO,ISEVEN(COLUMN('Source Sheet'!B2:AO2)))&"|"&FILTER('Source Sheet'!B2:AO,ISODD(COLUMN('Source Sheet'!B2:AO2)))),"|",0,0),"Select * Where Col2 Is Not Null")})
Before I invest time explaining how it works, please test it out and report back whether this formula produces the desired result. If so, I'll explain the workings of it.

How to create INDIRECT array string of multiple sheet references in Google Sheets?

I am attempting to use a query to display data off multiple Google Sheets. I make a new sheet every week that has a specific sheet name, e.g. Week of 01/13, Week of 01/06 and so forth.
The following is where my idea spawned from for reference:
I have a summary sheet that is using COUNTA(INDIRECT("'" & A5 &
"'!E4:E",true)
A5 being a cell that concatenates a date and words to replicate the
sheet names.
The row on the summary sheet does not populate until B5<=today()
So I am able to set it an forget it and the sheet will continue to
give me my weekly data as the days progress and keeps the sheets clean
until the week is upon us.
Long story short, I have a query that I use that gives me all the data I need with a specific parameter but I have to manually update the data syntax array with the new sheet names each week.
=QUERY({'Week of 01/13'!A:P;'Week of 01/06'!A:P;'Week of 12/30'!A:P;'Week of 12/23'!A:P;'WEEK OF 12/16'!A:P;'WEEK OF 12/09'!A:P;'WEEK OF 12/02'!A:P;'WEEK OF 11/25'!A:P;'WEEK OF 11/18'!A:P;'WEEK OF 11/11'!A:P;'WEEK OF 11/04'!A:P;'WEEK OF 10/28'!A:P;'WEEK OF 10/21'!A:P;'WEEK OF 10/14'!A:P;'WEEK OF 10/07'!A:P;'WEEK OF 09/30'!A:P;'WEEK OF 09/23'!A:P;'WEEK OF 09/16'!A:P;'WEEK OF 09/09'!A:P;'WEEK OF 09/02'!A:P},
"Select * where Col11 = 'RD' order by Col2 desc",0)
I would like to build a reference to an array that will auto-populate a concatenation based on the day.
Using the following code I can have the concatenate give me the array I need,
=if(H4<=today(),CONCATENATE("'",H$1,text(H4,"mm/dd"),"'!A:P;",),"")
but when I try to input it into the query function it just returns the concatenated text:
=QUERY(I1,"Select *")
'Week of 01/06'!A:P;'Week of 01/13'!A:P
I have tried with and without the curly brackets with no success.
I would like the sheet to be able to refresh and see that it is the correct day, the new sheet name is populated and the query gets updated.
I need help with making I1 work.
Link to Test Query Sheet
dudes who copy-pasted INDIRECT function into Google Sheets completely failed to understand the potential of it and therefore they made zero effort to improve upon it and cover the obvious logic which is crucial in this age of arrays.
in other words, INDIRECT can't intake more than one array:
=INDIRECT("Sheet1!A:B"; "Sheet2!A:B")
nor convert an arrayed string into active reference, which means that any attempt of concatenation is also futile:
=INDIRECT(MasterSheet!A1:A10)
————————————————————————————————————————————————————————————————————————————————————
=INDIRECT("{Sheet1!A:B; Sheet2!A:B}")
————————————————————————————————————————————————————————————————————————————————————
={INDIRECT("Sheet1!A:B"; "Sheet2!A:B")}
————————————————————————————————————————————————————————————————————————————————————
=INDIRECT("{INDIRECT("Sheet1!A:B"); INDIRECT("Sheet2!A:B")}")
the only possible way is to use INDIRECT for each end every range like:
={INDIRECT("Sheet1!A:B"); INDIRECT("Sheet2!A:B")}
which means that the best you can do is to pre-program your array like this if only part of the sheets/tabs is existant (let's have a scenario where only 2 sheets are created from a total of 4):
=QUERY(
{IFERROR(INDIRECT("Sheet1!A1:B5"), {"",""});
IFERROR(INDIRECT("Sheet2!A1:B5"), {"",""});
IFERROR(INDIRECT("Sheet3!A1:B5"), {"",""});
IFERROR(INDIRECT("Sheet4!A1:B5"), {"",""})},
"where Col1 is not null", 0)
so, even if sheet names are predictable (which not always are) to pre-program 100+ sheets like this would be painful (even if there are various sneaky ways how to write such formula under 30 seconds)
an alternative would be to use a script to convert string and inject it as the formula
A1 would be formula that treates a string that looks like real formula:
=ARRAYFORMULA("=QUERY({"&TEXTJOIN("; ", 1,
IF(A3:A<>"", "'Week of "&LEFT(A3:A, 5)&"'!A1:D5", ))&
"}, ""where Col1 is not null"", 1)")
further populating of A6:A will expand the string automatically
then this script will take the string from A1 cell and it will paste it as valid formula into C5 cell:
function onEdit() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Master Sheet');
var src = sheet.getRange("A1");
var str = src.getValue();
var cell = sheet.getRange("C5");
cell.setFormula(str);
}
of course, the script can be changed to onOpen trigger or with custom name triggered from the custom menu or via button (however it's not possible to use the custom function as formula directly)
If you're trying to update the data your query is looking at and you're feeding it a string, you need to put that string within the indirect() function. That will interpret your string as a data reference and point your query() in the right direction.
So for this you'd probably have
=QUERY(INDIRECT(I1),"Select *")

Change order of columns spreahseet

I just want to confirm if there is any inbuilt function is google apps script to reorder the columns in the spreadsheet or do I have to do something like this -
var r = currentSheet.getRange(1, 1, currentSheet.getLastRow() - 1, currentSheet.getLastColumn()).getValues();
for(var i1=0, dLen=r[0].length; i1<dLen; i1++) {
//something to reorder
}
Please help!
it depends. if you just want to present the data in a different order, just use the built-in "=query" cell function, selecting the rows in the order you want.
query has some limitations thou. for example each column must have a consistent type on all rows (date, number, string etc). there arr other built-in functions that do similar things.
if you actually want to change the source range, you need to do it by getting the entire range array values, transforming it (possibly needing to take care of functions too) and writting the entire new array in a single range write.

Copy formula down using script

I've been working on a script (With some help from folk on SO). I have managed to create a script that imports a csv file and updates named ranges based on the imported data.
Here is an image of what I'm trying to do:
At this point I have imported data which has been populated into columns A:G. The cells in columns H:L are formula and are based on the data in A:G. Because I have imported data the data are now longer than the formula range. If not relying on script I would just double click the small blue square at the bottom right of the highlighted cells and the formulas would copy down. This is what I'd like to have the script do once I have imported the data.
I created a range called "formula_range" which automatically updates with the length of data. "formula_range" starts in cell H3 and ends in L:N where N is the length of rows in the sheet.
"formula_range" therefore contains some populated cells with formula and then blank rows all the way to the bottom of the sheet.
I saw this SO post. So in English, my line of thinking is:
Create a variable formula_range H3:L3 as a range
paste formula_range to every row in formula_range
Voila?
How do I get the variable formula_range if the first row in formula_range will always have the formula to be copied down. Put another way it should be fixed at H3:L3.
I tried this:
var copy_range = ss.getRangeByName("formula_range").getRange(1,5,1);
SpreadsheetApp.getUi().alert(copy_range);
My alert said "undefined" (I really wasn't sure what it would show.)
My line of thinking is not the most efficient since I am going to copy over existing formula. Perhaps there is a better way?
How do I get the first row in a named range formula_range[0]; ?
How can I use script to copy down formula in formula_range?
I think what you're trying to accomplish could be achieved more efficiently with a built-in arrayformula:
https://support.google.com/docs/answer/3093275
Place in H3:
=ARRAYFORMULA(DATE(A3:A,B3:B,1))
for each of your formulas in H through L in row 3 surround them with array formula and extend the arguments with open-ended ranges starting with row 3 as in the above example