remove parentheses only when they are there - google-sheets

I have google sheets, and trying to create a function that will only get rid of the () when they are there and times the number by a negative only when i get rid of the (), but if i don't have any or don't get rid of any it can just go on and complete the function i have below.
=IF(RIGHT('Cash Flow'!B17,1)="B",LEFT('Cash Flow'!B17,LEN('Cash Flow'!B17)-1)\*1000000000,IF(RIGHT('Cash Flow'!B17,1)="M",LEFT('Cash Flow'!B17,LEN('Cash Flow'!B17)-1)\*1000000,'Cash Flow'!B17))

you can remove parenthesis this way:
=REGEXREPLACE(B1; "[\(\)]"; )

Related

How to iterate over a countif checker in an if function

Im not an expert at excel/sheets. I wanted to make a dynamic function that would stop user error by doing all of the work. The function basically uses regex to get the first letters and adds a number i.e. stell ball = sb01, stell cage = sc01 and so on. My problem grows when I also want to be able to catch multiple entries and potentially offer the next iteration. i.e. stell ball = sb01, stell ball = sb02.
Here is my code ive got working so far:
=COUNTIF(B:B,B1)>1 + CONCAT(ArrayFormula(REGEXREPLACE(proper(A1:A),"[^A-Z]+","")),RIGHT("00"&ROW(A:A),2))
Im getting bogged down with how to iterate the number. Im thinking it needs to be an IF function but Im getting into a super nested problem. In my mind it should be something on the lines of:
=IF(COUNTIF(B:B,B1)>1 + CONCAT(ArrayFormula(REGEXREPLACE(proper(A1:A),"[^A-Z]+","")),RIGHT("00"&ROW(A:A),2)),CONCAT(ArrayFormula(REGEXREPLACE(proper(A1:A),"[^A-Z]+","")),RIGHT("00"&ROW(A:A),+1),"")
try:
=ARRAYFORMULA(IF(A1:A="",,
REGEXREPLACE(PROPER(A1:A), "[^A-Z]+", )&
TEXT(COUNTIFS(A1:A, A1:A, ROW(A1:A), "<="&ROW(A1:A)), "00")))
=ARRAYFORMULA(IF(A1:A="",,
REGEXREPLACE(PROPER(A1:A), "[^A-Z]+", )&
TEXT(COUNTIFS(REGEXREPLACE(PROPER(A1:A), "[^A-Z]+", ),
REGEXREPLACE(PROPER(A1:A), "[^A-Z]+", ), ROW(A1:A), "<="&ROW(A1:A)), "00")))

Odd Shortened Syntax for Closures in Swift?

I'm trying to fully wrap my head around closures in Swift. I think I understand the basics. However, I ran into some odd syntax and would appreciate if someone can understand what it means.
I'm going to show the shortened syntax that I came across, but first I'll show how I would write this code without the shortned syntax. I think I understand everything that's going on in the following code, but I'll narrate it just to make sure :)
//function
func manipulate(numbers: [Int], using algorithm: (Int) -> Int){
for number in numbers{
let result = algorithm(number)
print("Manipulating \(number) produced \(result)")
}
}
//trailing closure syntax
manipulate(numbers: [1,2,3]){(number: Int) -> Int in
return number * number
}
Okay, so we're basically declaring a function called manipulate that takes 2 parameters. One of these parameters is numbers and it is an array of Ints. The second parameter, known as using externally and algorithm internally is a closure, which takes an Int as a parameter, and returns an Int.
Okay cool, the function then goes through all the numbers in the array numbers and applies the result of calling the closure, algorithm on each number.
Okay, so that's what the function does, now, let's call it with trailing closure syntax.
What I did was call the function, manipulate and pass in the first parameter, numbers, and then, going by the trailing closure syntax, defined the closure that I'm going to be using. It's taking a parameter, which I called number, that is an Int, and it's returning another Int, number * number.
This code makes perfect sense to me.
Here's the variant that tripped me up. The definition of the function was exactly the same, but, the function was called in a way that I don't understand.
//function
func manipulate(numbers: [Int], using algorithm: (Int) -> Int){
for number in numbers{
let result = algorithm(number)
print("Manipulating \(number) produced \(result)")
}
}
//trailing closure syntax
manipulate(numbers: [1,2,3]){ number in
number * number
}
First of all, we're not specifying the type of the parameter that the closure takes, and we're also not specifying the type of the return value. Also, what does number even mean in this context when we're calling the function?
I'd appreciate it if someone could explain what's going on here. I'm pretty sure it's some idiomatic syntax in Swift, but I don't quite understand it.
Thanks!

Google sheets recursive ROUND for fractional parts

I have a number with fractional parts, but Google sheets ROUND function works only for first number specified in places parameter:
1.48852 => ROUND(1.48852) => 1
1.48852 => ROUND(ROUND(ROUND(ROUND(ROUND(ROUND(1.48852,5),4),3),2),1),0) => 2
Is any function in Google sheets that would work as shown in the second case?
Basically, I want something like ROUND but work recursively.
To answer your question directly, no, I don't believe that there is any function that enables recursive rounding (or recursive anything for that matter).
However, with the same degree of curiosity that you showed in asking this question, I've looked at how I might achieve a recursive result through scripting. This is the result.
function reround(input) {
var num1 = 5;
do {
input = input.toFixed(num1);
input=+input;
num1--;
}while (num1 >-1); // continue running the loop while is greater than -1, i.e. until num1 = 0
return input;
}
This code gets entered as a script and then the function is available for use in the spreadsheet. It only takes one parameter (value) because the number of places is coded into the script. But no doubt, one could modify the code to accept the number of decimal places as a parameter also (I wasn't that curious;).
This is the sequence of values at each stage of the conventional and recursive rounding.
This is the log of values from the REROUND process.
Interesting to note the difference at three decimal places. It's not something that I've explored, so I don't have an explanation.

How can Scala understand function calls in different formats?

I realize the following function calls are all same, but I do not understand why.
val list = List(List(1), List(2, 3), List(4, 5, 6))
list.map(_.length) // res0 = List(1,2,3) result of 1st call
list map(_.length) // res1 = List(1,2,3) result of 2nd call
list map (_.length) // res2 = List(1,2,3) result of 3rd call
I can understand 1st call, which is just a regular function call because map is a member function of class List
But I can not understand 2nd and 3rd call. For example, in the 3rd call, how can Scala compiler know "(_.length)" is parameter of "map"? How can compiler know "map" is a member function of "list"?
The only difference between variant 2 and 3 is the blank in front of the parenthesis? This can only be a delimiter - list a and lista is of course different, but a opening parens is a new token, and you can put a blank or two or three in front - or none. I don't see how you can expect a difference here.
In Java, there is no difference between
System.out.println ("foo");
// and
System.out.println("foo");
too.
This is the operator notation. The reason it works is the same reason why 2 + 2 works.
The space is used to distinguish between words -- listmap(_.length) would make the compiler look for listmap. But if you write list++list, it will work too, as will list ++ list.
So, one you are using operator notation, the space is necessary to separate words, but otherwise may be present or not.

Custom Array Functions in Open Office Calc

Could someone please tell me how to write a custom function in Open Office Basic to be used in Open Office Calc and that returns an array of values. An example of one such built-in function is MINVERSE. I need to write a custom function that populates a range of cells in much the same way.
Help would be much appreciated.
Yay, I just figured it out: all you do is return an array from your macro, BUT you also have to press Ctrl+Shift+Enter when typing in the cell formula to call your function (which is also the case when working with other arrays in calc). Here's an example:
Function MakeArray
Dim ret(2,2)
ret(0,0) = 1
ret(1,0) = 2
ret(0,1) = 3
ret(1,1) = 4
MakeArray = ret
End Function
FWIW, damjan's MakeArray function returns a Variant containing an array, I think. (The type returned by MakeArray is unspecified, so it defaults to Variant. A Variant is a container with a descriptive header, apparently cast as needed by the interpreter.)
Almost, but not quite, the same thing as returning an array. According to http://www.cpearson.com/excel/passingandreturningarrays.htm, Microsoft did not introduce the ability to return an array until 2000. His example [ LoadNumbers(Low As Long, High As Long) As Long()] does not compile in OO, flagging a syntax error on the parens following Long. It appears that OO's Basic emulates the pre-2k VBA.

Resources