1.0.8 • Published 8 months ago

excel-formula-utilities v1.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

excel-formula-utilities

This project is a port of the excel-formula library to ES6.
It contains a set of functions that can be used to pretty print Excel formulas and convert them into JavaScript, C# or Python code.

Key Differences from excel-formula:

  • Removed external dependencies (Bootstrap, jQuery)
  • 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

Install

npm install excel-formula-utilities

Usage

Module bundler

import { formatFormula } from 'excel-formula-utilities'

const formattedFormula = formatFormula('SUM(A1:A2)')

Browser

<script src="https://unpkg.com/excel-formula-utilities"></script>
<script>
  const formattedFormula = ExcelFormulaUtilities.formatFormula('SUM(A1:A2)')
</script>

Available methods

formatFormula

Formats an excel formula.

Signature:
formatFormula(formula: string, options): string

  • formula - The excel formula to format
  • options - An optional object with the following properties:
NameDescriptionDefault
tmplFunctionStartTemplate for the start of a function, the {{token}} will contain the name of the function.'{{autoindent}}{{token}}(\n'
tmplFunctionStopTemplate for when the end of a function has been reached.'\n{{autoindent}}{{token}})'
tmplOperandErrorTemplate for errors.' {{token}}'
tmplOperandRangeTemplate for ranges and variable names.'{{autoindent}}{{token}}'
tmplLogicalTemplate for logical operators'{{token}}{{autolinebreak}}'
tmplOperandLogicalTemplate for logical operators such as + - = ...'{{autoindent}}{{token}}'
tmplOperandNumberTemplate for numbers.'{{autoindent}}{{token}}'
tmplOperandTextTemplate for text/strings.'{{autoindent}}"{{token}}"'
tmplArgumentTemplate for argument separators such as ,.'{{token}}\n'
tmplOperandOperatorInfix-' {{token}}{{autolinebreak}}'
tmplFunctionStartArrayTemplate for the start of an array.''
tmplFunctionStartArrayRowTemplate for the start of an array row.'{'
tmplFunctionStopArrayRowTemplate for the end of an array row.'}'
tmplFunctionStopArrayTemplate for the end of an array.''
tmplSubexpressionStartTemplate for the sub expression start.'{{autoindent}}(\n'
tmplSubexpressionStopTemplate for the sub expression stop.'\n)'
tmplIndentTabTemplate for the tab char.'\t'
tmplIndentSpaceTemplate for space char.' '
autoLineBreakWhen rendering line breaks automatically which types should it break on.'TOK_TYPE_FUNCTION \| TOK_TYPE_ARGUMENT \| TOK_SUBTYPE_LOGICAL \| TOK_TYPE_OP_IN'
newLineUsed for the {{autolinebreak}} replacement as well as some string parsing.'\n'
trimTrim the output.true
customTokenRenderThis is a call back to a custom token function.null
prefixAdd a prefix to the formula.''
postfixAdd a suffix to the formula.''
isEuIf truethen ; is treated as list separator, if false then ; is treated as array row separatorfalse

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

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 }
}

formatFormulaHTML

Formats an excel formula into HTML.

Signature:
formatFormulaHTML(formula: string, options): string

  • formula - The excel formula to format
  • options - An optional object with the following properties (inherits defaults from formatFormula):
NameDescriptionDefault
tmplFunctionStartTemplate for the start of a function, the {{token}} will contain the name of the function.'{{autoindent}}<span class="function">{{token}}</span><span class="function_start">(</span><br />'
tmplFunctionStopTemplate for when the end of a function has been reached.'<br />{{autoindent}}{{token}}<span class="function_stop">)</span>'
tmplOperandErrorTemplate for errors.' {{token}}'
tmplOperandRangeTemplate for ranges and variable names.'{{autoindent}}{{token}}'
tmplLogicalTemplate for logical operators'{{token}}{{autolinebreak}}'
tmplOperandLogicalTemplate for logical operators such as + - = ...'{{autoindent}}{{token}}'
tmplOperandNumberTemplate for numbers.'{{autoindent}}{{token}}'
tmplOperandTextTemplate for text/strings.'{{autoindent}}<span class="quote_mark">"</span><span class="text">{{token}}</span><span class="quote_mark">"</span>'
tmplArgumentTemplate for argument separators such as ,.'{{token}}<br />'
tmplOperandOperatorInfix-' {{token}}{{autolinebreak}}'
tmplFunctionStartArrayTemplate for the start of an array.''
tmplFunctionStartArrayRowTemplate for the start of an array row.'{'
tmplFunctionStopArrayRowTemplate for the end of an array row.'}'
tmplFunctionStopArrayTemplate for the end of an array.''
tmplSubexpressionStartTemplate for the sub expression start.'{{autoindent}}('
tmplSubexpressionStopTemplate for the sub expression stop.' )'
tmplIndentTabTemplate for the tab char.'<span class="tabbed">&nbsp;&nbsp;&nbsp;&nbsp;</span>'
tmplIndentSpaceTemplate for space char.'&nbsp;'
autoLineBreakWhen rendering line breaks automatically which types should it break on.'TOK_TYPE_FUNCTION \| TOK_TYPE_ARGUMENT \| TOK_SUBTYPE_LOGICAL \| TOK_TYPE_OP_IN '
newLineUsed for the {{autolinebreak}} replacement as well as some string parsing.'<br />'
trimTrim the output.true
customTokenRenderThis is a call back to a custom token function.Custom function for formatFormulaHTML
prefixAdd a prefix to the formula.'='
postfixAdd a suffix to the formula.''

formula2CSharp

Converts an excel formula into C# code.

Signature:
formula2CSharp(formula: string, options): string

  • formula - The excel formula to format
  • options - An optional object with the following properties (inherits defaults from formatFormula):
NameDescriptionDefault
tmplFunctionStartTemplate for the start of a function, the {{token}} will contain the name of the function.'{{token}}('
tmplFunctionStopTemplate for when the end of a function has been reached.'{{token}})'
tmplOperandErrorTemplate for errors.'{{token}}'
tmplOperandRangeTemplate for ranges and variable names.'{{token}}'
tmplOperandLogicalTemplate for logical operators such as + - = ...'{{token}}'
tmplOperandNumberTemplate for numbers.'{{token}}'
tmplOperandTextTemplate for text/strings.'"{{token}}"'
tmplArgumentTemplate for argument separators such as ,.'{{token}}'
tmplOperandOperatorInfix-'{{token}}'
tmplFunctionStartArrayTemplate for the start of an array.''
tmplFunctionStartArrayRowTemplate for the start of an array row.'{'
tmplFunctionStopArrayRowTemplate for the end of an array row.'}'
tmplFunctionStopArrayTemplate for the end of an array.''
tmplSubexpressionStartTemplate for the sub expression start.'('
tmplSubexpressionStopTemplate for the sub expression stop.')'
tmplIndentTabTemplate for the tab char.'\t'
tmplIndentSpaceTemplate for space char.' '
autoLineBreakWhen rendering line breaks automatically which types should it break on.'TOK_SUBTYPE_STOP \| TOK_SUBTYPE_START \| TOK_TYPE_ARGUMENT'
trimTrim the output.true
customTokenRenderThis is a call back to a custom token function.Custom function for formula2CSharp

formula2JavaScript

Signature: formula2JavaScript(formula: string, options): string

  • formula - The excel formula to format
  • options - An optional object with the following properties (inherits options from formula2CSharp):

formula2Python

Signature: formula2Python(formula: string, options): string

  • formula - The excel formula to format
  • options - An optional object with the following properties (inherits defaults from formatFormula):
NameDescriptionDefault
tmplFunctionStartTemplate for the start of a function, the {{token}} will contain the name of the function.'{{token}}('
tmplFunctionStopTemplate for when the end of a function has been reached.'{{token}})'
tmplOperandErrorTemplate for errors.'{{token}}'
tmplOperandRangeTemplate for ranges and variable names.'{{token}}'
tmplOperandLogicalTemplate for logical operators such as + - = ...'{{token}}'
tmplOperandNumberTemplate for numbers.'{{token}}'
tmplOperandTextTemplate for text/strings.'"{{token}}"'
tmplArgumentTemplate for argument separators such as ,.'{{token}}'
tmplOperandOperatorInfix-'{{token}}'
tmplFunctionStartArrayTemplate for the start of an array.''
tmplFunctionStartArrayRowTemplate for the start of an array row.'{'
tmplFunctionStopArrayRowTemplate for the end of an array row.'}'
tmplFunctionStopArrayTemplate for the end of an array.''
tmplSubexpressionStartTemplate for the sub expression start.'('
tmplSubexpressionStopTemplate for the sub expression stop.')'
tmplIndentTabTemplate for the tab char.'\t'
tmplIndentSpaceTemplate for space char.' '
autoLineBreakWhen rendering line breaks automatically which types should it break on.'TOK_SUBTYPE_STOP \| TOK_SUBTYPE_START \| TOK_TYPE_ARGUMENT'
trimTrim the output.true
customTokenRenderThis is a call back to a custom token function.Custom function for formula2CSharp

getTokens

Tokenizes an excel formula.

Signature: getTokens(formula: string isEu: boolean): F_token[]

  • formula - The excel formula to format
  • isEu - 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:

[
  {
    "subtype": "range",
    "type": "operand",
    "value": "A1"
  },
  {
    "subtype": "math",
    "type": "operator-infix",
    "value": "+"
  },
  {
    "subtype": "number",
    "type": "operand",
    "value": "1000"
  }
]
1.0.8

8 months ago

1.0.7

9 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago