Skip to content

Commit 0eb86e4

Browse files
feat: enhance Excel cell writer tool with JSON updates and improved error handling
- Removed debug print statements from `excel-cell-editor.py` and `excel-cell-writer.py`. - Added JSON parsing for updates in `excel-cell-writer.py`, allowing for string input to be processed as JSON. - Implemented error handling for invalid updates format and JSON parsing errors in `excel-cell-writer.py`. - Updated `excel-cell-writer.yaml` to include a new parameter for cell updates, specifying the format for inputting cell references and their new values.
1 parent 4ca5b64 commit 0eb86e4

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

tools/excel-cell-editor.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessag
4646
response.raise_for_status()
4747
file_bytes = response.content
4848

49-
# デバッグ用ログ(コンソール出力)
50-
print(f"ファイルURL: {file_url}")
51-
print(f"ファイルサイズ: {len(file_bytes)} bytes")
5249
except Exception as e:
5350
yield ToolInvokeMessage(
5451
type="text",
@@ -79,31 +76,6 @@ def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessag
7976
type="text",
8077
message={"text": str(cell_data)}
8178
)
82-
83-
# elif operation == "edit":
84-
# # セル内容の編集
85-
# edits = tool_parameters.get("edits", {})
86-
# for cell_ref, new_value in edits.items():
87-
# ws[cell_ref] = new_value
88-
89-
# # 編集結果を保存
90-
# output = BytesIO()
91-
# wb.save(output)
92-
# output.seek(0)
93-
94-
# yield self.create_blob_message(
95-
# blob=output.getvalue(),
96-
# meta={
97-
# "mime_type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
98-
# "filename": "edited_file.xlsx"
99-
# }
100-
# )
101-
102-
# else:
103-
# yield ToolInvokeMessage(
104-
# type="text",
105-
# message={"text": f"無効な操作モード: {operation}"}
106-
# )
10779

10880
except Exception as e:
10981
yield ToolInvokeMessage(

tools/excel-cell-writer.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessag
5252
response.raise_for_status()
5353
file_bytes = response.content
5454

55-
# デバッグ用ログ(コンソール出力)
56-
print(f"ファイルURL: {file_url}")
57-
print(f"ファイルサイズ: {len(file_bytes)} bytes")
5855
except Exception as e:
5956
yield ToolInvokeMessage(
6057
type="text",
@@ -75,8 +72,42 @@ def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessag
7572

7673
# セル内容の更新
7774
try:
75+
# updatesが文字列の場合、JSONとしてパース
76+
if isinstance(updates, str):
77+
import json
78+
try:
79+
# シングルクォートをダブルクォートに置換
80+
updates = updates.replace("'", '"')
81+
# datetimeオブジェクトを文字列に変換
82+
updates = updates.replace(
83+
"datetime.datetime(", '"datetime('
84+
).replace(")", ')"')
85+
updates = json.loads(updates)
86+
except json.JSONDecodeError as e:
87+
yield ToolInvokeMessage(
88+
type="text",
89+
message={"text": f"JSONパースエラー: {str(e)}\n入力データ: {updates}"}
90+
)
91+
return
92+
93+
# updatesが辞書型か確認
94+
if not isinstance(updates, dict):
95+
yield ToolInvokeMessage(
96+
type="text",
97+
message={"text": f"無効なupdates形式です。辞書型またはJSON文字列を指定してください\n入力データ: {updates}"}
98+
)
99+
return
100+
101+
# セル更新処理
78102
for cell_ref, new_value in updates.items():
79103
ws[cell_ref] = new_value
104+
105+
except json.JSONDecodeError:
106+
yield ToolInvokeMessage(
107+
type="text",
108+
message={"text": "JSONパースエラー: updatesが有効なJSON形式ではありません"}
109+
)
110+
return
80111
except Exception as e:
81112
yield ToolInvokeMessage(
82113
type="text",

tools/excel-cell-writer.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ parameters:
2727
zh_Hans: 要写入数据的Excel文件
2828
llm_description: The Excel file that needs to be written to
2929
form: llm
30+
- name: updates
31+
type: string
32+
required: true
33+
label:
34+
en_US: Cell Updates
35+
ja_JP: セル更新
36+
zh_Hans: 单元格更新
37+
human_description:
38+
en_US: Specify the cell references and their new values
39+
ja_JP: セルの参照と新しい値を指定
40+
zh_Hans: 指定单元格引用及其新值
41+
llm_description: A mapping of cell references to new values
42+
form: llm
3043

3144
extra:
3245
python:

0 commit comments

Comments
 (0)