Recently I had a nice challange in Sharepiont 2007.
I had to group a view by a field, but not just group it on the text but on part of the value.
Eg. The field contained a value like this: A chapter\a subchapter.
The view had to be grouped by chapter and secondly by subchapter.This is the simple solution to the problem:
I created 2 new calculated fields in the sharepoint list.
The first field called chapter, the second subchapter.
The chapter field had to contain the part of the title field before the "\". And if there was no "\" in the text,
then it had to be the complete string. This is the code I used to create this field:
=IF(ISNUMBER(FIND("\",Title)),LEFT(Title,FIND("\",Title)-1),Title)
First we check if the "\" is present in the string, if not we just use title as it is. If "\" is present, then we take the left of the "\".
Here are some examples:
A testing code\b subtest 2 --> becomes: A testing code
A testing code --> becomes: A testing code
The second field had to contain the part of the string after the "\" and in case there was no "\" it had to be empty. Here the code I used:
=IF(ISNUMBER(FIND("\",Title)),RIGHT(Title,LEN(Title)-FIND("\",Title)),"")
First we check if the "\" is present in the string, if not we return "". If "\" is present, then we take the right of the "\".
Here are some examples:
A testing code\b subtest 2 --> becomes: b subtest 2
A testing code --> becomes:
Then I grouped the view on these 2 fields.
Since I'm a programmer myself, I know that examples are the best way to learn a code. So here's one more.
This time I had to get some values out of a text field called Soort with this format: text1\text2\text3 .
I had to show text2 and text3 in different columns. The first calculated field called Project contains the string text2.
The code to get text2 out of the string text1\text2\text3 is the following:
=LEFT(RIGHT(Soort,LEN(Soort)-FIND("\",Soort)),FIND("\",RIGHT(Soort,LEN(Soort)-FIND("\",Soort)))-1)
So this code return: text2. The next calculated field called Subproject had to contain the string that is in place of text3.
Since Sharepoint code does not have the possibility to split or use loops we had to come up with the following code:
=RIGHT(RIGHT(Soort,LEN(Soort) - FIND("\",Soort)),LEN(RIGHT(Soort,LEN(Soort)-FIND("\",Soort)))
- FIND("\",(RIGHT(Soort,LEN(Soort) - FIND("\",Soort)))))
I have not reviewed these functions, I just know that they work.
Maybe it is possible to get the results in an easier way. Feel free to share it with us.