Google Sheets Parse Mathematical Expressions in Text Format - google-sheets

Is there a way to take a text in a cell that says 5*5 and to parse to make the result 25... using QUERY function or any of the other built-in functions...?

it would be like this:
=QUERY(SPLIT(B19, "*"), "select Col1*Col2 label Col1*Col2''")

No. But if you have all such Mathematical expressions in a defined range(say,A5:A7), then you can use Find and Replace
FIND:
^
REPLACE:
=
Range:
Sheet1!A5:A7
Checkmark "Search using regular expressions". Click Replace all. Otherwise you'll need scripts.

IF you are just looking to parse simple mathematical expressions (+,-,x,/), then it can be done, but not in a single formula.
First, you can split the text based on your math operator, which will provide the numbers. Then you can use regexextract to output the math operator, then use a nested if to perform the needed math operation on the numbers, based upon that operator. You can hide the intermediate columns to put the answer next to the original.
Note that if you need to do more complex operations or operations on more than two numbers, this will not work.

Related

How to make a word mentioned in column A bold in column B where that word is mentioned in a sentence (Google Sheets)?

Basically, I am trying to create conditional formatting that does the following:
Simple example
I really would like to just make bold a word (not a whole sentence) in column B that is mentioned in column A.
I tried many different formulas in the "Value or formula" field:
=REGEXMATCH($B1,A1)
=REGEXMATCH($B1,"<"&A1&">")
=REGEXMATCH(B1,"\b"&A1&"\b")
=ARRAYFORMULA(REGEXMATCH(SPLIT(B1," "),"\b"&A1&"\b"))
...but none of these work.
This is actually sort of possible, if you are willing to stretch your definition of 'bold'. It's absolutely right that you can't make a particular part of a text string within a cell bold (or italic/underlined/coloured/etc.) in a way that replicates the effect if you manually select an area of a text string and format it using the menu bar options - effectively you are setting some metadata which sits 'outside' of the cell so isn't accessible to a formula.
However, Unicode fonts generally contain a region within their character map which corresponds to a bold version of the underlying font, and so with a little bit of trickery it's possible to substitute characters with their bold version. Here's a formula which achieves something like your original request:
=let(wordstobold,A1:A2,
sentencestosplit,B1:B2,
boldwords,map(wordstobold,lambda(eachword,concatenate(map(split(regexreplace(eachword,"(.)","$1_"),"_"),lambda(eachchar,filter(BOLDCHARS(),exact(STDCHARS(),eachchar))))))),
splitsentences,map(sentencestosplit,wordstobold,lambda(sentence,word,split(sentence,word,false))),
presplits,choosecols(splitsentences,1),
postsplits,choosecols(splitsentences,2),
map(presplits,boldwords,postsplits,lambda(x,y,z,x&y&z)))
The formula exploits the fact that SPLIT treats a multicharacter delimiter as a single entity by default, so if you pass the word to be bolded to SPLIT as the delimiter for the sentence it will split the sentence into two halves, presplit and postsplit. The substitution of the normal characters for bold characters could be done in a number of ways, but what I'm doing here is to explode the word to be bolded into individual characters and then MAPping a the equivalent bold character onto each one using FILTER/EXACT.
A couple of 'helper' Named Functions are required: STDCHARS() & BOLDCHARS(); these don't accept any parameters and are a means of storing the character sets for the part of the formula which swaps characters for their bold equivalents. It would be possible to integrate these into the formula, albeit at the expense of readability.
STDCHARS():
={"a";"b";"c";"d";"e";"f";"g";"h";"i";"j";"k";"l";"m";"n";"o";"p";"q";"r";"s";"t";"u";"v";"w";"x";"y";"z";"A";"B";"C";"D";"E";"F";"G";"H";"I";"J";"K";"L";"M";"N";"O";"P";"Q";"R";"S";"T";"U";"V";"W";"X";"Y";"Z";"0";"1";"2";"3";"4";"5";"6";"7";"8";"9"}
BOLDCHARS():
={"๐—ฎ";"๐—ฏ";"๐—ฐ";"๐—ฑ";"๐—ฒ";"๐—ณ";"๐—ด";"๐—ต";"๐—ถ";"๐—ท";"๐—ธ";"๐—น";"๐—บ";"๐—ป";"๐—ผ";"๐—ฝ";"๐—พ";"๐—ฟ";"๐˜€";"๐˜";"๐˜‚";"๐˜ƒ";"๐˜„";"๐˜…";"๐˜†";"๐˜‡";"๐—”";"๐—•";"๐—–";"๐——";"๐—˜";"๐—™";"๐—š";"๐—›";"๐—œ";"๐—";"๐—ž";"๐—Ÿ";"๐— ";"๐—ก";"๐—ข";"๐—ฃ";"๐—ค";"๐—ฅ";"๐—ฆ";"๐—ง";"๐—จ";"๐—ฉ";"๐—ช";"๐—ซ";"๐—ฌ";"๐—ญ";"๐Ÿฌ";"๐Ÿญ";"๐Ÿฎ";"๐Ÿฏ";"๐Ÿฐ";"๐Ÿฑ";"๐Ÿฒ";"๐Ÿณ";"๐Ÿด";"๐Ÿต"}

Finding characters in the middle of a string that includes "

I have the following extract of code. My aim is to extract the value 7.4e-07 after the symbol DAN. My usual go-to formula (using MID & FIND formula's) for this can't work because DAN is surrounded by ", and therefore confuses the formula.
{"data":{"log":{"address":[{"balances":[{"currency":{"address":"example1","symbol":"ROB"},"value":0.0},{"currency":{"address":"example2","symbol":"DAN"},"value":7.4e-07},{"currency":{"address":"example3","symbol":"COLIN"},"value":0.0},{"currency":{"address":"example4","symbol":"BOB"},"value":0.0},{"currency":{"address":"example5","symbol":"PAUL"},"value":13426.64}}}
I will always need to find the number shown in the 'value' after DAN. However, all other data surrounding will change so cannot be used in the search formula.
Any help would be appreciated.
The extract the digit you want, it can be achieved by using regex, split, index, here is the formula, accept if help :)
=index(split(REGEXEXTRACT(A1,"\""DAN\""},\""value\"":[\d.a-zA-Z-]+"),":"),0,2)
This is the regex I used to extract the value including the beginning text
"DAN"},"value":[\d.a-zA-Z-]+
This is outcome from the regex,
You could try an arrayformula to work down the sheet, extracting all values after 'DAN':
=arrayformula(regexreplace(A1:A,".*(DAN...........)([\w\.\-]*)(\}.*)","$2"))

Google Sheets/Excel: IMAGE() formula into plain URL

I am converting a Google Sheets file into a CSV format.
Some cells have an image inserted into them via =IMAGE("https://placekitten.com/200/300")
I need to be able to extract in plain text https://placekitten.com/200/300
Is this possible? I've tried SUBSTITUTE but of course that doesn't work.
Thanks to whoever can solve this!
try:
=REGEXEXTRACT(FORMULATEXT(A2); """(.*)""")
I thought that explaining #player0โ€™s answer would be nice, especially for future people.
His answer can be split in 2 parts:
Using FORMULATEXT to get the formula
Using a regular expression to extract the URL from it
The first part is FORMULATEXT. It is pretty self explanatory: it takes a cell and returns the formula as string.
The second part is using a regular expression (see wikipedia article and documentation on the exact syntax used by Google) to extract the URL. In this case #player0 relies on the fact that the URL is the only quoted text of that formula. So using the regex "(.+)" will match the quoted part inside the quotes of the formula, and thus only the URL. When using literal strings in a formula, you need to surround them with quotes (eg hello as "hello"). Double quotes need to be doubled (eg aaa"aaa is written as "aaa""aaa"). So "(.+)" becomes """(.+)""".
References
FORMULATEXT (Docs Editors Help)
REGEXEXTRACT (Docs Editors Help)
Regular expression (Wikipedia)
RE2 syntax (Googleโ€™s RE2 repository on GitHub)

how to add subscript(or superscript) in notion block๏ผŸ

i am new in using notion as a notebook.Before this,i use oneNote instead,because i found that notion is more convinient in same ways.But one thing making me annoyed is that i can't add subscript(or superscript) in notion text blockใ€‚Are there any fellows can handle this๏ผŸ
as shown๏ผš
text with subscript i need
when i edit in notion block i get like this
For subscript:
/math
Then C_1 renders C1
For superscript:
/math
Then C^2 renders C2
In the latest version as of writing, you could use:
$$c^2$$ - for superscript
$$c_2$$ - for subscript
If you write any mathematical equation between $$ $$, it will render beautifully on notion.
For more than 1 character superscript
/math
P^{xyz}
Result: Pxyz
For more than 1 character subscript
/math
P_{xyz}
Result: Pxyz
In fact, this works for 1 or many characters as subscript or superscript.
Even I just started using notion and faced the same problem. The solution I found was to keep a tab of laTex containing the syntax for all the required math formulae open. It is only temporary as you will get used to them pretty quickly.
You can use tex to write things is superscript or subscript or basically any type of formulae , but its still in tex.
I did and worked on my Notion
Step 1: Choose what to add subscript(or superscript) in notion block (Example a^n)
Step 2: Choose symbol sqrt(x)
Step 3: Press "Done"
In Notion, there are two ways to add math formulas: inline equations and block equations. Both use KaTeX formatting to show the formulas. KaTeX is a TeX display formulas method based on JavaScrpt language. When you place a block or inline equation, for your example, you have to do something like that in the code:
w_{1}=0 ~,~ w_{2}=1
Note that spaces are not rendered, you gotta use ~ separate things, and the { } on these subscripts are optional because when not placed, the "" will take just the first next character. To do superscript, change the "" to "^". You can combine both in the same main variable, like w_2^2=1.
In the following links, you can see a Notion's guide about how to use it, the supported features KaTeX allows you to do, and a link to easy creating formulas.
Notion:
https://www.notion.so/help/math-equations
KaTeX Supported Functions:
https://katex.org/docs/supported.html or
https://katex.org/docs/support_table.html
Easy web TeX editor:
https://atomurl.net/math/
Take time to try and explore writing by typping. Then, when you get used to it, it flows easily like writing regular text.
For anyone stumbling upon this question who does not want to write math equations but simply wants to add a couple of superscript or exponent elements to a Notion page, for instance to write footnotes: there seems to be no way to add "simple" superscript (inline and with the same font), but you can use superscript unicode characters, for instance with a tool such as this one.
It is not perfect as many characters are not available, but will cover manyยน commonยฒ usesยณ of superscriptแถœแถฆแต—แตƒแต—แถฆแต’โฟ โฟแต‰แต‰แตˆแต‰แตˆ.

Character Replacements

I have a UniCode string UniStr.
I also have a MAP of { UniCodeChar : otherMappedStrs }
I need the 'otherMappedStrs' version of UniStr.
Eg: UniStr = 'ABC', MAP = { 'A':'233','B':'#$','C':'9ij' }, Result = '233#$9ij'
I have come up with the formula below which works;
=ArrayFormula(JOIN("",VLOOKUP(REGEXEXTRACT(A1,REPT("(.)",LEN(A1))),MapRange,2,FALSE)))
The MAP being a whole character set (40 chars) is quite large.
I need to use this function in multiple spreadsheets. How can I subsume the MAP into the formula for portability ?
Is there a better way to iterate a string other than the REGEXEXTRACT method in formula ? This method has limitation for long strings.
I also tested the below formula. Problem here is it gives 2 results (or the size of the array within SUBSTITUTE replacement). If 3 substitutions made, then it gives three results. Can this be resolved ?
=ArrayFormula(SUBSTITUTE(A1,{"s","i"},{"#","#"}))
EDIT;
#Tom 's first solution appears best for my case (1) REGEX has an upper limit on search criteria which does not hinder in your solution (2) Feels fast (did not do empirical testing) (3) This is a better way to iterate string characters, I believe (you answered my Q2 - thanks)
I digress here. I wish google would introduce Named-Formulas or Formula-Aliases. In this case, hypothetically below. I have sent feed back along those lines many times. Nothing :(
MyFormula($str) == ArrayFormula(join(,vlookup(mid($str,row(indirect("1:"&len($str))),1), { "A","233";"B","#$";"C","9ij" },2,false)))
Not sure how long you want your strings to be, but the more traditional
=ArrayFormula(join(,vlookup(mid(A1,row(indirect("1:"&len(A1))),1), { "A","233";"B","#$";"C","9ij" },2,false)))
seems a bit more robust for long strings.
For a more radical idea, supposing the maximum length of your otherMappedStrings is 3 characters, then you could try:
=ArrayFormula(join(,trim(mid("233 #$9ij",find(mid(A1,row(indirect("1:"&len(A1))),1), "ABC")*3-2,3))))
where I have put a space in before #$ to pad it out to 3 characters.
Incidentally the original VLOOKUP is not case sensitive. If you want this behaviour, use SEARCH instead of FIND.
You seem to have several different Qs, but considering only portability, perhaps something like the following would help:
=join(,switch(arrayformula(regexextract(A1&"",rept("(.)",len(A1)))),"A",233,"B","#$","C","9ij"))
extended with 37 more pairs.

Resources