Fakultas Ilmu Komputer UI

Verified Commit a3de74b6 authored by Giovan Isa Musthofa's avatar Giovan Isa Musthofa
Browse files

[CHORES] Use merge & center technique for excel header

parent fce77f3a
Pipeline #50010 passed with stages
in 2 minutes and 16 seconds
from django.conf import settings
from django.contrib import admin
from django.db.models import Q
from django.http import HttpResponse
from django.utils.translation import gettext_lazy as _
from .models import JadwalDonor, DaftarDonor
......@@ -269,12 +269,32 @@ class JadwalDonorSpreadsheetService:
return baru_ulang_list
def _get_col_widths(cls, dataframe):
def _write_header(cls, writer):
workbook = writer.book
merge_format = workbook.add_format({
'align': 'center',
'valign': 'vcenter',
'bold': True,
worksheet = writer.sheets['Sheet1']
worksheet.merge_range('A1:A3', 'No', merge_format)
worksheet.merge_range('B1:B3', 'NoTrans', merge_format)
worksheet.merge_range('C1:C3', 'Tanggal', merge_format)
worksheet.merge_range('D1:L1', 'Pendonor', merge_format)
headers = 'ID', 'Nama Lengkap', 'Alamat', 'HP', 'Umur', 'Gol (RH)', 'JK', 'Baru Ulang', 'Donor Ke-'
for i, header in enumerate(headers):
col = 3 + i
worksheet.merge_range(1, col, 2, col, header, merge_format)
def _get_col_widths(cls, df):
# First we find the maximum length of the index column
idx_max = max([len(str(s)) for s in dataframe.index.values] + [len(str(dataframe.index.name))])
idx_max = max([len(str(s)) for s in df.index.values] + [len(str(df.index.name))])
# Then, we concatenate this to the max of the lengths of column name
# and its values for each column, left to right
return [idx_max] + [max([len(str(s)) for s in dataframe[col].values] + [len(col)]) for col in dataframe.columns]
return [idx_max] + [max([len(str(s)) for s in df[col].values] + [len(col)]) for col in df.columns]
def download_jadwaldonor(self, modeladmin, queryset):
......@@ -305,6 +325,10 @@ class JadwalDonorSpreadsheetService:
for daftar_donor_ulang in daftar_donor_ulang_list
if len(df):
df[fieldnames[1]] = df[fieldnames[1]].dt.tz_localize(None)
# Not actually used to write headers but still usefull to adjust column widths
df = df.rename(columns={
fieldnames[0]: 'NoTrans',
fieldnames[1]: 'Tanggal',
......@@ -317,11 +341,9 @@ class JadwalDonorSpreadsheetService:
fieldnames[8]: 'JK',
if len(df):
df['Tanggal'] = df['Tanggal'].dt.tz_localize(None)
writer = pd.ExcelWriter(response, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
df.to_excel(writer, sheet_name='Sheet1', startrow=3, header=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment