Aggregating using regex in Google Sheets - regex

I have the following cell in my spreadsheet. It is a string that has the structure of a JSON object.
D2 = {color: white, quantity: 23, size: small}, {color: black, size: medium, Quantity: 40}
For the cell D3, I want the output to be 63, which is the total of the quantities found in D2. Moreover, the following things should be noted:
If something goes wrong, for example, if a quantity is given as a string, then D3 is simply kept empty.
Anytime D2 is updated, D3 is updated as well.
If D3 is updated manually, that result is kept instead of the formula. This will happen for rows where D2 is empty.
Is there any way to do this using Google Sheets? I tried using REGEXEXTRACT, however, it did not produce any result. Any help is greatly appreciated.
Edits:
The order of the attributes is not important. For example, the quantity can appear before or after the size.
The case is also not important.

=SUMPRODUCT(IFERROR(REGEXREPLACE(SPLIT(D22, ":"), "}.*", "")))
=SUMPRODUCT(IFERROR(REGEXREPLACE(SPLIT(SUBSTITUTE(D21, "quantity:", "♦"), "♦"), "}.*", "")))

Related

How can I calculate a new date conditionally based on other information?

I have a Google Sheet which is being populated by a Google Form. I am using Google Apps Script to add some extra functionality. Please feel free to access and modify these as needed in order to help.
Based on answers from the Form, I need to return a new date that factors in the time stamp at form submission.
This is a dumbed down example of what I need to do, but let's think of it like ordering a new car and its color determines how long it is going to take.
Car
Color
Toyota
Red
Honda
Blue
Tesla
Green
I need to write a conditional IF statement that determines how many weeks it will take to get the car based on the ordered color.
-
Red
Blue
Green
Toyota
1
3
5
Honda
2
4
6
Tesla
1
1
1
So if you order a Toyota in Red, it will take one week. If you order a Toyota in Green, it will take 5 weeks. If you order a Tesla, it will be really in one week no matter what color. Etc...
I started by writing some language in Sheets to take the Timestamp which is in Column A and add the appropriate amount of time to that:
=IFS(AND(B2 = "Toyota",C2 = "Red"),A2 + 7,AND(B2="Toyota",C2="Blue"), A2 + 21,AND(B2="Toyota",C2="Green"), A2 + 35,AND(B2 = "Honda",C2 = "Red"),A2 + 14,AND(B2="Honda",C2="Blue"), A2 + 28,AND(B2="Honda",C2="Green"), A2 + 42,AND(B2 = "Tesla"),A2 + 7)
And then I dragged that down the length of the entire column so that it would fill in as submissions came in.
However when you fill in the Google Form, it will overwrite what's in that entire row, blowing out what I had in that column.
Now I realized that the code needs to be written in Google Apps Script and returned as a value.
What kinds of modifications need to be made to my IFS statement in order to make it compatible with Google Apps Script?
For easier approach, QUERY would actually solve your issue without doing script as Broly mentioned in the comment. An approach you can try is to create a new sheet. Then have that sheet contain this formula on A1
Formula (A1):
=query('Form Responses 1'!A:C)
This will copy A:C range from the form responses, and then, copy/paste your formula for column Date Needed on column D.
Output:
Note:
Since you only copied A:C, it won't affect column D formula.
Your A:C in new sheet will update automatically, then the formula you inserted on D will recalculate once they are populated.
Add IFNA on your formula for column D to not show #N/A if A:C is still blank.
Formula (D2):
=IFNA(IFS(AND(B2 = "Toyota",C2 = "Red"),A2 + 7,AND(B2="Toyota",C2="Blue"), A2 + 21,AND(B2="Toyota",C2="Green"), A2 + 35,AND(B2 = "Honda",C2 = "Red"),A2 + 14,AND(B2="Honda",C2="Blue"), A2 + 28,AND(B2="Honda",C2="Green"), A2 + 42,AND(B2 = "Tesla"),A2 + 7), "")

Cell formating based on other cell value google sheets

I have 2 sheets the first one is Orders and second one is the ShippingDoc.
At Shipping doc, I have Cell C2. In that cell I choose/write ID from range of A7:A from Orders.
Is there a way if I choose example ID 1 at C2 then automatically A7 at Orders to change background to green. Also if I change the ID to 2 the A8 to be green and to not delete the A7 color.
There is a better way of achieving this using Apps Script. Go to tools->Script editor and there use the following code (explained on the comments in the code):
// Update everytime the value on C2 changes
function onEdit() {
var spreadsheet = SpreadsheetApp.getActive();
// Get C2 value
var C2value = spreadsheet.getSheetByName("ShippingDoc").getRange('C2').getValue();
// If input value is higher than 0
if(C2value>0){
// Ofset to set the background after A7
var number = C2value+6;
// Set background color of appropiate cell
spreadsheet.getSheetByName("Orders").getRange('A'+number+'').setBackground('green');
}
}
Check out more information about how to do amazing things in sheets with simple scripts HERE
I hope this has helped you. Let me know if you need anything or if you did not understand something.
try:
=(REGEXMATCH(""&INDIRECT("ShippingDoc!C2"), ""&A1))*(A1<>"")

Reversing a column when calculating slope for a line chart in Google Sheets

I have the following sheets:
https://docs.google.com/spreadsheets/d/1aC9lsmxVw0pYN_Wjk7gooB0c7CsvmkRsEeCUBEKUIlM/edit?usp=sharing
It should be pretty obvious looking at it. There is a spark-line which becomes green if the the trend is positive. From the data, it makes intuitive sense that the line should be trending up. However, due to the way I wrote the formula, the line is instead trending down and red. How can I reverse the columns being used in the formula?
Note: The data on the right hand side should remain in the same order.
Thanks for any help.
try:
=IFERROR(ARRAYFORMULA(SPARKLINE(
QUERY({B2:B, ROW(B2:B)}, "select Col1 order by Col2 desc"),
{"charttype", "line"; "color", IF(SLOPE(
QUERY({B2:B, ROW(B2:B)}, "select Col1 order by Col2 desc"),
ROW(A2:A)-1)>=0, "lime", "red"); "linewidth", 2})))

What formula or method could I go to subtract amounts from values in a range using a table

I have a master sheet with values of what I would sell for. I want to create a formula or rules where I can subtract commission based on the value of the cell. I want to be able to edit from the table only so I don't have to mess around with hundreds of cells formulas when things change. I also don't want to just take commission by percentage. I know how to link the cells. I want a formula that will look in the table and say hey its between the two values so ill extract this amount of commission. I have attached a picture of an example of the rules table.
I've tried doing IF statements and ran into too many arguments issues.
I expect the formula to look in my table and take out the proper commission beside it.
=ARRAYFORMULA(Main!B2-VLOOKUP(Main!B2,
{REGEXEXTRACT(Comission!$A$3:$A$13, "\d+")*1, Comission!$B$3:$B$13}, 2))
you can do various things like:
=ARRAYFORMULA(IF(A9:A<>"", IF(COUNTIF(A9:A, A9:A)>1,
B9:B-(B9:B*IFERROR(VLOOKUP(B9:B,
{{REGEXEXTRACT(A3, "\d+")*1, -B3% };
{REGEXEXTRACT(A4, "\d+")*1, -B4%};
{REGEXEXTRACT(A5, "\d+")*1, -B5%};
{REGEXEXTRACT(A6, "\d+")*1, -B6%};
{400, 0}}, 2))),
B9:B-(B9:B*IFERROR(VLOOKUP(B9:B,
{{REGEXEXTRACT(C3, "\d+")*1, -D3% };
{REGEXEXTRACT(C4, "\d+")*1, -D4%};
{REGEXEXTRACT(C5, "\d+")*1, -D5%};
{REGEXEXTRACT(C6, "\d+")*1, -D6%};
{400, 0}}, 2)))), ))
assuming Ema is a reseller and Jane & Yuki are one-timers
alternatives: https://webapps.stackexchange.com/q/123729/186471
=ARRAYFORMULA(IF(A2:A<>"", IFERROR(VLOOKUP(A2:A, Main!A2:B, 2, 0))-
IFERROR(VLOOKUP(IFERROR(VLOOKUP(A2:A, Main!A2:B, 2, 0)),
{IFERROR(REGEXEXTRACT(Comission!A3:A, "\d+")*1), Comission!B3:B}, 2)), ))

Correcting an aggregation formula using regex in Google Sheets so that all instances are considered

I have 2 cells in my spreadsheet. It looks like this:
D2 = {color: white, quantity: 23}, {color: black, quantity: 73}, {color: red, quantity: 10}
D3 = 106
D3 is being generated using the following formula:
=SUMPRODUCT(IFERROR(REGEXREPLACE(SPLIT(SUBSTITUTE(D2, "quantity:", "♦"), "♦"), "}.*", "")))
In other words, all I want to do is aggregate the quantities.
It works, however, the quantity attribute must come at the very end of each "JSON" object. If I change D2 to this:
D2 = {color: white, quantity: 23, size: small}, {color: black, quantity: 73}, {color: red, quantity: 10}
Because I added the size attribute after the quantity, D3 now becomes 83. Therefore, I would like to do three things:
Aggregate all instances of quantity wherever they occur.
Make the matching case insensitive. So things like "QUANTITY" would also work.
Spaces after "quantity:" should not be considered. So, something like {quantity:100}, {quantity: 23}, {quantity: 120} would also work.
How can I fix this? I tried removing the "}.*" of the regex, but that seemed to break the formula. Any help is appreciated.
=SUMPRODUCT(IFERROR(REGEXREPLACE(SPLIT(
SUBSTITUTE(LOWER(D28), "quantity:", "♦"), "♦"), ",.*|}.*", "")))