merge_hwp_cells_horizontal
Merge adjacent cells horizontally in a table row by setting colSpan on the first cell and removing the absorbed cells. Specify file, table, row, start column, and number of cells to merge.
Instructions
Merge horizontal cells in a row of a table by setting colSpan on the first cell and removing the absorbed cells. Args: file_path, table_index, row, col_start, col_count (>=2), output_path (optional).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| table_index | Yes | ||
| row | Yes | ||
| col_start | Yes | ||
| col_count | Yes | ||
| output_path | No |
Implementation Reference
- src/tools/edit.ts:381-395 (handler)The tool handler function `mergeHwpCellsHorizontal` in the tools layer. It performs preflight checks, computes output path, calls the core mutation function `mergeHwpxCellsHorizontal`, and returns a Korean-language result string.
export async function mergeHwpCellsHorizontal(args: MergeCellsArgs): Promise<string> { const err = preflight(args.file_path); if (err) return err; const out = args.output_path && args.output_path.length > 0 ? args.output_path : defaultOutput(args.file_path, "merged"); try { const r = await mergeHwpxCellsHorizontal( args.file_path, out, args.table_index, args.row, args.col_start, args.col_count ); return `셀 병합 완료 (merged ${r.merged} cells): 표 ${args.table_index}, 행 ${args.row}, 열 ${args.col_start}..${args.col_start + args.col_count - 1}\n저장 (saved): ${out}`; } catch (e) { return `셀 병합 오류 (merge error): ${(e as Error).message}`; } } - src/tools/edit.ts:372-379 (schema)The `MergeCellsArgs` interface defining the input schema: file_path, table_index, row, col_start, col_count, and optional output_path.
export interface MergeCellsArgs { file_path: string; table_index: number; row: number; col_start: number; col_count: number; output_path?: string; } - src/server.ts:417-431 (schema)The MCP tool registration schema for 'merge_hwp_cells_horizontal', including description and inputSchema with property types and required fields.
name: "merge_hwp_cells_horizontal", description: "Merge horizontal cells in a row of a table by setting colSpan on the first cell and removing the absorbed cells. Args: file_path, table_index, row, col_start, col_count (>=2), output_path (optional).", inputSchema: { type: "object", properties: { file_path: { type: "string" }, table_index: { type: "number" }, row: { type: "number" }, col_start: { type: "number" }, col_count: { type: "number" }, output_path: { type: "string" }, }, required: ["file_path", "table_index", "row", "col_start", "col_count"], }, - src/server.ts:536-536 (registration)Registration of the tool name 'merge_hwp_cells_horizontal' mapping to the handler function `mergeHwpCellsHorizontal` in the server's tool list.
merge_hwp_cells_horizontal: mergeHwpCellsHorizontal, - src/core/hwpx-mutate.ts:770-814 (handler)Core implementation `mergeHwpxCellsHorizontal` that performs the actual HWPX XML mutation: loads the zip/section, finds the specified table/row/columns, sets colSpan on the first cell, removes absorbed cells, and writes the output.
export async function mergeHwpxCellsHorizontal( inputPath: string, outputPath: string, tableIndex: number, rowIndex: number, colStart: number, colCount: number ): Promise<{ merged: number }> { if (colCount < 2) throw new Error("colCount must be >= 2"); const { zip, sectionName, xml } = await loadSection(inputPath); const tblRegex = /<hp:tbl [^>]*>[\s\S]*?<\/hp:tbl>/g; const tables = [...xml.matchAll(tblRegex)]; if (tableIndex < 0 || tableIndex >= tables.length) { throw new Error(`Table index out of range: ${tableIndex}`); } const tableXml = tables[tableIndex][0]; const trRegex = /<hp:tr(?:\s[^>]*)?>[\s\S]*?<\/hp:tr>/g; const trs = [...tableXml.matchAll(trRegex)]; if (rowIndex < 0 || rowIndex >= trs.length) { throw new Error(`Row index out of range: ${rowIndex}`); } const trXml = trs[rowIndex][0]; const tcMatches = [...trXml.matchAll(/<hp:tc(?:\s[^>]*)?>[\s\S]*?<\/hp:tc>/g)]; if (colStart < 0 || colStart + colCount > tcMatches.length) { throw new Error(`Column merge range out of bounds: ${colStart}..${colStart + colCount - 1}`); } const firstTc = tcMatches[colStart][0]; // Add or update colSpan on the first <hp:tc> let mergedFirst: string; if (/colSpan="\d+"/.test(firstTc)) { mergedFirst = firstTc.replace(/colSpan="\d+"/, `colSpan="${colCount}"`); } else if (/^<hp:tc\s/.test(firstTc)) { mergedFirst = firstTc.replace(/^<hp:tc /, `<hp:tc colSpan="${colCount}" `); } else { mergedFirst = firstTc.replace(/^<hp:tc>/, `<hp:tc colSpan="${colCount}">`); } let newTrXml = trXml.replace(firstTc, mergedFirst); for (let i = 1; i < colCount; i++) { newTrXml = newTrXml.replace(tcMatches[colStart + i][0], ""); } const newTableXml = tableXml.replace(trXml, newTrXml); const newXml = xml.replace(tableXml, newTableXml); await writeSection(zip, sectionName, newXml, outputPath); return { merged: colCount }; }