def test_converts_excel_dates_to_python_datetime(self, mock_xlrd_date_as_tuple): mock_excel_worksheet = Mock() def mock_cell(row, col): mock_cell = Mock() mock_cell.ctype = xlrd.XL_CELL_DATE mock_cell.value = (row, col) return mock_cell mock_excel_worksheet.cell.side_effect = mock_cell mock_excel_worksheet.nrows = 4 mock_excel_worksheet.ncols = 3 def mock_xlrd_date_as_tuple_function(cell_value, datemode): row, col = cell_value self.assertEquals(datemode, mock_excel_worksheet.book.datemode) return (2011, row, col, 1, 2, 3) mock_xlrd_date_as_tuple.side_effect = mock_xlrd_date_as_tuple_function worksheet = worksheet_from_excel(mock_excel_worksheet) for col in range(mock_excel_worksheet.ncols): for row in range(mock_excel_worksheet.nrows): self.assertEquals( worksheet[col + 1, row + 1].formula, '=DateTime(2011, %s, %s, 1, 2, 3)' % (row, col))
def test_handles_excel_errors(self, mock_xlrd_date_as_tuple): mock_excel_worksheet = Mock() errors = { (0, 0): (0x0, '=#NULL!'), (1, 0): (0x7, '=#DIV/0!'), (2, 0): (0xf, '=#VALUE!'), (3, 0): (0x17, '=#REF!'), (4, 0): (0x1d, '=#NAME?'), (5, 0): (0x24, '=#NUM!'), (6, 0): (0x2a, '=#N/A'), } def mock_cell(row, col): mock_cell = Mock() mock_cell.ctype = xlrd.XL_CELL_ERROR mock_cell.value = errors[row, col][0] return mock_cell mock_excel_worksheet.cell.side_effect = mock_cell mock_excel_worksheet.nrows = 7 mock_excel_worksheet.ncols = 1 worksheet = worksheet_from_excel(mock_excel_worksheet) for col in range(mock_excel_worksheet.ncols): for row in range(mock_excel_worksheet.nrows): self.assertEquals(worksheet[col + 1, row + 1].formula, errors[row, col][1])
def test_handles_excel_errors(self, mock_xlrd_date_as_tuple): mock_excel_worksheet = Mock() errors = { (0,0) : (0x0, '=#NULL!'), (1,0) : (0x7, '=#DIV/0!'), (2,0) : (0xf, '=#VALUE!'), (3,0) : (0x17, '=#REF!'), (4,0) : (0x1d, '=#NAME?'), (5,0) : (0x24, '=#NUM!'), (6,0) : (0x2a, '=#N/A'), } def mock_cell(row, col): mock_cell = Mock() mock_cell.ctype = xlrd.XL_CELL_ERROR mock_cell.value = errors[row, col][0] return mock_cell mock_excel_worksheet.cell.side_effect = mock_cell mock_excel_worksheet.nrows = 7 mock_excel_worksheet.ncols = 1 worksheet = worksheet_from_excel(mock_excel_worksheet) for col in range(mock_excel_worksheet.ncols): for row in range(mock_excel_worksheet.nrows): self.assertEquals( worksheet[col + 1, row + 1].formula, errors[row, col][1] )
def test_converts_excel_dates_to_python_datetime(self, mock_xlrd_date_as_tuple): mock_excel_worksheet = Mock() def mock_cell(row, col): mock_cell = Mock() mock_cell.ctype = xlrd.XL_CELL_DATE mock_cell.value = (row, col) return mock_cell mock_excel_worksheet.cell.side_effect = mock_cell mock_excel_worksheet.nrows = 4 mock_excel_worksheet.ncols = 3 def mock_xlrd_date_as_tuple_function(cell_value, datemode): row, col = cell_value self.assertEquals(datemode, mock_excel_worksheet.book.datemode) return (2011, row, col, 1, 2, 3) mock_xlrd_date_as_tuple.side_effect = mock_xlrd_date_as_tuple_function worksheet = worksheet_from_excel(mock_excel_worksheet) for col in range(mock_excel_worksheet.ncols): for row in range(mock_excel_worksheet.nrows): self.assertEquals( worksheet[col + 1, row + 1].formula, '=DateTime(2011, %s, %s, 1, 2, 3)' % (row, col) )
def import_xls(request, username): if request.user.username != username: return HttpResponseForbidden(render_to_string("403.html")) handle, filename = mkstemp() try: os.write(handle, request.FILES['file'].read()) wb = xlrd.open_workbook(filename) for xl_sheet in wb.sheets(): if xl_sheet.nrows > 0 and xl_sheet.ncols > 0: name = '%s - %s' % ( splitext(request.FILES['file'].name)[0], xl_sheet.name ) sheet = Sheet(owner=request.user, name=name) sheet.jsonify_worksheet(worksheet_from_excel(xl_sheet)) sheet.save() try: calculate(request, sheet.owner.username, sheet.id) except: pass except Exception: return render_to_response( 'import_xls_error.html', {}, context_instance=RequestContext(request) ) finally: os.close(handle) os.unlink(filename) return HttpResponseRedirect('/')
def test_populates_worksheet_handles_float_source_values(self): mock_excel_worksheet = Mock() def mock_cell(row, col): mock_cell = Mock() mock_cell.value = col + row + 0.1 return mock_cell mock_excel_worksheet.cell.side_effect = mock_cell mock_excel_worksheet.nrows = 4 mock_excel_worksheet.ncols = 3 worksheet = worksheet_from_excel(mock_excel_worksheet) for col in range(mock_excel_worksheet.ncols): for row in range(mock_excel_worksheet.nrows): self.assertEquals(worksheet[col + 1, row + 1].formula, '%s' % (col + row + 0.1, ))