Skip to content

Commit

Permalink
Return #NAME? error for undefined variables, Fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
leonelcamara committed Jan 6, 2021
1 parent 792c90c commit a836ecb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# hotxlfp changelog

## 0.0.9

* Fixed bug with undefined variables not returning a #NAME? error

## 0.0.8

* Fix bug with accented charactes in criteria
Expand Down
6 changes: 5 additions & 1 deletion hotxlfp/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,16 @@ def get_variable(self, name):
return self.variables[name]

def call_variable(self, name):
result = {'value': self.variables.get(name)} # get around 2.7 not having nonlocal
not_found = lambda : 0
value = self.variables.get(name, not_found)
result = {'value': value} # get around 2.7 not having nonlocal

def valsetter(new_value):
if new_value is not None:
result['value'] = new_value
self.emit('callVariable', name, valsetter)
if result['value'] is not_found:
raise formulaserror.NAME
return result['value']

def call_cell_value(self, label):
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

setup(
name='hotxlfp',
version='0.0.8',
version='0.0.9',
packages=['hotxlfp', 'hotxlfp._compat', 'hotxlfp._compat.py3', 'hotxlfp.helper', 'hotxlfp.formulas', 'hotxlfp.grammarparser'],
license='MIT',
test_suite='tests',
author='Leonel Câmara',
author_email='[email protected]',
url='https://github.com/aidhound/hotxlfp',
download_url='https://github.com/aidhound/hotxlfp/archive/0.0.8.tar.gz',
download_url='https://github.com/aidhound/hotxlfp/archive/0.0.9.tar.gz',
keywords=['excel', 'formula', 'parser'],
install_requires=['ply', 'python-dateutil'],
long_description='\n'.join(l for l in open('README.md', encoding="utf-8").readlines() if not l.startswith('[!')),
Expand Down
18 changes: 18 additions & 0 deletions tests/test_vars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
import unittest
from hotxlfp import Parser


class TestVars(unittest.TestCase):

def test_undefined(self):
p = Parser()
ret = p.parse("MY_VAR")
self.assertEqual(ret['result'], None)
self.assertEqual(ret['error'], '#NAME?')
ret = p.parse("MY_VAR + 5")
self.assertEqual(ret['result'], None)
self.assertEqual(ret['error'], '#NAME?')
ret = p.parse("5 + MY_VAR")
self.assertEqual(ret['result'], None)
self.assertEqual(ret['error'], '#NAME?')

0 comments on commit a836ecb

Please sign in to comment.