Related
I've been trying to figure out what i'm doing wrong here when i'm doing the sumif formula's in b2,c2,d2
I have a lot going on, I realize. The data we are looking at, is between L5:U21
I have a query in a5 that pulls from l5:U that pairs any data in n5:n,p5:p,r5:r,t5:t to the selected data in the dropdown in a2. This part is working correctly for what I need.
B2 I am trying to extract from the top 3 options in the range b5:J that match a2, and add them together. Ultimately I'd like to do this if they do not have "Left" or "Right" in the J column as well.
To achieve this I pulled the data from b5:I into a sortn function seen in y5.
=SORTN(B5:I,3,,B5:B,false,D5:D,false,F5:F,false,H5:H,false)
and then my SUMIF function is as follows: =SUMIF(Z5:AF,A2,Y5:AE)
C2 is similar to B2, but I only data that matches the selection in a2, but also have "Left" in the J column.
I tried to achieve this with a similar SUMIF function i'm using in b2, but it seems to only pull the left most cell's data in the range given, not the matching column's data. So lets say if e9 = example1, it doesn't then grab the matching 2 in d9, it grabs whatever is in b9 only, and adds that. Which right now, it adds them all. I want to ultimately only pull the top 1, but I cannot even get it working correctly with all of them.
=SUMIF(J5:J,"Left",B5:H)
D2 is the same as C2, but "Right" in the J column.
This is my example / testing document I created to get a closer look at what's going on, if what i'm explaining isn't making a ton of sense.
https://docs.google.com/spreadsheets/d/1eZ7_yOrkoy_PCgcn_YxscPnDCvLXWK48JW-S7DqEgdQ/edit?usp=sharing
Try the following in C2
=QUERY({{B5:C;D5:E;F5:G},{J5:J;J5:J;J5:J}},
"select sum(Col1) where Col2='"&A2&"' and Col3='Left'
label sum(Col1) '' ",0)
For cell D2 all you need to do is use Col3='Right'
=QUERY({{B5:C;D5:E;F5:G},{J5:J;J5:J;J5:J}},
"select sum(Col1) where Col2='"&A2&"' and Col3='Right'
label sum(Col1) '' ",0)
In case you want to add more ranges like columns H-I you would adjust your formula like:
{{B5:C;D5:E;F5:G;H5:I},{J5:J;J5:J;J5:J;J5:J}}
(Do adjust the formula according to your ranges and locale)
SUGGESTION
I was experimenting earlier with SUMIFS & QUERY formulas to no success, as I also have a limited knowledge when it comes to implementing advanced Google Sheet formulas in a complex scenario. What I can suggest you try is by using a Custom Function formula in Google Sheet made possible by Google Apps Script that's integrated to the Google Sheet service.
This custom formula function will filter the range that does not contain Left or Right in column J based on the selected drop-down data, and then it returns the top 3 results in descending order.
The Custom formula Script named as CUSTOM_FUNC
/**
* Filters data in descending order from a range that doesn't contain any Left & Right based on selection in a cell dropdown selection & return the sum of the top 3 result.
*
* #param {B5:J21,A2} reference The range to be used.
* #returns The range and the cell reference to used in filtereing the data.
* #customfunction
*/
function CUSTOM_FUNC(data,dropdown_selection) {
/**Filter data that do not contain Left or Right on column J */
var lvl1 = data.map(x => {return x.toString().includes('Left') || x.toString().includes('Right') ? null : x }).filter(y => y)
/**Further filter lvl1 that matches the drop down selection*/
var res = lvl1.map(d => {
return d.map((find, index) => {return find == dropdown_selection ? d[index-1] : null}).filter(z => z)
});
/**Return the top 3 result in descending order */
return res.sort().reverse().slice(0, 3);
}
The parameters of this customer formula would be:
=CUSTOM_FUNC(data,dropdown_selection)
data
The sheet range (e.g. B5:J21) where the data you'd like to be processed resides.
dropdown_selection
The cell reference of the drop-down selection on your spreadsheet file.
To add this script in your spreadsheet file, copy and paste the script as a bound script in your Spreadsheet file by following this official guide
Demonstration
You can use the custom function formula named CUSTOM_FUNC similarly to how you use another Google Sheet formula on a cell, like this =SUM(CUSTOM_FUNC(B5:J21,A2))
I have 3 columns A, B & C as shown in the image. Column A contains the search key. The second column B contains names and their respective content in the third column C.
I am filtering rows that contain the text in A1 in B:C and concatenating them. The challenge is that each text in the third column is roughly 40k characters. The filter formula works well so the issue is the character limit. This formula =ArrayFormula(query(C1:C,,100000)) which I have in F1 concatenates more than 50000 characters but I am not how to apply it for my case.
Tried to wrap my formula in E1 inside the query function but it wasn't successful. Like so:
=ArrayFormula(query(CLEAN(CONCATENATE(FILTER(C1:C, B1:B=A1))),,100000)).
I also tried to SPLIT the concatenated result into sets of 50000 characters and put the extras in the next columns but wouldn't manage either. The formula I tried in this case is:
=SPLIT(REGEXREPLACE(CLEAN(CONCATENATE(FILTER(C1:C, B1:B=A1))),".{50000}", "$0,"),",")
The link to the spreadsheet
https://docs.google.com/spreadsheets/d/1rhVSQJBGaPQu6y2WbqkO2_UqzyfCc3_76t4AK3PdF7M/edit?usp=sharing
Since cell is limited to 50,000 characters, using CONCATENATE is not possible. Alternative solution is to use Google Apps Script's custom function. The good thing about Apps Script is it can handle millions of string characters.
To create custom function:
Create or open a spreadsheet in Google Sheets.
Select the menu item Tools > Script editor.
Delete any code in the script editor and copy and paste the code below.
At the top, click Save.
To use custom function:
Click the cell where you want to use the function.
Type an equals sign (=) followed by the function name and any input value — for example, =myFunction(A1) — and press Enter.
The cell will momentarily display Loading..., then return the result.
Code:
function myFunction(text) {
var arr = text.flat();
var newStr = arr.join(' ');
var slicedStr = stringChop(newStr, 50000);
return [slicedStr];
}
function stringChop(str, size){
if (str == null) return [];
str = String(str);
size = ~~size;
return size > 0 ? str.match(new RegExp('.{1,' + size + '}', 'g')) : [str];
}
Example:
Based on your sample spreadsheet, there are 4 rows that matches the criteria of the filter and each cell contains 38,976 characters, which is 155,904 characters in total. Dividing it by 50,000 is 3.12. The ceiling of 3.12 is 4 which means we have 4 columns of data.
Usage:
Paste this in cell E1:
=myFunction(FILTER(C1:C, B1:B=A1))
Output:
Reference:
Custom Function
I have a row which look like this:
|Data A-1|Data A-2|Data A-3|Data A-4|Data A-5|Data A-6|Data B-1|Data B-2|Data B-3|Data B-4|Data B-5|Data B-6|Data C-1|Data C-2|Data C-3|Data C-4|Data C-5|Data C-6|
There are 5 columns that are related to each Data. I need to convert all the data in a row into fix rows and columns which look like this:
|Data A-1|Data A-2|Data A-3|Data A-4|Data A-5|Data A-6|
|Data B-1|Data B-2|Data B-3|Data B-4|Data B-5|Data B-6|
|Data C-1|Data C-2|Data C-3|Data C-4|Data C-5|Data C-6|
How can I achieve this in Google Sheets?
Example Sheet
In your case, I thought that this thread might be able to be used.
Sample formula 1:
For the goal in your question, how about the following sample formula?
=ARRAYFORMULA(TRIM(SPLIT(TRANSPOSE(SPLIT(REGEXREPLACE(TEXTJOIN(",",TRUE,A1:1),"(([\w\s\S]+?,){6})","$1#"),"#")),",")))
In this case, one row is used. So A1:1 is used as the range. But when you have several rows, please modify the range. And, the row is splitted by 6 column. So (([\w\s\S]+?,){6}) is used as the regex.
The flow of this formula is as follows.
Join all cell values by ignoring the empty cells using TEXTJOIN.
Put # to the joined text value for 6 columns using REGEXREPLACE.
Split the text value with # using SPLIT.
Transpose the splitted values using TRANSPOSE.
Split the each row with , using SPLIT.
Result:
Sample formula 2:
For your shared Spreadsheet, how about the following sample formula?
=ARRAYFORMULA(TRIM(SPLIT(TRANSPOSE(SPLIT(REGEXREPLACE(TEXTJOIN(",",TRUE,A8:8),"(([\w\s\S]+?,){8})","$1#"),"#")),",")))
In this case, one row is used. So A8:8 is used as the range. But when you have several rows, please modify the range. And, the row is splitted by 8 column. So (([\w\s\S]+?,){8}) is used as the regex.
Result:
Note:
In this case, because of REGEXREPLACE(TEXTJOIN(",",TRUE,A8:8),"(([\w\s\S]+?,){8})","$1#"), when the characters are over 50,000, an error occurs. In that case, I would like to propose to use the Google Apps Script as the custom function. The sample script is as follows. Please copy and paste the following script to the script editor of Spreadsheet, and put =SAMPLE(A8:8, 8) to a cell when your shared Spreadsheet is used. In this case, the arguments of A8:8 and 8 are the range and the splitted number, respectively. By this, your goal can be achieved.
const SAMPLE = (values, split) => values.flatMap(r => {
const temp = [];
while (r.length > 0) temp.push(r.splice(0, split));
return temp
});
References:
TEXTJOIN
REGEXREPLACE
SPLIT
TRANSPOSE
Custom Functions in Google Sheets
Added:
About your additional question by your comment as follows,
Is there any way to make the first column to be sorted?
how about the following sample formula?
Sample formula:
=SORT(ARRAYFORMULA(TRIM(SPLIT(TRANSPOSE(SPLIT(REGEXREPLACE(TEXTJOIN(",",TRUE,A8:8),"(([\w\s\S]+?,){8})","$1#"),"#")),","))),1,TRUE)
In this case, the rows are sorted by the 1st column as the ascending order.
When you use the custom formula created by Google Apps Script, you can also use SORT as follows.
=SORT(SAMPLE(A8:8, 8),1,TRUE)
Or, you can also use the following script. When you use this, please put =SAMPLE2(A8:8, 8) to a cell.
const SAMPLE2 = (values, split) => values.flatMap(r => {
const temp = [];
while (r.length > 0) temp.push(r.splice(0, split));
return temp.sort((a, b) => a[0] - b[0]);
});
or try:
=QUERY(
{FLATTEN(FILTER(A8:15, MOD(COLUMN(A1:1)-1, 8)=0)),
FLATTEN(FILTER(A8:15, MOD(COLUMN(A1:1)-2, 8)=0)),
FLATTEN(FILTER(A8:15, MOD(COLUMN(A1:1)-3, 8)=0)),
FLATTEN(FILTER(A8:15, MOD(COLUMN(A1:1)-4, 8)=0)),
FLATTEN(FILTER(A8:15, MOD(COLUMN(A1:1)-5, 8)=0)),
FLATTEN(FILTER(A8:15, MOD(COLUMN(A1:1)-6, 8)=0)),
FLATTEN(FILTER(A8:15, MOD(COLUMN(A1:1)-7, 8)=0)),
FLATTEN(FILTER(A8:15, MOD(COLUMN(A1:1)-8, 8)=0))},
"where Col1 is not null", 0)
Your assistance will be greatly appreciated as I have been struggling with this for a while and couldn't find a solution.
I have a Google Sheets file with comma-separated data in two columns as per the screenshot attached.
Screenshot of the two columns
text from the screenshot:
soon,son,so,on,no N/A
kind,kid,din,ink,kin,in dink
sing,sign,sin,gin,in,is gis,ins,sig,gins
farm,arm,ram,far,mar,am arf
may,yam,am,my N/A
tulip,lip,lit,pit,put,tip piu,pul,til,tui,tup,litu,ptui,puli,uplit
gift,it,if,fit,fig gif,git
hear,are,ear,hare,era,her hae,rah,rhea
dish,his,is,hi,hid dis,ids,sidh
trip,pit,rip,tip,it N/A
wife,few,if,we fie
thaw,what,hat,at haw,taw,twa,wat,wha
red,deer,reed ere,dee,ree,dere,dree,rede
as,save,vase,sea ave,sae,sev,vas,aves
from,for,form,of,or fro,mor,rom
won,now,on,own,no N/A
sport,port,spot,post,stop,sort,top,opt,pot,pro tor,sotrot,ops,tors,tops,trop,pots,opts,rots,pros,prost,strop,ports
I would love to have in another column a formula to show if in these two columns there are any duplicate values.
Thank you in advance for your help... it's been weeks without success haha
If you have Excel for Windows O365 with the UNIQUE and FILTERXML functions,
and if you mean to consider both columns together as if they were a single piece of data,
then try:
=UNIQUE(FILTERXML("<t><s>" & SUBSTITUTE(TEXTJOIN("</s><s>",TRUE,$A$1:$A$17,$B$1:$B$17),",","</s><s>") & "</s></t>","//s[.=following-sibling::*]"))
If that is not what you want, please clarify your question.
First place your data in columns A and B of an Excel worksheet. Then run this short VBA macro:
Sub report()
Dim rng As Range, r As Range, c As Collection, K As Long
Set rng = Range("A1:B17")
Set c = New Collection
K = 1
For Each r In rng
arr = Split(r.Value, ",")
For Each a In arr
On Error Resume Next
c.Add a, CStr(a)
If Err.Number <> 0 Then
Err.Number = 0
Cells(K, "C").Value = a
K = K + 1
End If
On Error GoTo 0
Next a
Next r
Range("C:C").RemoveDuplicates Columns:=1, Header:=xlNo
Set c = Nothing
End Sub
The duplicates appear in column C
What I have understood from your question: you want to find out if there are any words delimited by commas matching between the cells of two different columns.
For this solution I have used Apps Script. The following commented piece of code will find matching words between the two columns. Moreover, as the function used is an onEdit() trigger, it will automatically detect any changes done in either of these columns and automatically find out new matches or matches that are no longer there and update the value of cell C1:
function onEdit() {
// get current sheet
var sheet = SpreadsheetApp.getActive().getActiveSheet();
// get values from our columns. This returns a 2D array that is flatten into a
// 1 D array to then convert it into a string where its elements are separated
// by a comma and white spaces are removed (so that a matches space + a for example)
var colA = sheet.getRange('A1:A2').getValues().flat().join().replace(/\s/g, '');
var colB = sheet.getRange('B1:B2').getValues().flat().join().replace(/\s/g, '');
// Create two arrays where each element is a word delimited by a comma in their original
// string
var ArrayA = colA.split(',');
var ArrayB = colB.split(',');
// find matches in these two arrays and return these matches
var matchingValues = ArrayA.filter(value => ArrayB.includes(value));
// set the value of C1 to the words that the filter has matched between our two columns
// join is used to display all the matching elements of the match array
sheet.getRange('C1').setValue(matchingValues.join());
}
Demo:
If you do not know how to open the script editor, you can access it on your Google Sheets menu bar under Tools-> Script editor.
What I am trying to accomplish is I would like to search for a term in one cell, if that cell has the term write text to another cell. My specific example would be I would like to search for the term 'DSF' in column 4. If I find 'DSF' it would then write 'w' in column 5 & write '1.2' in column 3. This is searched per row.
I do understand the the .setvalue will write the needed text, but I do not understand how to create a search function. Some help would be greatly appreciated.
EDIT
Here is the code I am working with at the moment. I am modifying it from something I found.
function Recalls()
{
var sh = SpreadsheetApp.getActiveSheet();
var data = sh.getDataRange().getValues(); // read all data in the sheet
for(n=0;n<data.length;++n){ // iterate row by row and examine data in column D
if(data[n][3].toString().match('dsf')=='dsf'){ data[n][4] = 'w'}{ data[n][2] = '1.2'};// if column D contains 'dsf' then set value in index [4](E)[2](C)
}
//Logger.log(data)
//sh.getRange(1,1,data.length,data[3].length).setValues(data); // write back to the sheet
}
With the Logger.log(data) not using the // It works properly but it overwrites the sheet, which will not work since I have formulas placed in a lot of the cells. Also, Maybe I did not realize this but Is there a way to do a live update, as in once I enter text into a cell it will research the sheet? Otherwise having to 'run' the macro with not save me much time in the long run.
Try this. It runs when the sheet is edited. It only captures columns C,D,&E into the array and only writes back those columns. That should solve overwriting your formulas. It looks for 'DSF' or 'dsf' in column D (or contains dsf with other text in the same cell either case). Give it a try and let me know if I didn't understand your issue.
function onEdit(){
var sh = SpreadsheetApp.getActiveSheet();
var lr = sh.getLastRow()// get the last row number with data
var data = sh.getRange(2,3,lr,3).getValues(); // get only columns C.D,& E. Starting at row 2 thur the last row
//var data = sh.getDataRange().getValues();// read all data in the sheet
for(n=0;n<data.length-1;++n){ // iterate row by row and examine data in column D
// if(data[n][0].toString().match('dsf')=='dsf'){
if(data[n][1].match(/dfs/i)){ //changed to find either upper or lower case dfs or with other text in string.
data[n][2] = 'w';
data[n][0] = '1.2'};
}
sh.getRange(2,3,data.length,data[3].length).setValues(data); // write back to the sheet only Col C,D,& E
}