BigCommerce Handlebars Convert Value to Integer Only - bigcommerce

My products in BigCommerce have a custom field called "Special" and I need to convert the value of these to integers on the category pages for the purposes of ordering (with CSS). They can't be ordered by anything else... it must be numerically by the value. Not all of the products have this custom field (these will be at the end of the ordered list). Some of them contain alphanumeric values which I need to convert to integers only. Furthermore, some contain an ampersand (example: 14&18) which I would require just the first number.
{{#filter custom_fields 'Special' property='name'}}
{{#if value}}
<!-- Add Class -->
{{/if}}
{{#else}}
<!-- Standard Class -->
{{/else}}
{{/filter}}
Given that information, how can I achieve converting the string to an integer and taking the first number when an ampersand is present? I am not interested in using JavaScript unless absolutely necessary.
Update
Is there an easier way to replace multiple characters than doing it like this...
{{#replace "A" value}}{{else}}
{{#replace "a" value}}{{else}}
{{#replace "B" value}}{{else}}
{{#replace "b" value}}{{else}}
{{#replace "C" value}}{{else}}
{{#replace "c" value}}{{else}}
{{#replace "D" value}}{{else}}
{{#replace "d" value}}{{else}}
{{#replace "E" value}}{{else}}
{{#replace "e" value}}{{else}}
{{#replace " " value}}{{else}}
{{value}}
{{/replace}}
{{/replace}}
{{/replace}}
{{/replace}}
{{/replace}}
{{/replace}}
{{/replace}}
{{/replace}}
{{/replace}}
{{/replace}}
{{/replace}}
Also, where would the split go?

The way filter works is to return all matching values as an array, and then loop through them. This means that if some products do not have this custom field, they will not display any code inside the filter. Hence, we need to put them in an else block on the filter itself.
{{#filter custom_fields 'Special' property='name'}}
<!-- Add Class -->
{{else}}
<!-- Standard Class -->
{{/filter}}
To get the first number in the case where there is an ampersand is easy. We use first and split helpers: {{first (split value '&')}}
Lastly, getting rid of the letters is probably the hardest part. Your best options are to use split and join, or replace. However, you would need to do this for each letter:
{{first (split (join (split (join (split (join (split (lowercase value) 'a') '') 'b') '') 'c') '') '&')}}
Note: edited previous line to tie it all into one line based on new information.

Related

Remove one character without removing all of same characters

So, I need some help from removing one character without removing all of them.
For example, I want to remove "x" but I don't want to remove all of "x" letter what does a text contain. I made code but it removes all "x" letters from a text.
TextBox1.Text = Replace(TextBox1.Text, "x" "")
The VBA Replace function has a few more optional arguments which you could use: start and count. If you only want to replace the first occurrence do:
TextBox1.Text = Replace(TextBox1.Text, "x", "", 1, 1)

Find and delete text within a cell in excel

I have pasted text in cell A1 that may look like this:
"Vanguard - Total Market - Composite" or "Vanguard - Total Market - Commingled"
I want to cut off the " - Composite" or " - Commingled" and return the rest in cell B1. Currently I'm using this formula in B1:
=LEFT(A1,FIND(" - Composite",A1)-1)
However, I can't figure out how to look for multiple terms (i.e. Composite or Commingled). Is there a formula that could accomplish this? If not, how could I do it in VBA?
Thanks for the help!
If I understand correctly, you're simply looking to strip everything past the second occurrence of -, i.e. returning the trimmed (extra whitespace removed) text left of the first - character. Adapting this solution to locate the last word in the string, this would be it:
=TRIM(SUBSTITUTE(LEFT(A1,LEN(A1)-LEN(MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"-",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"-",""))))+1,LEN(A1)))),"-","",2))
This formula will work with or without the spaces around the -, and regardless of what follows:
If even spacing is important, you can wrap it with SUBSTITUTE functions:
=SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(LEFT(A1,LEN(A1)-LEN(MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"-",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"-",""))))+1,LEN(A1)))),"-","",2)),"-"," - ")," "," ")
And now you have clean, identically-formatted output for all cases:
I've made this formula that will work with many possibilities as long you fill the possibilities range, not needing to change the formula when there's a new one. Here it is:
=LEFT(A2,FIND(INDIRECT(ADDRESS(SUMPRODUCT((--ISNUMBER(SEARCH(Possibilities,A2)))*ROW(Possibilities)),SUMPRODUCT((--ISNUMBER(SEARCH(Possibilities,A2)))*COLUMN(Possibilities)))),A2)-1)
You can use the SUBSTITUTE function and chain multiple calls:
=SUBSTITUTE(SUBSTITUTE(A1," - Composite", ""), " - Commingled", "")

VBA script in excel to find and highlight text

I am looking for a VBA script that will help me find certain keywords in a cell and if found highlight the entire row. Below are my requirements:
I have a database of words eg hell, get out, shut up, don't you dare etc. I need a macro to search the data in column "E" of excel and in case any of the cell in column "E" contains any word listed in the database (irrespective of the case of the word upper or lower)the entire row is highlighted. The word can be in the beginning, middle or end of the cell and the macro should be able to find that word and highlight the column.
Seeking help from all VBA masters for this.
You can do this with conditional formatting, instead of VBA.
Conditional formatting works by applying a 'second formula' to a given cell. If the 'second formula' results in TRUE, then special formatting conditions can be applied.
EXAMPLE CONDITIONAL FORMATTING
For example, if you have a single column of Data, A:A, and you want to check if that column has the exact string "hello world", you could add a conditional format [Home ribbon, Styles section, Conditional Formatting] that turns a cell yellow with this formula:
=$A1="hello world"
This will only result in TRUE if the cell in column A at that row equals exactly "hello world" [note that Column A has an absolute-reference $, and row 1 does not, so row 1 is relative to the position of the cell in the condiitonal format rule].
To check to see if any row in column A includes hellow world, we need to add a SEARCH function, which checks to see if a small search string is inside of a larger string:
=SEARCH("hello world",$A1)>0
Because SEARCH by default returns the first character in a larger string that matches the search term (and if it finds nothing, it returns #N/A), we check to see if our search for "hello world" in column A returns a number.
SEARCHING MULTIPLE COLUMNS
Now, to see if ANY column, say from A-D, includes "hello world", we concatenate each value of each column so that it gives us a single string, which we can search through for "hello world", like so:
=SEARCH("hello world",$A1&$B1&$C1&$D1)>0
This will first create a single string, equal to A1 & B1 & C1 & D1 all in a row. Then it will search that newly created string to see if "hello world" is inside it, and return a number value if it is.
ARRAY FORMULA BASICS
Finally, we need to do the tricky part - searching for multiple terms instead of just "hello world". This is called an Array Formula. An array formula works by performing a single operation on multiple cells, and then returning multiple results in an Array. In an Excel sheet, an array formula must be confirmed with CTRL + SHIFT + ENTER (instead of just ENTER), but in conditional formatting, you actually don't need to do anything special - it will recognize an array formula without a special command.
As an example of conditional formatting, see this example, which checks whether any value from A1:A5 = 10, and if it does, it gives us the value in B1:B5:
=IF(A1:A5=10,B1:B5,"")
Remember in Excel on a worksheet, this would be confirmed by pressing CTRL + SHIFT + ENTER. If you do test this, it will give you the following result, assuming A2 = 10 and A5 = 10:
={"";B2;"";"";B5}
This result would actually be hidden, because Excel can't "collapse" an array function on its own. So assume column B had values, and we actually want to sum them together. We would then wrap the Array formula in a SUM function:
=SUM(IF(A1:A5=10,B1:B5,""))
As you can see if you test this, we have actually created our own SUMIF function, using Array formulas instead of the built-in SUMIF.
SEARCHING FOR MULTIPLE TERMS WITH ARRAY FORMULAS
So now we apply these principles to the conditional formatting, to create an array formula which will check our concatenated 'NEW STRING' for any number of provided terms, as follow [Assumes the search terms are typed into cells E1:E10]:
=SUM(SEARCH($E$1:$E$10,$A1&$B1&$C1&$D1)>0)
This formula can be placed as a conditional formatting rule which reaches all of A:D. Set the rule to highlight / change format in whatever way you like.

VBA syntax to find an if conditional statement field code

I am searching for the correct syntax for my macro to find an IF Conditional statement field code.
I need to search for {IF{DOCVARIABLE"CODE"} = "YES" "A" "B"} to remove this and replace with "B" (the false statement).
When searching for a word field like {DOCVARIABLE "CODE"} alone, I write as: Text1 = "CODE" but when there is an IF & Text & Yes & TRUE, FALSE like above, how do I write?
Consolidated comment (OP crossposted elsewhere):
For that specific sequence, you can search for
^19IF^19DOCVARIABLE"CODE"
Word will select the entire IF field (so you will then need to parse the entire IF field anyway), but it should not select e.g.
{IF{DOCVARIABLE"BOX"}
If there is some variation (e.g. additional whitespace in the string) you may be able to use something like
^19^wIF^w^19^wDOCVARIABLE^w"CODE"

Find and replace a character to create a new cell

For example if have
" size:1, color:red CH 4"
in one cell, I want
" size:1. color:red" new cell "4"
Basically, find "CH" and replace it with a new cell and everything in front of it.
Anyone know how?
Yeap, I do this stuff all the time. You'll need two functions, they're found under the text section. THey're called SEARCH & MID.
What you are going to want to do is use an excel function SEARCH to first find the position of "CH" - this will be different depending on the length of the other strings.
Then you use another excel function, MID, to extract a substring starting from the location you found in the first function. Now, you can copy the column that just has "4" in it and use "Paste Special" from the right click menu. In paste special, choose "values". This will allow you to move the actual value the function generated and not the function itself.
Detailed Example
A1 B1 C1
size:1, color:red CH 4 =SEARCH("CH",A1)+3 =MID(A1,B1,1)
(Text we want to decode) (Returns 21) (returns 4)
+3 is because CH starts at the 19th character, then there is the "H" and a space, making the number your 3rd digit (+3). Then do the "Paste Special" procedure on cell C1, and you have isolated that value.