def test_set_value(self): # Clone object data = self.graph.asdict() graph = Spreadsheet.from_dict(data) # Set value and check result in clone. graph.set_value('INPUT', 2025) self.assertTrue(graph.evaluate('INPUT') == 2025) self.assertTrue(graph.evaluate('Sheet1!A1') == 2025) # self.assertTrue(graph.evaluate('RESULT') == 2211) # Check original not changed. graph = self.graph self.assertTrue(graph.evaluate('INPUT') == 1) self.assertTrue(graph.evaluate('Sheet1!A1') == 1) self.assertTrue(graph.evaluate('RESULT') == 187)
def test_set_value(self): # Clone object self.graph.dump_json("dump.txt.gz") graph = Spreadsheet.load_json("dump.txt.gz") # Set value and check result in clone. graph.set_value('INPUT', 2025) self.assertTrue(graph.evaluate('INPUT') == 2025) self.assertTrue(graph.evaluate('Sheet1!A1') == 2025) # self.assertTrue(graph.evaluate('RESULT') == 2211) # Check original not changed. graph = self.graph self.assertTrue(graph.evaluate('INPUT') == 1) self.assertTrue(graph.evaluate('Sheet1!A1') == 1) self.assertTrue(graph.evaluate('RESULT') == 187)
def test_set_value(self): # Clone object self.graph.dump("dump.txt.gz") graph = Spreadsheet.load("dump.txt.gz") # Set value and check result in clone. graph.set_value('INPUT', 2025) self.assertTrue(graph.evaluate('INPUT') == 2025) self.assertTrue(graph.evaluate('Sheet1!A1') == 2025) # self.assertTrue(graph.evaluate('RESULT') == 2211) # Check original not changed. graph = self.graph self.assertTrue(graph.evaluate('INPUT') == 1) self.assertTrue(graph.evaluate('Sheet1!A1') == 1) self.assertTrue(graph.evaluate('RESULT') == 187)
def test_as_from_dict(self): file_name = os.path.abspath("./tests/excel/VDB.xlsx") sp1 = Spreadsheet(file_name) sp1.cell_set_value('Sheet1!B7', value=100) sp1.cell_set_value('Sheet1!B8', value=200) # serialize to a dict, then back to a clone instance data = sp1.asdict() sp2 = Spreadsheet.from_dict(data) # set values in the new spreadsheet - should be different than before sp2.cell_set_value('Sheet1!B7', value=500) sp2.cell_set_value('Sheet1!B8', value=600) self.assertNotEqual(sp1.cell_evaluate('Sheet1!B7'), sp2.cell_evaluate('Sheet1!B7')) self.assertNotEqual(sp1.cell_evaluate('Sheet1!B8'), sp2.cell_evaluate('Sheet1!B8'))
def test_create_evaluate_update(self): spreadsheet = Spreadsheet() spreadsheet.cell_add('Sheet1!A1', value=1) spreadsheet.cell_add('Sheet1!A2', value=2) spreadsheet.cell_add('Sheet1!A3', formula='=SUM(Sheet1!A1, Sheet1!A2)') spreadsheet.cell_add('Sheet1!A4', formula='=SUM(Sheet1!A1:A2)') spreadsheet.cell_add('Sheet1!A5', formula='=SUM(Sheet1!A1:A1)') self.assertEqual(spreadsheet.evaluate('Sheet1!A3'), 3) # test function self.assertEqual(spreadsheet.evaluate('Sheet1!A4'), 3) # test range self.assertEqual(spreadsheet.evaluate('Sheet1!A5'), 1) # test short range spreadsheet.set_value('Sheet1!A2', 10) self.assertEqual(spreadsheet.evaluate('Sheet1!A3'), 11) # test function self.assertEqual(spreadsheet.evaluate('Sheet1!A4'), 11) # test range self.assertEqual(spreadsheet.evaluate('Sheet1!A5'), 1) # test short range
def test_load_stream(self): file_name = os.path.abspath("./tests/excel/VDB.xlsx") with open(file_name, 'rb') as fin: spreadsheet = Spreadsheet(fin) # really just testing the loading from "fin", but check one cell to be sure it read self.assertEqual(spreadsheet.evaluate('Sheet1!A2'), "Init")
from koala import Spreadsheet sp = Spreadsheet("example.xlsx") v = sp.cell_evaluate('Sheet 1!A1') print("A1", v) sp.cell_set_value('Sheet 1!B5', 15) v = sp.cell_evaluate('Sheet 1!A1') print("A1", v) address = 'Sheet 1!B6' if address not in sp.cellmap and address not in sp.named_ranges: set_value = sp.cell_add else: set_value = sp.cell_set_value set_value(address, value=15)
def setUp(self): self.sp = Spreadsheet("./tests/files/NamedRanges.xlsx", ignore_sheets=['IHS']) sys.setrecursionlimit(10000)
class Test_NamedRanges(unittest.TestCase): def setUp(self): self.sp = Spreadsheet("./tests/files/NamedRanges.xlsx", ignore_sheets=['IHS']) sys.setrecursionlimit(10000) def test_before_set_value(self): self.assertTrue( self.sp.cell_evaluate('INPUT') == 1 and self.sp.cell_evaluate('Sheet1!A1') == 1 and self.sp.cell_evaluate('RESULT') == 187) def test_after_set_value(self): # test if changing a input (named cell) leads to change in result (named cell with formula) self.sp.cell_set_value('INPUT', 2025) self.assertTrue( self.sp.cell_evaluate('INPUT') == 2025 and self.sp.cell_evaluate('Sheet1!A1') == 2025 and self.sp.cell_evaluate('RESULT') == 2211) # want to test multiple values as sometimes it gets stuck after one self.sp.cell_set_value('INPUT', 26) self.assertTrue( self.sp.cell_evaluate('INPUT') == 26 and self.sp.cell_evaluate('Sheet1!A1') == 26 and self.sp.cell_evaluate('RESULT') == 212)