harbor run -t openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-2643harbor download openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-2643PSEUDOCODE:
// Data model for a cell to be written CLASS ExcelCell INTEGER col // 0-based column index INTEGER row // 0-based row index ANY val // value to write DICT style // style dict or NULL (shape: used by _convert_to_style_kwargs) INTEGER or NULL mergestart // columns to merge to the right; 0 means no horizontal merge INTEGER or NULL mergeend // rows to merge downward; 0 means no vertical merge
// OpenPyxl-backed writer with style conversion CLASS _OpenpyxlWriter
// Convert a style dict into OpenPyxl-ready keyword objects FUNCTION _convert_to_style_kwargs(style: DICT) RETURNS DICT IF style IS NULL THEN RETURN EMPTY DICT END IF
DICT result = EMPTY DICT
// Font
IF "font" IN style THEN
DICT f = style["font"]
result["font"] = Font(bold = f.get("bold", FALSE),
color = Color(style = f.get("color"))) // or Color(f["color"]) depending on API
// Border
IF "border" IN style THEN
DICT b = style["border"]
result["border"] = Border(
top = Side(style = b.get("top")),
right = Side(style = b.get("right")),
bottom = Side(style = b.get("bottom")),
left = Side(style = b.get("left"))
)
// Alignment
IF "alignment" IN style THEN
DICT a = style["alignment"]
result["alignment"] = Alignment(
horizontal = a.get("horizontal"),
vertical = a.get("vertical")
)
// Fill
IF "fill" IN style THEN
DICT fl = style["fill"]
result["fill"] = PatternFill(
patternType = fl.get("patternType"),
fgColor = Color(rgb = fl.get("fgColor", {}).get("rgb"),
tint = fl.get("fgColor", {}).get("tint"))
)
// Number format
IF "format_code" IN style THEN
result["number_format"] = style["format_code"]
// Protection
IF "locked" IN style OR "hidden" IN style THEN
DICT p = {
"locked": style.get("locked", FALSE),
"hidden": style.get("hidden", FALSE)
}
result["protection"] = Protection(locked = p["locked"], hidden = p["hidden"])
END IF
RETURN result
END FUNCTION
// Write a collection of ExcelCell objects into a named sheet // Creates the sheet if it does not exist; supports per-cell styling and merges FUNCTION write_cells(self, cells: LIST[ExcelCell], sheet_name: STRING) // Obtain or create target worksheet within internal workbook ws = self._get_or_create_sheet(sheet_name)
FOR each cell IN cells DO
ROW_IDX = cell.row + 1 // convert to 1-based
COL_IDX = cell.col + 1
// Write value
ws.cell(row = ROW_IDX, column = COL_IDX).value = cell.val
// Apply style if provided
IF cell.style IS NOT NULL THEN
DICT kwargs = _convert_to_style_kwargs(cell.style)
IF "font" IN kwargs THEN ws.cell(ROW_IDX, COL_IDX).font = kwargs["font"]
IF "border" IN kwargs THEN ws.cell(ROW_IDX, COL_IDX).border = kwargs["border"]
IF "alignment" IN kwargs THEN ws.cell(ROW_IDX, COL_IDX).alignment = kwargs["alignment"]
IF "fill" IN kwargs THEN ws.cell(ROW_IDX, COL_IDX).fill = kwargs["fill"]
IF "number_format" IN kwargs THEN ws.cell(ROW_IDX, COL_IDX).number_format = kwargs["number_format"]
IF "protection" IN kwargs THEN ws.cell(ROW_IDX, COL_IDX).protection = kwargs["protection"]
END IF
// Merging
IF cell.mergestart IS NOT NULL AND cell.mergeend IS NOT NULL THEN
// 0-based to 1-based conversion
END_ROW = ROW_IDX + cell.mergeend
END_COL = COL_IDX + cell.mergestart
RANGE_STR = coordinate_range_to_string(ROW_IDX, COL_IDX, END_ROW, END_COL) // e.g., "A1:C2"
ws.merge_cells(RANGE_STR)
// Ensure merged region has consistent styling
FOR r FROM ROW_IDX TO END_ROW DO
FOR c FROM COL_IDX TO END_COL DO
IF cell.style IS NOT NULL THEN
// Re-apply style to all cells in merged region
DICT kwargs = _convert_to_style_kwargs(cell.style)
IF "font" IN kwargs THEN ws.cell(r, c).font = kwargs["font"]
IF "border" IN kwargs THEN ws.cell(r, c).border = kwargs["border"]
IF "alignment" IN kwargs THEN ws.cell(r, c).alignment = kwargs["alignment"]
IF "fill" IN kwargs THEN ws.cell(r, c).fill = kwargs["fill"]
IF "number_format" IN kwargs THEN ws.cell(r, c).number_format = kwargs["number_format"]
IF "protection" IN kwargs THEN ws.cell(r, c).protection = kwargs["protection"]
END IF
END FOR
END FOR
END IF
END FOR
END FUNCTION
// Helper: get or create sheet by name within the workbook FUNCTION _get_or_create_sheet(self, sheet_name: STRING) RETURNS Worksheet IF sheet_name IN self._wb.sheetnames THEN RETURN self._wb[sheet_name] ELSE new_ws = self._wb.create_sheet(title = sheet_name) RETURN new_ws END IF END FUNCTION
// Helper: convert 1-based row/col coordinates to A1-style string FUNCTION coordinate_range_to_string(start_row: INTEGER, start_col: INTEGER, end_row: INTEGER, end_col: INTEGER) RETURNS STRING START_LETTER = get_column_letter(start_col) END_LETTER = get_column_letter(end_col) RETURN CONCAT(START_LETTER, start_row, ":", END_LETTER, end_row) END FUNCTION
// OpenPyxl-backed Excel writer with mode handling CLASS ExcelWriter
// Initialize with path and mode FUNCTION init(path: STRING, mode: STRING = "w", ...) self.path = path self.mode = mode
IF mode == "w" THEN
self._wb = new Workbook() // new workbook
// Optionally remove default sheet if present
IF "Sheet" IN self._wb.sheetnames THEN
self._wb.remove(self._wb["Sheet"])
END IF
ELSE IF mode == "a" THEN
self._wb = load_workbook(path) // append to existing file
self._existing_order = list(self._wb.sheetnames)
ELSE
RAISE error "Unsupported mode"
END IF
END FUNCTION
// Write a new sheet with given cells; used by tests FUNCTION write_sheet(sheet_name: STRING, cells: LIST[ExcelCell]) // In mode "w", ensure only newly written sheets exist IF self.mode == "w" THEN // Remove any pre-existing sheets in workbook except those just created FOR each sh IN self._wb.sheetnames DO IF sh != sheet_name THEN self._wb.remove(self._wb[sh]) END IF END FOR END IF
// Ensure target sheet exists
ws = self._get_or_create_sheet_in_wb(sheet_name)
// Delegate to _OpenpyxlWriter for cell writing
writer = _OpenpyxlWriter(self._wb)
writer.write_cells(cells, sheet_name)
END FUNCTION
// Save workbook to disk FUNCTION save(self) self._wb.save(self.path) END FUNCTION
// Internal helper: ensure sheet exists in current workbook (operates on self._wb) FUNCTION _get_or_create_sheet_in_wb(self, sheet_name: STRING) RETURNS Worksheet IF sheet_name IN self._wb.sheetnames THEN RETURN self._wb[sheet_name] ELSE RETURN self._wb.create_sheet(title = sheet_name) END IF END FUNCTION
END CLASS
// Notes for implementation (informational, not executable pseudocode)