'how to solve xlsx report data overwrite in odoo
I have a dictionary stored in a list, this dictionary looks like:
res = []
if 'depts' in data:
for department in self.env['hr.department'].browse(data['depts']):
res.append(
{
'dept': department.name,
'display': self.get_all_date(data['data'],department.id)
})
When I try to print the data in xlsx sheet, when I select more than one department the data is overwritten. This is my for loop to print report:
i = 0
row = 0
for i in range(len(res)):
print(res[i])
print(len(res[i]['display']))
for row ,j in enumerate(res[i]['display']):
print(j)
print("jenan")
col = 0
sheet.write(row + 2, col, j.department_id.name, format2)
sheet.write(row + 2, col + 1, j.employee_id.name, format2)
sheet.write(row + 2, col + 2, j.date_from, format)
sheet.write(row + 2, col + 3, j.date_to, format)
sheet.write(row + 2, col + 4, j.holiday_status_id.name, format2)
row + 1
i+1
Here is the output in python console to check if the for loop is working or not. From the output I am pretty sure it is working, I don't know what the problem is when printing the report:
{'dept': 'Management', 'display': hr.leave(26, 3, 4, 11, 2, 7, 8, 1)}
8
hr.leave(26,)
jenan
hr.leave(3,)
jenan
hr.leave(4,)
jenan
hr.leave(11,)
jenan
hr.leave(2,)
jenan
hr.leave(7,)
jenan
hr.leave(8,)
jenan
hr.leave(1,)
jenan
{'dept': 'Sales', 'display': hr.leave(18, 15, 22, 19, 23)}
5
hr.leave(18,)
jenan
hr.leave(15,)
jenan
hr.leave(22,)
jenan
hr.leave(19,)
jenan
hr.leave(23,)
jenan
this is the xlsx report that was generated, expected to print 13 leaves but it prints 8 leaves
Solution 1:[1]
Your problem is probably because the row being updated on each loop in for row ,j in enumerate(res[i]['display']):
You just need to change your code a bit:
i = 0
row = 0
for i in range(len(res)):
print(res[i])
print(len(res[i]['display']))
# this is the problem, the variable row is being updated on each loop
# for row ,j in enumerate(res[i]['display']):
for j in res[i]['display']:
print(j)
print("jenan")
col = 0
sheet.write(row + 2, col, j.department_id.name, format2)
sheet.write(row + 2, col + 1, j.employee_id.name, format2)
sheet.write(row + 2, col + 2, j.date_from, format)
sheet.write(row + 2, col + 3, j.date_to, format)
sheet.write(row + 2, col + 4, j.holiday_status_id.name, format2)
row + 1
# i+1 no need for this, i is already being updated on each loop
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SDBot |
