Pretty print Excel formulas.
npm install @pequity/format-formulaThis project is a partial port of the excel-formula library to ES6.
It contains a set of functions that can be used to pretty print Excel formulas.
Key Differences from excel-formula:
- Removed external dependencies (Bootstrap, jQuery)
- Removed methods not related to formatting
- Removed jQuery methods and replaced them with ES6 equivalents
- Modularized the library with support for tree-shaking
- Does not expose a global (window) variable
- Added isEu as an option to the getTokens, formatFormula and formatFormulaHTML methods
- Provides ES, CJS, and UMD module formats
``bash`
npm install @pequity/format-formula
`javascript
import { formatFormula } from '@pequity/format-formula';
const formattedFormula = formatFormula('SUM(A1:A2)');
`
`html`
Formats an excel formula.
Signature:
formatFormula(formula: string, options): string
- formula - The excel formula to formatoptions
- - An optional object with the following properties:
| Name | Description | Default |
| ------------------------- | ----------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| tmplFunctionStart | Template for the start of a function, the {{token}} will contain the name of the function. | '{{autoindent}}{{token}}(\n' |'\n{{autoindent}}{{token}})'
| tmplFunctionStop | Template for when the end of a function has been reached. | |' {{token}}'
| tmplOperandError | Template for errors. | |'{{autoindent}}{{token}}'
| tmplOperandRange | Template for ranges and variable names. | |'{{token}}{{autolinebreak}}'
| tmplLogical | Template for logical operators | |+ - = ...
| tmplOperandLogical | Template for logical operators such as | '{{autoindent}}{{token}}' |'{{autoindent}}{{token}}'
| tmplOperandNumber | Template for numbers. | |'{{autoindent}}"{{token}}"'
| tmplOperandText | Template for text/strings. | |,.
| tmplArgument | Template for argument separators such as | '{{token}}\n' |' {{token}}{{autolinebreak}}'
| tmplOperandOperatorInfix | - | |''
| tmplFunctionStartArray | Template for the start of an array. | |'{'
| tmplFunctionStartArrayRow | Template for the start of an array row. | |'}'
| tmplFunctionStopArrayRow | Template for the end of an array row. | |''
| tmplFunctionStopArray | Template for the end of an array. | |'{{autoindent}}(\n'
| tmplSubexpressionStart | Template for the sub expression start. | |'\n)'
| tmplSubexpressionStop | Template for the sub expression stop. | |'\t'
| tmplIndentTab | Template for the tab char. | |' '
| tmplIndentSpace | Template for space char. | |'TOK_TYPE_FUNCTION \| TOK_TYPE_ARGUMENT \| TOK_SUBTYPE_LOGICAL \| TOK_TYPE_OP_IN'
| autoLineBreak | When rendering line breaks automatically which types should it break on. | |{{autolinebreak}}
| newLine | Used for the replacement as well as some string parsing. | '\n' |true
| trim | Trim the output. | |null
| customTokenRender | This is a call back to a custom token function. | |''
| prefix | Add a prefix to the formula. | |''
| postfix | Add a suffix to the formula. | |true
| isEu | If then ; is treated as list separator, if false then ; is treated as array row separator | false |
Template Values
- {{autoindent}} - apply auto indent based on current tree level{{token}}
- - the named token such as FUNCTION_NAME or "string"{{autolinebreak}}
- - apply line break automatically. tests for next element only at this point
customTokenRender Example
`javascript
function (tokenString, token, indent, lineBreak) {
const outStr = token
const useTemplate = true
// In the return object "useTemplate" tells formatFormula()
// weather or not to apply the template to what your return from the "tokenString".
return { tokenString: outStr, useTemplate }
}
`
Formats an excel formula into HTML.
Signature:
formatFormulaHTML(formula: string, options): string
- formula - The excel formula to formatoptions
- - An optional object with the following properties (inherits defaults from formatFormula):
| Name | Description | Default |
| ------------------------- | -------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
| tmplFunctionStart | Template for the start of a function, the {{token}} will contain the name of the function. | '{{autoindent}}{{token}}(
' |'
| tmplFunctionStop | Template for when the end of a function has been reached. |
{{autoindent}}{{token}})' |' {{token}}'
| tmplOperandError | Template for errors. | |'{{autoindent}}{{token}}'
| tmplOperandRange | Template for ranges and variable names. | |'{{token}}{{autolinebreak}}'
| tmplLogical | Template for logical operators | |+ - = ...
| tmplOperandLogical | Template for logical operators such as | '{{autoindent}}{{token}}' |'{{autoindent}}{{token}}'
| tmplOperandNumber | Template for numbers. | |'{{autoindent}}"{{token}}"'
| tmplOperandText | Template for text/strings. | |,.
| tmplArgument | Template for argument separators such as | '{{token}}
' |' {{token}}{{autolinebreak}}'
| tmplOperandOperatorInfix | - | |''
| tmplFunctionStartArray | Template for the start of an array. | |'{'
| tmplFunctionStartArrayRow | Template for the start of an array row. | |'}'
| tmplFunctionStopArrayRow | Template for the end of an array row. | |''
| tmplFunctionStopArray | Template for the end of an array. | |'{{autoindent}}('
| tmplSubexpressionStart | Template for the sub expression start. | |' )'
| tmplSubexpressionStop | Template for the sub expression stop. | |' '
| tmplIndentTab | Template for the tab char. | |' '
| tmplIndentSpace | Template for space char. | |'TOK_TYPE_FUNCTION \| TOK_TYPE_ARGUMENT \| TOK_SUBTYPE_LOGICAL \| TOK_TYPE_OP_IN '
| autoLineBreak | When rendering line breaks automatically which types should it break on. | |{{autolinebreak}}
| newLine | Used for the replacement as well as some string parsing. | '
' |true
| trim | Trim the output. | |'='
| customTokenRender | This is a call back to a custom token function. | Custom function for formatFormulaHTML |
| prefix | Add a prefix to the formula. | |''
| postfix | Add a suffix to the formula. | |
Tokenizes an excel formula.
Signature:
getTokens(formula: string isEu: boolean): F_token[]
- formula - The excel formula to formatisEu
- - If truethen ; is treated as list separator, if false then ; is treated as array row separator
Returns an array of tokens, e.g. given the formula A1+1000 the output would be:
`json``
[
{
"subtype": "range",
"type": "operand",
"value": "A1"
},
{
"subtype": "math",
"type": "operator-infix",
"value": "+"
},
{
"subtype": "number",
"type": "operand",
"value": "1000"
}
]