Description
5g functions are for others to use. This requires extra work. To minimize the extra work we offer a template that helps ensure we complete all 5g requirements and, in some places, provides the code for those requirements.
Learning Objectives
At the end of this course, learners will know:
What the various 5g function source sections are
And how they are organized
Prerequisites
Microsoft's free add-in: Advanced Formula Environment (AFE)
Discussion
Farther down this page is a template for creating 5g functions. The various sections are numbered. We will review what each section does at a high level and why it is in the position that it is in. In later classes we will explain each section in detail.
Before we get into what each section does, note the colors. These are the colors that Microsoft's free add-in, Advanced Formula Environment (AFE), adds to source.
Purple is for the LAMBDA's name.
Green is for comments. NOTE! AFE's module editor is the only editor that allows all comments. There are three types of comments.
// designates a single line comment. Everything to the right of the double slash is treated as a comment until the first new line (paragraph).
/* designates a multi-line comment. Everything between a slash asterisk to an asterisk slash */ are treated as a comment.
/** designates a multi-line comment that will be added to Name Manager's comment box. Everything between a slash double asterisk to an asterisk slash */ is treated as a comment. There can only be one of this type of comment per named LAMBDA.
Red is for literal text and strings.
Blue is for identifies such as named formulas within the LAMBDA.
Dark Blue is for reserved symbols (operators, commas, brackets. etc.) and keywords, like Excel's function names.
Comments are only available in AFE's modules tab.
The Template
Below is BXL's 5g template. It can be downloaded using AFE from this URL:
https://gist.github.com/CHatmaker/3e6cba6414fadfabfa3725255da2ba74
NOTE! The circled numbers are NOT part of the code. There are added to mark sections for discussion.
Documentation Block - This section is where we can add a comment that appears in the Name Manager. We also add our revision log. The revision log also serves as a version identifier.
Change:
<enter function's name here with λ> to your function's name. Place a lambda character (λ) at the end to help differentiate custom functions from Excel's functions. Do not include the angled brackets (< >).
<enter a brief description of the function's purpose> to a sentence or two explaining what this function does.
<Mmm dd yyyy> to the date this was edited..
<Your Name> Your name> to your name
<What changed> to a brief description of what was done. If this is when the function was created, we suggest either a copyright notice or "original development".
Function Declaration - Here is where we declare our function's name.
Change:
FunctionNameλ to your function's name. Use the lambda symbol (λ) at the end of the function name to draw attention to the fact this is a user defined function. Excel's IntelliSense will make it so no one has to enter that symbol when using our function.
Argument Declarations - All function arguments must be declared here. Argument names must be concise and meaningful to strangers.
All arguments must be enclosed in square brackets to enable inline help to function. To Excel, this makes function arguments optional which allows users to enter the function name without any arguments. When the required arguments are missing, our function can use that to trigger the inline help.
Change:
[ArgumentX> to your function's arguments. Enclose all arguments in square brackets here.
Omitted Required Arguments Test - With all arguments declared as optional, we must test for omitted required arguments, and if any required arguments are missing, we must display inline help. In this example Argument1 and Argument2 are required.
Change:
[ArgumentX] to your function's required arguments. Add all required arguments.
Inline Help - All Excel functions have inline help. At this time, Microsoft has no facility for adding hypertext pop-up inline help so we came up with a way to display inline help in Excel's cells using the TEXTSPLIT() function. To control TEXTSPLIT's behavior when text contains commas, we replace TEXTSPLIT's default delimiters with → for columns, and ¶ for rows.
Change:
<FunctionNameλ> to your function's name.
ArgumentX to your function's arguments. Be sure to enclose optional arguments in square brackets here.
<Copy the functions description to here> to the description in the documentation block.
<enter function's webpage URL here (if any)> to a webpage dedicated to explaining this function (if any exists).
<Author's initials> to your initials or your company's name
Mmm dd yyyy to the date this was edited..
<argument description> to what is appropriate to describe each argument.
<Default value> to each optional argument's default value
Defaults and Named Constants - In this example, Argument3 is optional.
Change:
"default value" to whatever is required for your default value. Any optional arguments must be provided defaults and If we find ourselves adding hard coded numbers to our formulas, like 12 for 12 months, we urge creators to name those values in this section and use the name, rather than the value, in our formulas.
Procedure - This is where we put the formulas that provide our function's functionality. Change LET step names and "enter formulas starting here" to your LET step names and formulas. All LET steps must have clear and concise names. Use mnemonics for LET step names with LET step names would otherwise be more than about 10 characters..
Change:
LET Step name to clear and concise names for each formula
"enter formulas here" to your function's formulas.
"enter last formula here" to your function's last formula
Function Result - At the end of the procedure, LET allows us to have a formula as its last step; however, to make debugging functions easier, 5g requires the last LET step to be the name of the last formula. This allows us, when debugging, to change this last name to any of the other LET step names to see what they produce.
Using this template reminds us what we should include and where things go. It also helps to ensure users know how to use our function and which version they are using. In addition, the structure makes it easier for us, or anyone else, to maintain our code.