If statement within a mailmerge - if-statement

I`m trying to create a mail merge for attendence certificates that brings in an attendence value from an excell spreadasheet.
The attendence column in the spreasheet will hold either a two digit numerical value or blank.
I would like it to display the attendence percentage if there is a value, else display nothing.
The logic in the mailmerge is {If { MERGEFIELD Attendance} = "[0-9]*", "{MERGEFIELD Attendance}", ""}
Could anyone tell me where I am going wrong?

Assuming Attendance holds the percentage (e.g. is 03 for 3%), use { IF "{ MERGEFIELD Attendance}" = "??" "{ ={ MERGEFIELD Attendance } }%" }. You can get rid of some of the quotes and spacing in there if you prefer, but AFAICR you need at least one of the spaces around the "=".
i.e. you can't use regex wildcards (not even the Word version of regex wildcards). The only wildcards allowed here AFAIK are "?" and "*", and you can only use "*" at one end of the expression.

Related

SUM multiple values after a substring within all cells in a column in Google Sheets

For an open source chat analyser in Google Sheets, I need to extract all numeric values after a substring (Example), then total them.
For example, if a cell contains Example1 another text 123 Example500 text, Example1 and Example500 should be extracted out, and their numeric values summed to 501.
This is complicated further by needing to obtain the total for a column of messages.
What I've tried already:
=REGEXEXTRACT(A1, "Example(\d+)"): This only extracts the first matching value, but works!
=SUM(SPLIT(A1, "Example")): This works for messages that only include my target string, but falls apart when other strings are included. The output could possibly be filtered to results that start with a number, but this is very messy and possibly a red herring.
CONCATENATEing all my cells together, then searching for numbers. This is error-prone due to additional numbers within messages.
Another idea is to substitute each Example(\d+) to $1 the captured digit and space |. or replace anything else with empty string (regex101 demo). Knowing that $1 is unset on the right side of the alternation. Then split on space and sum up digits (any other occurring digits have been removed). If Example is a placeholder, replace with e.g. [[:alpha:]]+ for one or more alphabetic characters.
=IF(ISTEXT(A1);SUM(SPLIT(REGEXREPLACE(A1;"Example(\d+)|.";"$1 ");" "));0)
I added IF(ISTEXT(A1);...) for only processing text in the source field (to avoid errors). Else if empty or no text it's set to 0. Just remove if the field always contains text and this is unneeded.
Edit from #TheMaster: As a array formula, we can use BYROW
=BYROW(A:A; LAMBDA(row; IF(ISTEXT(row); SUM(SPLIT(
REGEXREPLACE(row;"Example(\d+)|.";"$1 ");" "));)))
try:
=LAMBDA(x, REGEXEXTRACT(A1, "(\w+)\d+")&
SUMPRODUCT(IF(IFERROR(REGEXMATCH(x, "\w+\d+")),
REGEXEXTRACT(x, "\w+(\d+)"), )))(SPLIT(A1, " "))
update 1:
=LAMBDA(x, REGEXEXTRACT(A1, "(\D+)\d+")&
SUMPRODUCT(IF(IFERROR(REGEXMATCH(x, "\D+\d+")),
REGEXEXTRACT(x, "\D+(\d+)"), )))(SPLIT(A1, " "))
update 2:
=INDEX(LAMBDA(xx, REGEXEXTRACT(xx, "(\D+)\d+")&
BYROW(LAMBDA(x, IF(IFERROR(REGEXMATCH(x, "\D+\d+")),
REGEXEXTRACT(x, "\D+(\d+)"), ))(SPLIT(xx, " ")), LAMBDA(x, SUMPRODUCT(x))))
(A1:INDEX(A:A, MAX((A:A<>"")*ROW(A:A)))))
if you start from A2 just change A1: to A2:

Regex match text followed by curly brackets

I have a text like this:
"entity"
{
"id" "5040044"
"classname" "weapon_defibrillator_spawn"
"angles" "0 0 0"
"body" "0"
"disableshadows" "0"
"skin" "0"
"solid" "6"
"spawnflags" "3"
"origin" "449.47 5797.25 2856"
editor
{
"color" "0 0 200"
"visgroupshown" "1"
"visgroupautoshown" "1"
"logicalpos" "[-13268 14500]"
}
}
What would regex expression be to select only that part in Notepad++:
editor
{
"color" "0 0 200"
"visgroupshown" "1"
"visgroupautoshown" "1"
"logicalpos" "[-13268 14500]"
}
First word is always "editor", but the number of lines and content in curly brackets may vary.
editor\s*{\s*(?:\"[a-z]*\"\s*\".*\"\s*)*\}
Demo
Also tested it in Notepad++ it works fine
The simplest way to find everything between curly brackets would be \{[^{}]*\} (example 1).
You can prepend editor\s* on it so it limits the search to only that specific entry: editor\s*\{[^{}]*\} (example 2).
However... if any of the keys or value strings within editor {...} contain a { or }, you're going to have edge cases.
You'll need to find double-quoted values and essentially ignore them. This example shows how you would stop before the first double quote within the group, and this example shows how to match up through the first key-value pair.
You essentially want to repeatedly match those key-value pairs until no more remain.
If your keys or values can contain \" within them, such as "help" "this is \"quoted\" text", you need to look for that \ character as well.
If there are nested groups within this group, you'll need to recursively handle those. Most regex (Notepad++ included) don't handle recursion, though, so to get around this, you copy-paste what you have so far inside of the code if it happens to come across more nested { and }. This does not handle more than one level of nesting, though.
TL;DR
For Notepad++, this is a single line regex you could use.

How can I separate a string by underscore (_) in google spreadsheets using regex?

I need to create some columns from a cell that contains text separated by "_".
The input would be:
campaign1_attribute1_whatever_yes_123421
And the output has to be in different columns (one per field), with no "_" and excluding the final number, as it follows:
campaign1 attribute1 whatever yes
It must be done using a regex formula!
help!
Thanks in advance (and sorry for my english)
=REGEXEXTRACT("campaign1_attribute1_whatever_yes_123421","(("&REGEXREPLACE("campaign1_attribute1_whatever_yes_123421","((_)|(\d+$))",")$1(")&"))")
What this does is replace all the _ with parenthesis to create capture groups, while also excluding the digit string at the end, then surround the whole string with parenthesis.
We then use regex extract to actuall pull the pieces out, the groups automatically push them to their own cells/columns
To solve this you can use the SPLIT and REGEXREPLACE functions
Solution:
Text - A1 = "campaign1_attribute1_whatever_yes_123421"
Formula - A3 = =SPLIT(REGEXREPLACE(A1,"_+\d*$",""), "_", TRUE)
Explanation:
In cell A3 We use SPLIT(text, delimiter, [split_by_each]), the text in this case is formatted with regex =REGEXREPLACE(A1,"_+\d$","")* to remove 123421, witch will give you a column for each word delimited by ""
A1 = "campaign1_attribute1_whatever_yes_123421"
A2 = "=REGEXREPLACE(A1,"_+\d*$","")" //This gives you : *campaign1_attribute1_whatever_yes*
A3 = SPLIT(A2, "_", TRUE) //This gives you: campaign1 attribute1 whatever yes, each in a separate column.
I finally figured it out yesterday in stackoverflow (spanish): https://es.stackoverflow.com/questions/55362/c%C3%B3mo-separo-texto-por-guiones-bajos-de-una-celda-en...
It was simple enough after all...
The reason I asked to be only in regex and for google sheets was because I need to use it in Google data studio (same regex functions than spreadsheets)
To get each column just use this regex extract function:
1st column: REGEXP_EXTRACT(Campaña, '^(?:[^_]*_){0}([^_]*)_')
2nd column: REGEXP_EXTRACT(Campaña, '^(?:[^_]*_){1}([^_]*)_')
3rd column: REGEXP_EXTRACT(Campaña, '^(?:[^_]*_){2}([^_]*)_')
etc...
The only thing that has to be changed in the formula to switch columns is the numer inside {}, (column number - 1).
If you do not have the final number, just don't put the last "_".
Lastly, remember to do all the calculated fields again, because (for example) it gets an error with CPC, CTR and other Adwords metrics that are calculated automatically.
Hope it helps!

Create an If statement comparing a custom field MS Word

I'm trying to create an if statement (in MS Word) that looks at a custom field.
The custom field is DocProperty Client_ABV
I want it to print a line of text if client_abv matches a certain value else be completely blank (or delete the empty line if possible)
I believe it needs to look something like this:
{IF DocProperty.Client_ABV="Test" "Print this line if Test",""}
I've very little experience with this function in Word but I have some with conditional programming.
Can anyone shed any light. I've been googling it for the last 45 minutes and have had little success with the example pages I've found.
Use Ctrl+F9 to insert the field code { brackets }. They look like wavy brackets, but these are actually special "escape codes" that tell Word this is a field code.
You need a pair of brackets for both the IF and the DocProperty fields.
When performing a string comparison it's a good idea to put "quotes" around the field code as well as around the literal string.
There is no punctuation in the DocProperty field code (no period). And no comma between the true/false evaluation, only a space between the closing " and opening ".
If a paragraph mark should be part of the true/false evaluation (for example, you want to suppress the paragraph mark if the comparison is false) include it inside the "quotes" for the evaluation result. The field code will look a bit odd, but that does work.
For example:
{ IF "{ DocProperty Client_ABV }"="Test" "Print this line if Test¶
" ""}

Using regular expression in Word merge condition

Having trouble getting a condition to match when creating a Word mail merge. What I want is to match based on the first letter of the field. If the first letter is K-Z, then it should evaluate True.
I have the following in Word:
{ IF { MERGEFIELD Provider } = "[K-Z]*" "Person1" "Person2" }
which does not work. I've tried escaping the square brackets, but this also has had no success.
I can't find anything useful on a search. Has anyone got any ideas how to make this work?
You can't use regex expressions in Word IF fields - not even the limited regex that you can use in Word's Find and Replace function. In an IF field, all you get is the wildcards ? (to match any character) and * (to match multiple characters). Even these have their limitations.
So you have to find another way. One is the tedious one where you enumerate all the possibilities - in this case you could use something like
{ SET KtoZ 0
}{ IF "{ MERGEFIELD Provider }" = "K*" "{ SET KtoZ 1}"
}{ IF "{ MERGEFIELD Provider }" = "L*" "{ SET KtoZ 1}"
}{...
}{ IF "{ MERGEFIELD Provider }" = "Z*" "{ SET KtoZ 1}"
}{IF { REF KtoZ } = 1 "Person1" "Person2" }
(with similar IF fields for M..Y where I have put "..."). If you need to deal with upper/lower case you can add a suitable switch to your MERGEFIELD fields to.
Another way, depending on your situation and on the data source, might be to do the comparison in the data source. That requires either that you can create a view (or in Access, a query) that performs the comparison and returns, for example, a field called KtoZ, or that you can construct your query in SQL and issue it in a Word VBA OpenDataSource call. In the latter case, your data source must use a SQL dialect that lets you do that, and your query must be less than the 255/511 character limit that Word VA imposes.