Fix printing area and options step2

This commit is contained in:
2024-11-11 01:13:33 +09:00
parent 5e2b5add5c
commit 872f252923

View File

@ -902,23 +902,27 @@ class SumasenExcel:
source_setup = source_sheet.page_setup
target_setup = target_sheet.page_setup
# fitToPage の設定をコピー
# まず現在の設定をクリア
target_setup.scale = None
target_setup.fitToWidth = None
target_setup.fitToHeight = None
# fitToPageの設定を最初に行う
target_setup.fitToPage = source_setup.fitToPage
if source_setup.fitToPage:
# 「次のページに合わせる」が選択されている場合
target_setup.fitToWidth = source_setup.fitToWidth # ページ(横)の設定
target_setup.fitToHeight = source_setup.fitToHeight # ページ(縦)の設定
# scale をクリア(%指定をオフに)
target_setup.scale = None
if hasattr(source_setup, 'fitToWidth'):
target_setup.fitToWidth = source_setup.fitToWidth
if hasattr(source_setup, 'fitToHeight'):
target_setup.fitToHeight = source_setup.fitToHeight
else:
# 拡大縮小率が指定されている場合
target_setup.scale = source_setup.scale
# fitTo* をクリア
target_setup.fitToWidth = None
target_setup.fitToHeight = None
if hasattr(source_setup, 'scale'):
target_setup.scale = source_setup.scale
logging.info(f"Print scaling settings copied - fitToPage: {target_setup.fitToPage}, "
logging.info(f"Print scaling settings - "
f"fitToPage: {target_setup.fitToPage}, "
f"fitToWidth: {target_setup.fitToWidth}, "
f"fitToHeight: {target_setup.fitToHeight}, "
f"scale: {target_setup.scale}")
@ -927,6 +931,58 @@ class SumasenExcel:
logging.error(f"Error copying print scaling settings: {str(e)}")
raise
def copy_cell_formatting(self, source_cell, target_cell):
"""
セルのフォーマット(罫線を含む)を正確にコピーする
Args:
source_cell: コピー元のセル
target_cell: コピー先のセル
"""
try:
if not source_cell or not target_cell:
return
# フォント設定のコピー
if source_cell.font:
target_cell.font = copy(source_cell.font)
# 塗りつぶしのコピー
if source_cell.fill:
target_cell.fill = copy(source_cell.fill)
# 罫線設定のコピー
if source_cell.border:
new_border = Border(
left=copy(source_cell.border.left) if source_cell.border.left else None,
right=copy(source_cell.border.right) if source_cell.border.right else None,
top=copy(source_cell.border.top) if source_cell.border.top else None,
bottom=copy(source_cell.border.bottom) if source_cell.border.bottom else None,
diagonal=copy(source_cell.border.diagonal) if source_cell.border.diagonal else None,
diagonal_direction=source_cell.border.diagonal_direction,
outline=source_cell.border.outline,
vertical=copy(source_cell.border.vertical) if source_cell.border.vertical else None,
horizontal=copy(source_cell.border.horizontal) if source_cell.border.horizontal else None
)
target_cell.border = new_border
# 配置設定のコピー
if source_cell.alignment:
target_cell.alignment = copy(source_cell.alignment)
# 数値フォーマットのコピー
target_cell.number_format = source_cell.number_format
# 保護設定のコピー
if source_cell.protection:
target_cell.protection = copy(source_cell.protection)
except Exception as e:
logging.error(f"Error copying cell formatting at cell {target_cell.coordinate}: {str(e)}")
def adjust_print_area(self, source_print_area: str, last_data_row: int) -> str:
"""
印刷範囲を最終データ行まで拡張する
@ -1060,27 +1116,6 @@ class SumasenExcel:
# 拡大縮小印刷の設定を正確にコピー
self.copy_print_scaling_settings(self.template_sheet, self.current_sheet)
# Copy cell contents and styles
row_offset = target_min_row - orig_min_row
for row in range(orig_min_row, orig_max_row + 1):
for col in range(orig_min_col, orig_max_col + 1):
source_cell = self.template_sheet.cell(row=row, column=col)
target_cell = self.current_sheet.cell(row=row+row_offset, column=col)
if source_cell.value:
# Copy value
target_cell.value = source_cell.value
#print(f"({col},{row}) : {target_cell.value}")
# Copy styles
if source_cell.has_style:
target_cell.font = copy(source_cell.font)
target_cell.border = copy(source_cell.border)
target_cell.fill = copy(source_cell.fill)
target_cell.number_format = source_cell.number_format
target_cell.protection = copy(source_cell.protection)
target_cell.alignment = copy(source_cell.alignment)
# Copy page setup
target_page_setup = self.current_sheet.page_setup
source_page_setup = self.template_sheet.page_setup
@ -1157,6 +1192,27 @@ class SumasenExcel:
# 用紙の向きと用紙サイズの検証
self._verify_page_setup()
# Copy cell contents and styles
row_offset = target_min_row - orig_min_row
for row in range(orig_min_row, orig_max_row + 1):
for col in range(orig_min_col, orig_max_col + 1):
source_cell = self.template_sheet.cell(row=row, column=col)
target_cell = self.current_sheet.cell(row=row+row_offset, column=col)
if source_cell.value:
# Copy value
target_cell.value = source_cell.value
#print(f"({col},{row}) : {target_cell.value}")
# Copy styles
if source_cell.has_style:
target_cell.font = copy(source_cell.font)
target_cell.border = copy(source_cell.border)
target_cell.fill = copy(source_cell.fill)
target_cell.number_format = source_cell.number_format
target_cell.protection = copy(source_cell.protection)
target_cell.alignment = copy(source_cell.alignment)
return {"status": True, "message": "Successfully copied template range"}
except Exception as e: