Issue with multiple conditions in script editor - if-statement

I've created a script editor for a google sheet that has multiple tabs. One if statement I can't seem to get working is - If sheet "Employee Evolution" column 8 EQUALS "Disqualified" AND column 13 is NOT EQUAL to "NO DATA", move the row to sheet "Disqualified" I've tried so many different ways to rearrange and can't get it to work.
**function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Employee Evolution" && r.getColumn() == 8 && r.getValue() == "Disqualified" && r.offset.getValue(0,5) != "NO DATA") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Disqualified");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1,numColumns).moveTo(target);
s.deleteRow(row);**
I have no coding experience, so I'm having a difficult time understanding javascript documents that explain this stuff. Please help!
Below is the link to my spreadsheet
https://docs.google.com/spreadsheets/d/1vp46hMbmB5968cRW2BGhS66qqNhl91Llk8xeknlRuQc/edit#gid=0
Right now I only have the first if statement and else if statement set up with multiple conditions, but that is not working. When I populate the 8th column (H) with Disqualified and populate the 13th column (M) with anything at all, nothing happens. And, if I populate column H with Qualified and populate column M with Paid Search, nothing happens.
Basically I want the row to move to either the PPC tab or the Disqualified tab. However, I don't want the row to move until both columns H and M are populated with specific text. If column H says "Qualified" AND column M says "Paid Search" the row should move to the PPC tab. If column H says "Disqualified" AND column M says anything other than NO DATA (even Paid Search), the row should move to the Disqualified tab.
The problem I can't get past is that I need to have each if statement look at both columns before executing true.
I hope this makes sense and thank you for your help.

It's not possible to define if that if should be working or not based just on the code as we don't have that spreadsheet you're using.
But I do notice some things that maybe are wrong on yout assumptiong, for this break that if into :
s.getName() == "Employee Evolution" => Checks if the current sheet that you have active is called "Employee Evolution"
r.getColumn() == 8 => checks if the current column that you have active is column number 8 (column h)
r.getValue() == "Disqualified" => checks if the current cell that you have active is equal to Disqualified (must matcha case as well)
r.offset.getValue(0,5) != "NO DATA") => Checks if column offset by 5 (will be equal column 13 indeed) is different then "NO DATA" (also must match case)
And of course as this function is onEdit, so this code will only run when you change something on the spreadsheet.
So I suppose by reading that code is that whenever someone changes column 8 to "Disqualified" (must match the capital letters as well) and all the other criterias match, it should be moved to "Disqualified" sheet. Pay attention to all the case sensitive scenarios you have.
I think overall the code seems fine. Share the spreadsheet so we can check what might be going wrong.
PS: by something being active I mean that your cursor is clicked/selected that thing

Related

Google Sheets: How can I extract partial text from a string based on a column of different options?

Goal: I have a bunch of keywords I'd like to categorise automatically based on topic parameters I set. Categories that match must be in the same column so the keyword data can be filtered.
e.g. If I have "Puppies" as a first topic, it shouldn't appear as a secondary or third topic otherwise the data cannot be filtered as needed.
Example Data: https://docs.google.com/spreadsheets/d/1TWYepApOtWDlwoTP8zkaflD7AoxD_LZ4PxssSpFlrWQ/edit?usp=sharing
Video: https://drive.google.com/file/d/11T5hhyestKRY4GpuwC7RF6tx-xQudNok/view?usp=sharing
Parameters Tab: I will add words in columns D-F that change based on the keyword data set and there will often be hundreds, if not thousands, of options for larger data sets.
Categories Tab: I'd like to have a formula or script that goes down the columns D-F in Parameters and fills in a corresponding value (in Categories! columns D-F respectively) based on partial match with column B or C (makes no difference to me if there's a delimiter like a space or not. Final data sheet should only have one of these columns though).
Things I've Tried:
I've tried a bunch of things. Nested IF formula with regexmatch works but seems clunky.
e.g. this formula in Categories! column D
=IF(REGEXMATCH($B2,LOWER(Parameters!$D$3)),Parameters!$D$3,IF(REGEXMATCH($B2,LOWER(Parameters!$D$4)),Parameters!$D$4,""))
I nested more statements changing out to the next cell in Parameters!D column (as in , manually adding $D$5, $D$6 etc) but this seems inefficient for a list thousands of words long. e.g. third topic will get very long once all dog breed types are added.
Any tips?
Functionality I haven't worked out:
if a string in Categories B or C contains more than one topic in the parameters I set out, is there a way I can have the first 2 to show instead of just the first one?
e.g. Cell A14 in Categories, how can I get a formula/automation to add both "Akita" & "German Shepherd" into the third topic? Concatenation with a CHAR(10) to add to new line is ideal format here. There will be other keywords that won't have both in there in which case these values will just show up individually.
Since this data set has a bunch of mixed breeds and all breeds are added as a third topic, it would be great to differentiate interest in mixes vs pure breeds without confusion.
Any ideas will be greatly appreciated! Also, I'm open to variations in layout and functionality of the spreadsheet in case you have a more creative solution. I just care about efficiently automating a tedious task!!
Try using custom function:
To create custom function:
1.Create or open a spreadsheet in Google Sheets.
2.Select the menu item Tools > Script editor.
3.Delete any code in the script editor and copy and paste the code below into the script editor.
4.At the top, click Save save.
To use custom function:
1.Click the cell where you want to use the function.
2.Type an equals sign (=) followed by the function name and any input value — for example, =DOUBLE(A1) — and press Enter.
3.The cell will momentarily display Loading..., then return the result.
Code:
function matchTopic(p, str) {
var params = p.flat(); //Convert 2d array into 1d
var buildRegex = params.map(i => '(' + i + ')').join('|'); //convert array into series of capturing groups. Example (Dog)|(Puppies)
var regex = new RegExp(buildRegex,"gi");
var results = str.match(regex);
if(results){
// The for loops below will convert the first character of each word to Uppercase
for(var i = 0 ; i < results.length ; i++){
var words = results[i].split(" ");
for (let j = 0; j < words.length; j++) {
words[j] = words[j][0].toUpperCase() + words[j].substr(1);
}
results[i] = words.join(" ");
}
return results.join(","); //return with comma separator
}else{
return ""; //return blank if result is null
}
}
Example Usage:
Parameters:
First Topic:
Second Topic:
Third Topic:
Reference:
Custom Functions
I've added a new sheet ("Erik Help") with separate formulas (highlighted in green currently) for each of your keyword columns. They are each essentially the same except for specific column references, so I'll include only the "First Topic" formula here:
=ArrayFormula({"First Topic";IF(A2:A="",,IFERROR(REGEXEXTRACT(LOWER(B2:B&C2:C),JOIN("|",LOWER(FILTER(Parameters!D3:D,Parameters!D3:D<>""))))) & IFERROR(CHAR(10)&REGEXEXTRACT(REGEXREPLACE(LOWER(B2:B&C2:C),IFERROR(REGEXEXTRACT(LOWER(B2:B&C2:C),JOIN("|",LOWER(FILTER(Parameters!D3:D,Parameters!D3:D<>""))))),""),JOIN("|",LOWER(FILTER(Parameters!D3:D,Parameters!D3:D<>""))))))})
This formula first creates the header (which can be changed within the formula itself as you like).
The opening IF condition leaves any row in the results column blank if the corresponding cell in Column A of that row is also blank.
JOIN is used to form a concatenated string of all keywords separated by the pipe symbol, which REGEXEXTRACT interprets as OR.
IFERROR(REGEXEXTRACT(LOWER(B2:B&C2:C),JOIN("|",LOWER(FILTER(Parameters!D3:D,Parameters!D3:D<>""))))) will attempt to extract any of the keywords from each concatenated string in Columns B and C. If none is found, IFERROR will return null.
Then a second-round attempt is made:
& IFERROR(CHAR(10)&REGEXEXTRACT(REGEXREPLACE(LOWER(B2:B&C2:C),IFERROR(REGEXEXTRACT(LOWER(B2:B&C2:C),JOIN("|",LOWER(FILTER(Parameters!D3:D,Parameters!D3:D<>""))))),""),JOIN("|",LOWER(FILTER(Parameters!D3:D,Parameters!D3:D<>"")))))
Only this time, REGEXREPLACE is used to replace the results of the first round with null, thus eliminating them from being found in round two. This will cause any second listing from the JOIN clause to be found, if one exists. Otherwise, IFERROR again returns null for round two.
CHAR(10) is the new-line character.
I've written each of the three formulas to return up to two results for each keyword column. If that is not your intention for "First Topic" and "Second Topic" (i.e., if you only wanted a maximum of one result for each of those columns), just select and delete the entire round-two portion of the formula shown above from the formula in each of those columns.

Google Sheet Conditional Formatting highlight 2 columns if one column contain certain text of 5 columns

In Google Sheet, I want to highlight only 2 columns out of 5 columns.
5 columns here but I want to highlight only 'Name' and 'Weight' columns if a cell contain the word 'Smith'
The outcome should be like this.
I want to input more name and if the name contain the word 'Smith', I want it to be automatically highlighted for name and weight columns.
I tried to use conditional formatting in Google sheet, and I could highlight only the name column.
This is what I tried.
Outcome was this.
You are not far, try the following formula in the conditional formatting:
=IF(REGEXMATCH($C3, "Smith"), 1, 0)
The formula given by #nabais works.
In conditional formatting though one does not need to use the starting IF function.
"Format cells if" is how conditional formatting rules are formed by default as noted in the official help page.
Create a rule.
Single color: Under "Format cells if," choose the condition that you want to trigger the rule. Under "Formatting style, choose what the
cell will look like when conditions are met.
Color scale: Under "Preview," select the color scale. Then, choose a minimum and maximum value, and an optional midpoint value. To choose
the value category, click the Down arrow Down Arrow.
So the following formula is all that is needed:
(Please adjust ranges to your needs)
=REGEXMATCH($G2, "Smith")
You can try the following code:
function highlight() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var data = ss.getRange('A1:E').getValues();
for (i = 1; i < data.length; i++) {
var tf = ss.getRange("B" + i).createTextFinder('smith');
tf.matchEntireCell(false);
tf.matchCase(false);
var result = tf.findNext();
if (result !== null) {
var range = result.getRow();
ss.getRange('B' + range).setBackground('Yellow');
ss.getRange('D' + range).setBackground('Yellow');
}
};
};

Create a True/False output column in Power BI that evaluates a column with text strings

I want to get a True/False Output column that evaluates another column of text strings
Table
Item Notes Carried
1 Bob was able to.. Y
2 Amy was not.... N
3 This is Bob..... N
In this example I want a way to know if I can evaluate the Notes column for specific text by row and have a column of True/False, for the example is there a way to evaluate for occurrence of "Bob"?
You can use Containsstring, adding a new column:
Carried = if(CONTAINSSTRING(Table[Notes], "Bob"), "Y", "N")
If you want to pass in more than one value, you can go with:
Carried = if(CONTAINSSTRING(Table[Notes], "Bob") || CONTAINSSTRING(Table[Notes], "Amy"), "Y", "N")

How to countif 56 exists in 156/56/2567 and only return true once? Google sheets

I have one sheet with data on my facebook ads. I have another sheet with data on the products in my store. I'm having trouble with some countifs where I'm counting how many times my product ID exists in a row where multiple numbers are. They are formatted like this: /2032/2034/2040/1/
It's easy on the rows where only one product ID exists but some rows have multiple ID's separated by a /. And I need to see if the ID exists as a exact match alone or somewhere between the /'s.
Rows with facebook ads data:
A1: /2032/2034/2040/1/
A2: /1548/84/2154/2001/
A3: /2032/1689/1840/2548/
Row with product data:
B1: 2034
C1: I need a countifs here that checks how many times B1 exists in column A. Lets say I have thousands of rows with different variations of A1 where B1 could standalone. How do I count this? I always need exact matches.
You can compare the number you want (56) with the REGEX #MonkeyZeus commented whith a little change -> "(?:^|/)"&B1&"(?:/|$)" so the end result is:
=IF(REGEXMATCH(A1, "(?:^|/)"&B1&"(?:/|$)"), true, false)
Example:
UPDATE
If you need to count the total of 56 in X rows you can change the "True / False" of the condition for "1 / 0" and then do a =SUM(C1:C5) on the last row:
=IF(REGEXMATCH(A1, "(?:^|/)"&B1&"(?:/|$)"), 1, 0)
UPDATE 2
Thanks for contributing. Unfortunately I'm not able to do it this way
since I have loads of data to do this on. Is there a way to do it with
a countif in a single cell without adding a extra step with "sum"?
In that case you can do:
=COUNTA(FILTER(A:A, REGEXMATCH(A:A, "(?:^|/)"&B2&"(?:/|$)")))
Example:
UPDATE 3
With the following condition you check every single possibility just by adding another COUNTIF:
=COUNTIF(A:A,B1) + COUNTIF(A:A, "*/"&B1) + COUNTIF(A:A, B1&"/*") + COUNTIF(A:A, "*/"&B1&"/*")
Hope this helps!
try:
=COUNTIF(SPLIT(A1, "/"), B1)
UPDATE:
=ARRAYFORMULA(IF(A2<>"", {
SUM(IF((REGEXMATCH(""&DATA!C:C, ""&A2))*(DATA!B:B="carousel"), 1, )),
SUM(IF((REGEXMATCH(""&DATA!C:C, ""&A2))*(DATA!B:B="imagepost"), 1, ))}, ))

Removing a row containing a specific text in Google Sheets

I have a data set of around 3000 columns, but some of the columns have several cells that contain cells "na". These rows have no importance since they don't have data that I will need, is there a command in google sheets that can either highlight the entire row that contains that text or delete the entire row containing that text?
Any help would be appreciated.
https://docs.google.com/spreadsheets/d/1u8OUfQOzgAulf1a8bzQ8SB5sb5Uvb1I4amF5sdGEBlc/edit?usp=sharing
My document ^.
you can use this formula to color all na rows:
=ARRAYFORMULA(REGEXMATCH(TRANSPOSE(QUERY(TRANSPOSE($A1:$Z),,999^99)), " na "))
This answer based on what I understand, sorry if I'm wrong. You can use conditional formatting to highlight all NA text
This is what rules I used
Here are another answers that may help you
Delete a row in Google Spreadsheets if value of cell in said row is 0 or blank
Google Sheets: delete rows containing specified data
Deleting Cells in Google Sheets without removing a whole row
Sorry for bad English.
I'm not sure if my understing is well but see below what you can do.
This is a google script function which color the whole column where "na" is in
function myFunction() {
//get the spreadsheet where the function is running
var ss = SpreadsheetApp.getActive()
//Replace "the name of your sheet" by your sheet name" be careful its case sensitive.
var sheet = ss.getSheetByName("The name of your sheet")
//Get all your data as an array (If your sheet has no header, change 2 by 1 and (sheet.getLastRow()-1) by sheet.getLastRow())
var values = sheet.getRange(2,1,(sheet.getLastRow()-1), sheet.getLastColumn()).getValues();
//For each column
for (var i = 0; i< sheet.getLastColumn(); i++){
//using function map is helping to select one column by one column
var mapValues = values.map(function(r){return r[i]});
//Searching your keyword in the column, in your case it's "na"
var position = mapValues.indexOf("Put the string that you are looking for, in your case 'na'");
//if at least there is one "na" inside the column
if( position >-1){
//then this color have to get red color as a background
var wholeColumn = sheet.getRange(2,(i+1),(sheet.getLastRow()-1));
wholeColumn.setBackground("red");
}
}
}``
Let me know if it works