It is pretty easy to type text onto a Visio shape. And linking text to shape data fields takes just a quick visit to Insert > Field.
But did you know that you can use Visio shape text as a data source? That you can evaluate it as a meaningful number or useful quantity?
Today, we’ll show you a few tricks that will allow you to use shape text as quantifiable data that you can take action on.
No “Text” Cell
If you scour the ShapeSheet, you won’t find any “Text” cell that magically holds the text of the shape. Sure, you’ll find ShapeSheet sections for Text Fields, Character, Paragraph, Tabs and Text Block Format, but you won’t find any Text cell in any of them.
While there are good reasons for this, I think this can initially trip-up would-be SmartShape designers.
The reason there is no text cell is because text can have different formatting characteristics. Bold, italic, underline, strike-through, bullets, font size, etc. Paragraphs can have different alignment. Some text might be typed by the user, other bits of text might be linked to inserted fields. In short, a shape’s text can be an unpredictable mish-mash. In light of all of this text-complexity, simply asking Microsoft for a Text cell might be a rather unclear feature request.
Workaround #1 to this problem is to insert a field that is linked to a Shape Data cell or a User-defined ShapeSheet cell. This allows you to control what is displayed in a shape’s text via data and logic stored in the shape. But it doesn’t solve the problem of utilizing text that the user types into a shape.
Make Sense of User-typed Text
We can’t directly modify the text a user types into a Visio shape using only the ShapeSheet. And if we have inserted fields that do smart things, the user is likely to destroy these fields by typing (unless we lock down the text and restrict any editing)
But we can respond to that text by using a special function: EVALTEXT!
With EVALTEXT, we can evaluate user-typed text as a number or a number-unit pair.
The syntax for EVALTEXT looks like this:
= EVALTEXT(TheText) = EVALTEXT(Sheet.23!TheText)
TheText is a special token that tells Visio to either look at the text that is typed into the shape that contains the formula, or if we are using the “bang” notation, to look at another shape’s text. In this example, “Sheet.23!” is an identifier to some other shape on the page.
We are likely to enter this formula in a User-defined cell to do some sort of calculation, or to store the value for use with other cell’s calculations. You might end up with expressions that look like this in your ShapeSheet:
User.myTextValue = EVALTEXT(TheText)
User.myTextValue = EVALTEXT(Shape.24!TheText)
EVALTEXT will look at the entire text block and try to make sense of it. If the text is an expression that Visio understands as a valid mathematical or ShapeSheet expression, you will get a nice value that you can put to good use.
For example, say a user typed the following text into a shape:
0*1000 + 73
our function would look like–and evaluate to–this:
EVALTEXT(TheText) = 73
It is interesting to note that EVALTEXT cannot take a string as an argument. So EVALTEXT(“1 + 2”) will not give you anything useful as a result.
You might see that this capability will allow us to quickly build a “Scientific Calculator Shape”, or a “Visio Expression Tester Shape”. And that’s just what we’ll do to further illustrate the power of this function, and cover a few other SmartShape related techniques as well!
Let’s Build a Visio ShapeSheet Calculator Shape!
We’re now going to build a Visio SmartShape that works like a free-form calculator. Whatever text you type into the shape will be evaluated by our special ShapeSheet function.
Build the Calculator Parts
We’ll want to build a shape that has two rectangles, grouped together. The top rectangle will take our text, and the bottom rectangle will show the results:
click to see larger image
Here, I’ve drawn the two rectangles and grouped them together. You can see how the top text is evaluated in the bottom rectangle. Also note that I’ve found the “Sheet IDs” for each shape: the top block, the bottom block and the group itself. These will come in handy for building proper ShapeSheet formulas.
To find out how to discover a shape’s ID, read the article SmartShape Tutorial: Fading Trees. You’ll find the ID-related stuff in the Sheet ID section.
Insert the Evaluation Formula
Now that you know the id of the top rectangle, sub-select the bottom rectangle and hit F2 to get into text-editing mode. Then, go to the menu: Insert > Field:
click to view larger image
You’ll see a dialog like this. Select Custom Field in the left list, then type a formula like: EVALTEXT(Sheet.2!TheText) into the Custom formula field, as shown. This tells the bottom rectangle to display the evaluation of the top rectangle’s text. In your case, Sheet.2 might have a different id, so make sure you get the right identification number!
Make the Top Rectangle Accept Text
Our calculator is just about ready to use, but there is still one detail to take care of.
If we select our grouped shape and start typing, the text goes to the wrong shape: it goes to the group!
We really want the text to go to the top rectangle, so we have to turn off the group’s text.
This is easy enough to do. Just go to Format > Behavior, then notice the Group behavior area. There, we can un-check Edit text of group. This easily turns off text-input at the group level. Now our text will go to the top-most sub-shape in the group, instead of the group itself:
click to view larger image
Now, I just mentioned the “top-most sub-shape”. What I really was talking about is the sub-shape that is foremost in the back-to-front sense. If the bottom rectangle is the top-most in the Z-order stack, we will destroy our inserted EVALTEXT formula. So we need to bring the top rectangle “to front”, ASAP!
Just sub-select the top rectangle and bring it to front via Shape > Order > Bring to Front, or simply Ctrl + F.
At this point, your calculator should work just fine. Select the group and type in an expression. The top rectangle should display what you’ve typed, and the bottom rectangle should evaluate it, as you type!
A Powerful Calculator!
Not only can you quickly input mathematical expressions, like 3 * 4 and get 12 back right away, but you can also enter complicated formulas that use functions that are understood by the ShapeSheet.
Here we’ve added a bit of trigonometry to the mix:
And since EVALTEXT is really performing a Visio ShapeSheet evaluation, you can even refer to other shapes and concatenate text.
There are lots of ShapeSheet functions that you can use: Ln(), Sin(), Cos(), Atan2(), Min(), Max(), etc. You can specify units like deg, rad, in, cm, mm, m and so-on. Just play around and see what you can discover!
Our next example gets tricky with some string concatenation.
Below, Sheet.4 is our group, and we are referring to it’s width by using the syntax “Sheet.4!Width. We then pre-pend the text “Length = ” to the expression and our calculator handles it just fine! Also, Visio knows that we are talking about a length, not just a number: see how it includes the inches tick-marks just after 1.75?
Note: Visio tries to automatically replace simple-quotes with “pretty quotes”–the ones that look like 66 99. These are not properly evaluated by EVALTEXT. So you’ll have to quickly type undo after you type each quote mark to get it to be the simpler version, or find the auto-replace options and turn off quote marks.
Visio does some interesting type-conversions. I’m not sure if I agree with this one:
And you can mix and match decimals, plus, minus, and division using the forward-slash character:
Here, Visio (or Office?) tried to ruin things again by inserting a long-dash for a minus sign. I had to undo after typing the minus sign, because EVALTEXT didn’t take the long dash as a mathematical operator.
The calculator shape is a very old idea that actually worked much better in older versions of Visio that didn’t have the auto replace feature!
Before you rush off and create your own calculator, I’ll throw out two more tips that will help you to finely-tune and polish your calculator SmartShape.
If you’ve been watching carefully, you will have noticed the vertical “=” sign in the shape I’ve shown here. Clearly it is the top-most sub-shape in the group, which should interfere with the text input. Ie: the equals sign should be intercepting the text typed into the shape–which make things rather ugly.
The way to avoid this is to lock text editing on any “foreground” shapes that have nothing to do with text. You do this via the Format > Protection menu:
Now you can easily add foreground elements to your shape to make it prettier without having to worry about text messing things up. You’ll see that I’ve added some black and white lines to add a bit of 3D effect to the shape. These are also “front-most”, but have locked text so they don’t affect text-input to the calculator.
Stop Sub-selection by Users
Once your calculator is built, users won’t have a need to sub-select anything. It makes the shape feel more solid and well-built if you can prevent accidental sub-selection of the group’s members. It also avoids potential confusion for users who aren’t so sure if they’ve sub-selected a shape.
You can make a group feel like a single entity by going again to the Format > Behavior dialog, and selecting Group only from the Selection drop-down list:
Now your group will behave like a single, sealed shape and users won’t be confused by accidental sub-selections!