Skip to content

Commit

Permalink
Added support for Excel formula (#2219)
Browse files Browse the repository at this point in the history
This adds support for Excel's formula terms.
  • Loading branch information
RunDevelopment authored Mar 10, 2020
1 parent 1e3070a commit bf4f7bf
Show file tree
Hide file tree
Showing 17 changed files with 422 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@
"title": "Erlang",
"owner": "Golmote"
},
"excel-formula": {
"title": "Excel Formula",
"alias": ["xlsx", "xls"],
"owner": "RunDevelopment"
},
"fsharp": {
"title": "F#",
"require": "clike",
Expand Down
66 changes: 66 additions & 0 deletions components/prism-excel-formula.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
Prism.languages['excel-formula'] = {
'comment': {
pattern: /(\bN\(\s*)"(?:[^"]|"")*"(?=\s*\))/i,
lookbehind: true,
greedy: true
},
'string': {
pattern: /"(?:[^"]|"")*"(?!")/,
greedy: true
},
'reference': {
// https://www.ablebits.com/office-addins-blog/2015/12/08/excel-reference-another-sheet-workbook/

// Sales!B2
// 'Winter sales'!B2
// [Sales.xlsx]Jan!B2:B5
// D:\Reports\[Sales.xlsx]Jan!B2:B5
// '[Sales.xlsx]Jan sales'!B2:B5
// 'D:\Reports\[Sales.xlsx]Jan sales'!B2:B5

pattern: /(?:'[^']*'|(?:[^\s()[\]{}<>*?"';,$&]*\[[^^\s()[\]{}<>*?"']+\])?\w+)!/,
greedy: true,
alias: 'string',
inside: {
'operator': /!$/,
'punctuation': /'/,
'sheet': {
pattern: /[^[\]]+$/,
alias: 'function'
},
'file': {
pattern: /\[[^[\]]+\]$/,
inside: {
'punctuation': /[[\]]/
}
},
'path': /[\s\S]+/
}
},
'function-name': {
pattern: /[A-Z]\w*(?=\()/i,
alias: 'keyword'
},
'range': {
pattern: /\$?(?:[A-Z]+\$?\d+:\$?[A-Z]+\$?\d+|[A-Z]+:\$?[A-Z]+|\d+:\$?\d+)/i,
alias: 'property',
inside: {
'operator': /:/,
'cell': /\$?[A-Z]+\$?\d+/i,
'column': /\$?[A-Z]+/i,
'row': /\$?\d+/
}
},
'cell': {
// Excel is case insensitive, so the string "foo1" could be either a variable or a cell.
// To combat this, we match cells case insensitive, if the contain at least one "$", and case sensitive otherwise.
pattern: /[A-Z]+\d+|\$[A-Za-z]+\$?\d+|[A-Za-z]+\$\d+/,
alias: 'property'
},
'number': /(?:\b\d+(?:\.\d+)?|\B\.\d+)(?:e[+-]?\d+)?\b/i,
'boolean': /\b(?:TRUE|FALSE)\b/i,
'operator': /[-+*/^%=&,]|<[=>]?|>=?/,
'punctuation': /[[\]();{}|]/
};

Prism.languages['xlsx'] = Prism.languages['xls'] = Prism.languages['excel-formula'];
1 change: 1 addition & 0 deletions components/prism-excel-formula.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions examples/prism-excel-formula.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h2>Full example</h2>
<pre><code>=SUM(G7*9)
=INT(RAND()*999)
=AVERAGE(A4:A13)+N("Average user rating")
=CONCATENATE(F4, ",", " ", G4," ",H4)
=IF($A4>500, $A4, 0)
=AND($B4>=501,$C4&lt;=500)
=SUBTOTAL(103,staff[Name])
=TRIMMEAN(staff[Salary],10%)
=SUM([Sales.xlsx]Jan!B2:B5)</code></pre>
2 changes: 2 additions & 0 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@
"jinja2": "django",
"dns-zone": "dns-zone-file",
"dockerfile": "docker",
"xlsx": "excel-formula",
"xls": "excel-formula",
"gamemakerlanguage": "gml",
"hs": "haskell",
"tex": "latex",
Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
"ejs": "EJS",
"etlua": "Embedded Lua templating",
"erb": "ERB",
"excel-formula": "Excel Formula",
"xlsx": "Excel Formula",
"xls": "Excel Formula",
"fsharp": "F#",
"firestore-security-rules": "Firestore security rules",
"ftl": "FreeMarker Template Language",
Expand Down
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions tests/languages/excel-formula/cell_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
B1
BBB1111

$B2
B$2
$B$2

$b2
b$2
$b$2

----------------------------------------------------

[
["cell", "B1"],
["cell", "BBB1111"],
["cell", "$B2"],
["cell", "B$2"],
["cell", "$B$2"],
["cell", "$b2"],
["cell", "b$2"],
["cell", "$b$2"]
]

----------------------------------------------------

Checks for cells.
17 changes: 17 additions & 0 deletions tests/languages/excel-formula/function_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
SUM()
FOO()

----------------------------------------------------

[
["function-name", "SUM"],
["punctuation", "("],
["punctuation", ")"],
["function-name", "FOO"],
["punctuation", "("],
["punctuation", ")"]
]

----------------------------------------------------

Checks for functions.
Loading

0 comments on commit bf4f7bf

Please sign in to comment.