def main(): w = xl.Workbook(sample_workbook_path) w.range("A1").set("Hello World!") def doubled(x): return 2*x def alpha(x): return "abcdefgh"[int(x) - 1] xl.map(doubled, w.get("Values")) xl.map(alpha, w.get("doubled")) print(w.range("C3:E7").get())
def test_map_visibility(self): """Tests that the 'map' function respects a range's filtering. Given a visibility-filtered range, the map function should not be applied to hidden data, etc. The mapped column should align 1:1 with source ranges""" c1, c2 = self.workbook.get("mv_x"), self.workbook.get("mv_y") # Only map visible xl.map(lambda x, y: x + y, c1, c2) self.assertEqual(self.workbook.get("E3:E8").get(), [22.0, 44.0, 66.0]) self.assertEqual(self.workbook.get("E3:E8").including_hidden.get(), [None, 22.0, None, 44.0, None, 66.0]) # Map all xl.map(lambda x, y: x + y, c1.including_hidden, c2.including_hidden) self.assertEqual(self.workbook.get("F3:F8").including_hidden.get(), [11.0, 22.0, 33.0, 44.0, 55.0, 66.0])
def test_view_to_table(self): """Tests that the `view` function extends a table, if specified, rather than the first open column. The top level map function should deduce destination table from source ranges""" tables = list(self.workbook.active_sheet.tables) self.assertEqual(len(tables), 1) self.workbook.view(['a', 'b', 'c'], to=tables[0]) # Range here specifies the first added column to the table. _not_ first empty column (A) self.assertEqual(self.workbook.get("G9:G11").get(), ['a', 'b', 'c']) c1, c2 = self.workbook.get("view_to_table_1"), self.workbook.get("view_to_table_2") xl.map(lambda x, y: x + y, c1, c2) # Range here specifies the second added column to the table. _not_ first empty column (A) self.assertEqual(self.workbook.get("H9:H11").get(), [11.0, 13.0, 15.0])
def main(): w = xl.Workbook(sample_workbook_path) w.range("A1").set("Hello World!") def doubled(x): return 2 * x def alpha(x): return "abcdefgh"[int(x) - 1] xl.map(doubled, w.get("Values")) xl.map(alpha, w.get("doubled")) print((w.range("C3:E7").get()))
def test_write_and_read_view(self): """Tests that a python list can be inserted as a column (with xl.view), and then read back. A header is expected""" x = range(1, 10) r = xl.view(x) self.assertEqual(x, r.get()) v = self.workbook.get("A2").get() self.assertEqual(1, v) for i in range(len(x)): val = r[i] # range index operator self.assertEqual(val, x[i]) self.assertEqual(r[-1], x[-1]) # negative indices! self.assertEqual(r[2:5], x[2:5]) # slice indexing for i, val in enumerate(r): # iterating over range self.assertEqual(val, x[i]) # Should be able to get the new workbook by name wb2 = xl.Workbook('Book1') self.assertEqual(1, wb2.range("A2").get()) # Basic map pattern def MyDouble(x): return x * 2 r2 = xl.map(MyDouble, r) self.assertEqual(map(MyDouble, x), r2.get())
def test_write_and_read_view(self): """Tests that a python list can be inserted as a column (with xl.view), and then read back. A header is expected""" x = range(1,10) r = xl.view(x) self.assertEqual(x, r.get()) v = self.workbook.get("A2").get() self.assertEqual(1, v) for i in range(len(x)): val = r[i] # range index operator self.assertEqual(val, x[i]) self.assertEqual(r[-1], x[-1]) # negative indices! self.assertEqual(r[2:5], x[2:5]) # slice indexing for i, val in enumerate(r): # iterating over range self.assertEqual(val, x[i]) # Should be able to get the new workbook by name wb2 = xl.Workbook('Book1') self.assertEqual(1, wb2.range("A2").get()) # Basic map pattern def MyDouble(x): return x * 2 r2 = xl.map(MyDouble, r) self.assertEqual(map(MyDouble, x), r2.get())
def main(): """Loads and modifies a sample spreadsheet in Excel.""" workbook_path = path.join(path.dirname(__file__), 'PyvotSample.xlsx') workbook = xl.Workbook(workbook_path) workbook.range("A1").set("Hello World!") def doubled(val): """Returns double of value passed in.""" return 2 * val def alpha(val): """Returns letter at specified position in alphabet.""" return "abcdefgh"[int(val) - 1] xl.map(doubled, workbook.get("Values")) xl.map(alpha, workbook.get("doubled")) print(workbook.range("C3:E7").get())