Parse Google sheets cell input to place in adjacent cell - scripting

How can I set things up in google sheets such that it will take my input in, say cell B2, clean it up and put things in cells C2 and D2?
For example, say I put in cell B2 the string "ts 4 x 4 x 3/8 x 13'-5". I can of course write a script called std_shape that can parse this into the ouput array ["HSS4X4X3/8", "13'-5""] and when I put =std_shape(B2) into the cell C2, I'll get what I want: C2 will contain HSS4X4X3/8 and cell D2 will contain "13'-5"". But how to do this such that column C & D will automatically fill in from any row in B? Using an arrayformula() tends to fill downward on column C.
What would be the most efficient way of going about this? I want to be able to make a list of material in column B and then have columns C & D format my list in the way described. For instance columns C & D would be derived from my B column inputs as below (where I've separated the columns with the "|" symbol):
> B | C | D
> ------------------------+---------------+----------
> TS 4x4x1/4 x 13'-4 | HSS4X4X1/4 | 13'-4"
> L 5 x 3-1/2 x 5/16 x 6' | L5X3-1/2X5/16 | 6'-0"
> PL 1/2 x 8 x 10 | PL1/2X8 | 0'-10"
> W10x12 x 13' | W10X12 | 13'-0"
Columns C & D have standardized what was put into B. Further cells in the spreadsheet will work with these standard texts for vlookups and other calculations. I want to keep B as input though. I also wish to validate the input in B as well. For instance, say the W10x12 above cannot be found in a vlookup, then column C for that row would say something like "W10x12 is not a valid entry".
What I'm saying is that I don't know how to handle ranges in my script. I also would like to know the most efficient way of going about this. After all, for any given row, columns C & D only care about that particular row's B value - I don't want my function tossing arrays around willy nilly. Maybe some sort of "onEdit" type deal that creates columns C & D whenever the B entry is edited for any particular row?
I'm having brain stalls with this since user-defined functions can't touch any other cells except in overflow. There's GOT to be some way to do such a thing as I'm describing here?

Related

Google Sheets SQL Query - Cell reference returns record

I am trying to run a relatively (for me!) complex query in google sheets, it has a few different parts to it but doesn't work. After much playing around I've found it's down to one small part of it that doesn't return the expected results.
If I run this...
=query('Link: AdNo | ClassPeriodID'!A2:D59001,"SELECT D WHERE C = 'AMon:1'",1)
it returns the exact response I want - a list of all classes (content of column D) that match AMon:1 (in column C).
However, if I make a cell B1 =AMon:1 and run this...
=query('Link: AdNo | ClassPeriodID'!A2:D59001,"SELECT D WHERE C = '"&B1&"'",1)
it only returns the first entry in that list. Thanks in advance for any help!
make sure B1 is the right cell
=QUERY('Link: AdNo | ClassPeriodID'!A2:D, "select D where C = '"&B1&"'", 1)

Order 2 columns by matching to another column

I have 4 columns in an excel file:
The two on the right have about 9000 rows, the 2 on the left have about 14000.
Column A and B are connected. Also, C & D are connected.
The THER_CLASS column is the 1st 6 numbers of the key column.
How would I order the left two columns by if the 1st 6 of the key matches THER_CLASS?
I want each row to take the A column find which row the 1st 6 # of GPI are equal and then move C and D to the row that A is on.
I could do this by building a console app in c# but I feel like there is a way to to it much easier in excel.
You're basically just looking up the "Category" of each "Item". As Flephal points out, you can do this with VLookup.
Add a column E with the header "CLASS" and a column F with the header "CLASS_DESC".
For column E, starting with cell E2 and filling downwards, use the formula "=VLOOKUP(Left(C2, 6), A:B, 1, False)"
For column F, starting with cell F2 and filling downwards, use the formula "=VLOOKUP(Left(C2, 6), A:B, 2, False)"
Really, it would make much more sense if you didn't have the two tables mashed up next to one another and instead had each one on a different sheet. For example, move A & B to another sheet and call it "CLASSES".

VBA Excel 2007 - Merge values in a string

I have a string who looks like this:
a -> 80
b -> 40
c -> 20
a -> 10
What I'd like to know is if it's possible to have this output:
a -> 80 + 10 = 90
b -> 40
c -> 10
The code would find the values that are repeated and then "merge" the numbers who are next to it. It's a string, so I assume it's not possible to do the calculation, but if there's a way to do the a -> 80 + 10 it would be also fine (and save me a lot of time).
Thanks in advance.
ummm... not quite sure what you mean, but if the letters represent columns, what you could do is scroll to the furthers down cell, so for example is you have 10 cells with numbers in column a and 15 in column b and 17 in column c you'd scroll down one cell past 17, and put this formula: =SUM(C1:C17) in the formula bar, (its c1 to c17 because in my example to you there were 16 used cells in colujmn c, and that was the most used cells in one column) and then u drage that left and right so basically it will sum each column..

Google spreadsheet "=QUERY" join() equivalent function?

This question is concerning joining two databases in Google spreadsheet using =QUERY function
I have a table like so in range A1:C3
a d g
b e h
c f i
I have another table
c j m
a k n
b l o
I want the final table to look like this
a d g k n
b e h l o
c f i j m
I can do this by using a vlookup function pretty easily in cell D1 and paste it down and across, but my dataset is huge. I would need a whole page of vlookups and Google Spreadsheet tells I'm at my limit in complexities.
I look at the Google's Query Language reference... there doesn't seem to be an type of "join" functions mentioned. You would think it would be an easy "join on A" type operation.
Can anybody solves this without a vlookup?
Short answer
Google QUERY Language version 0.7 (2016) doesn't include a JOIN (LEFT JOIN) operator but this could be achieved by using an array formula which result could be used as input for the QUERY function or for other uses.
Explanation
Array formulas and the array handling features of Google Sheets make possible to make a JOIN between two simple tables. In order to make easier to read, the proposed formula use named ranges instead of range references.
Named Ranges
table1 : Sheet1!A1:C3
table2 : Sheet2!A1:C3
ID : Sheet1!A1:A3
Formula
=ArrayFormula(
{
table1,
vlookup(ID,table2,COLUMN(Indirect("R1C2:R1C"&COLUMNS(table2),0)),0)
}
)
Remarks:
Using open ended ranges is possible but this could make the spreadsheet slower.
To speed up the recalculation time :
Replace Indirect("R1C2:R1C"&COLUMNS(table2),0) by an array of constants from 2 to number of columns of table2.
Remove the empty rows from the spreadsheet
Example
See this sheet for an example
Note
On 2017 Google improved the official help article in English about QUERY, QUERY function. It still doesn't include yet topics like this but could be helpful to understand how it works.
So, this answers how you do it WITH a Vlookup-function, but in only one cell.
In your example, given that each table of data has the following cell references:
Table1: Sheet1!A1:C3
a d g
b e h
c f i
Table2: Sheet2!A1:C3
c j m
a k n
b l o
This is how the formula should be constructed.
Join-formula
=ArrayFormula(
{
Sheet1!A1:C,
vlookup(Sheet1!A1:A, {Sheet2!A1:A, Sheet2!B1:C}, {2,3}, false)
}
)
The key to get this formula to work, is to understand how to use curly brackets in the Vlookup Range. You basically define the first cell reference of the Range as the column which is to be a match to the Vlookup Search_Key. The rest of the cell references in the Range is in relation to the columns which you would like to join.
The Index is written as {2,3} to return the second and third column of the Range (the Range consists of a total of 3 columns); curly brackets has nothing to do with Arrayformula in the Vlookup Index, but is necessary to return multiple columns from the Vlookup function. The reason to not write {1,2,3} is because you would not like to include the column which is being used for the purpose of joining.
Example where the column in table2 used for joining, is located in a different column (to the right of the data which is to be joined)
This kind of Join-formula can be utilized even if the join-column in the second table is located as the third column of that table.
Let's say that the raw-data in this example would look like this:
Table1 (Sheet1):
a d g
b e h
c f i
Table2 (Sheet2):
j m c
k n a
l o b
If you write the formula like this, you'll still get the desired outcome (as displayed in the table of joined data):
=ArrayFormula(
{
Sheet1!A1:C,
vlookup(Sheet1!A1:A, {Sheet2!C1:C, Sheet2!A1:B}, {2,3}, false)
}
)
The table of joined data:
a d g k n
b e h l o
c f i j m
In the Join-formula, notice that the third column of Table2 is located as the first cell reference in the Vlookup Range!
The reason to why this works, is because when you use curly brackets in the Range (in conjunction with Arrayformula), the Vlookup Search_Key will NOT look for a column as a common denominator within the raw-data, instead it will use the Array within curly brackets as a reference to find a column as a common denominator (by default this is the first column of the Range).
I've written a comprehensive guide about this topic called:
'Mastering Join-formulas in Google Sheets'
You can use ARRAYFORMULA or YOU can just drag this formula:
after an import or QUERY-ing the first table; in the D column:
=QUERY(Sheet2!A1:C3, "Select B,C WHERE A='" & A1 & "'", 0)
I solved this by using Javascript LINQ (language integrated query).
It lets you specify Javascript with complex join conditions. You can also perform other SQL queries such as Grouping, Projecting, Sorting and Filtering your sheets as if they were database tables. Look at the links below.
Note that in the LINQ query language I replaced all spaces in column names with underscores to make them valid JS identifiers.
https://docs.google.com/spreadsheets/d/1DHtQlQUlo-X_YVfo-Wo-b7315sSk2pxL5ci4Y9lxvZo/edit?usp=sharing
https://script.google.com/d/1R5L2ReHJrBRwyoSoVOFLzEQZiGtxidPfPkAeVownt7SWX6TpacY7gA7j/edit?usp=sharing
If you can map each "index" (a, b, c) to a specific row or column, then you could use the INDEX function.
In this case, you could probably map 'a' to column A (or row 1), 'b' to column B (or row 2), and so on.
Also, Merge Tables seem to address this exact use case.
With the 'other' table in A5:C7, please try:
=query({A1:C3,query(sort(A5:C7,1,TRUE),"Select Col2,Col3")})

How to sum data in one column based on the same month in another column using macros

I am using macros in excel 2007 for my work. I am working with many data and I need to sum data from 2 or more rows in the same coloumn according to the same month. However the month column is expressed as date.
for example, i have series of data
A B
2/10/2008 2
2/10/2008 3
4/10/2008 3
5/11/2008 4
5/11/2008 5
I want the result to be displayed in column C and D as followed
C D
Oct/08 8
Nov/08 9
I am very thankful if anyone can help me.
regards,
Tifu
A B C D E F
1 10/ 1/2008 24106 1 Oct-08 24106 8
2 10/31/2008 24106 7 Nov-08 24107 11
3 11/ 1/2008 24107 8 Dec-08 24108 6
4 11/30/2008 24107 3
5 12/ 1/2008 24108 2
6 12/ 2/2008 24108 4
B1 =MONTH(A1)+YEAR(A1)*12
E1 =MONTH(D1)+YEAR(D1)*12
F1 =SUMIF(B$1:B$6,CONCATENATE("=",E1),C$1:C$6)
I had to overcome two problems to solve this. First, SUMIF can only do direct comparison, it cannot run a function on the source location (except for range functions, which the date and time functions are not), so I had to add the B column. The E column is optional, it could be implemented as part of the formula in F, but I left it independent for illustrative purposes. Second, SUMIF takes a string parameter describing the comparison, so I built the necessary string (it is "=24106" for F1) on the fly.
using array functions:
C1:
=date(2008,small(month($A$1:$A$10),1),1)
C2:
=date(2008,small(month($A$1:$A$10),2),1)
right click on these cells and format them as mmm/yy
D1:
=sum(if(month($C1)=month($A$1:$A$10),($B$1:$B$10),0))
make sure to press ctrl-shift-enter when done writing this formula.
then copy an paste it down as needed.
If you are unfamiliar with VBA, I would start off by recording a macro while doing what you want to do by using the Subtotals feature under the Data menu (i.e. through Excel's interface).
Once the macro is recorded, you can look at the VBA code produced, and alter it to suit your needs.
This should be possible to do using regular excel formulas:
extract the month of the date (some Month-function)
then do a conditional sum (function sumwhen (?)) like this:
= SumWhen(A:B, ReferenceDate, B:B)
(with ReferenceDate = C2 C3 etc.)
(All I have is a German excel and the function names got translated as well, so my function names may be off. Maybe someone can check against an English excel and update if necessary.)