科研技能库/Excel 创建备选方案
科研编程
未发现用户侧风险

Excel 创建备选方案

当 execute_code_sandbox 因环境依赖问题无法执行 Excel 文件操作时,切换到 shell_agent 自主处理依赖安装、代码编写与纠错,确保文件成功生成。

文件预览

2 个文件
SKILL.md
3.7 KB · 可预览
---
name: excel-creation-fallback
description: Use shell_agent as fallback when execute_code_sandbox fails for Excel file operations
---

# Excel File Creation Fallback

When `execute_code_sandbox` fails for openpyxl or Excel operations, delegate to `shell_agent` which can autonomously handle dependency and environment issues.

## When to Use

- `execute_code_sandbox` fails with import errors for openpyxl or related libraries
- The sandbox environment lacks required dependencies
- You need to create or modify Excel files with complex requirements
- Repeated sandbox execution failures for file I/O operations

## Steps

### Step 1: Attempt with execute_code_sandbox first

Try creating the Excel file using Python's openpyxl library in the code sandbox:

```python
from openpyxl import Workbook

wb = Workbook()
ws = wb.active
ws.append(['Column1', 'Column2', 'Column3'])
wb.save('output.xlsx')
```

### Step 2: If it fails, switch to shell_agent

Delegate the task to shell_agent with a clear, comprehensive task description:

```
Create an Excel file named 'output.xlsx' with the following structure:
- Sheet 1: Data with columns [Date, Metric, Value]
- Include sample data rows with realistic values
- Apply basic formatting (bold headers, cell borders)
- Add a summary section with totals or averages
- Save the file in the current directory
```

### Step 3: Let shell_agent handle the environment

The shell_agent will:
- Decide whether to use Python or Bash
- Install dependencies if needed (e.g., `pip install openpyxl`)
- Write and execute the code
- Automatically retry and fix errors (up to several rounds)
- Confirm the file was created successfully

## Example Task Descriptions

### Basic Excel file:
```
Create an Excel file named 'sales_report.xlsx' with headers: Date, Product, Quantity, Price, Total. Add 10 sample rows of data and a formula column for Total (Quantity * Price).
```

### Complex Excel with formatting:
```
Create an Excel file with multiple sheets:
- Sheet 'Summary': Key metrics and totals
- Sheet 'Details': Full transaction data with columns [ID, Date, Customer, Amount, Status]
- Apply conditional formatting to highlight amounts over 1000
- Add borders to all cells and bold headers
```

### Tiered pricing structure:
```
Create an Excel file with a tiered pricing table:
- Column A: Quantity thresholds (0, 100, 500, 1000)
- Column B: Unit price at each tier
- Column C: Discount percentage (e.g., 15% discount over 1000 units)
- Include a financial summary section with projections
```

## Why This Pattern Works

`shell_agent` has several advantages over `execute_code_sandbox` for file creation tasks:

| Feature | execute_code_sandbox | shell_agent |
|---------|---------------------|-------------|
| Dependency installation | Manual/preset only | Autonomous |
| Error recovery | Returns error | Auto-retries and fixes |
| Tool selection | Python only | Python, Bash, or other |
| Filesystem access | Sandbox-limited | Full workspace access |
| Verification | None | Can verify file creation |

## Troubleshooting

If shell_agent also struggles:

1. **Be more specific** - Include exact column names, data types, and formatting requirements
2. **Provide sample data** - Include example rows to clarify expected output
3. **Break it down** - For complex files, request creation in stages
4. **Check permissions** - Ensure the target directory is writable

## Alternative Approaches

If shell_agent is unavailable or unsuitable:

- Use `run_shell` with explicit commands (if you know the exact syntax)
- Check for alternative libraries (xlsxwriter, pandas with openpyxl engine)
- Use CSV as intermediate format, then convert to Excel

SKILL.md

元数据
nameexcel-creation-fallback
description当 execute_code_sandbox 执行 Excel 文件操作失败时,使用 shell_agent 作为备选方案

Excel 文件创建备选方案

当 execute_code_sandbox 因 openpyxl 或 Excel 操作失败时,将任务委派给可自主处理依赖和环境问题的 shell_agent。

适用场景

  • execute_code_sandbox 抛出 openpyxl 或相关库的导入错误时
  • 沙箱环境缺少必要的依赖项
  • 需要创建或修改具有复杂需求的 Excel 文件
  • 针对文件 I/O 操作的沙箱执行反复失败

操作步骤

第 1 步:首先尝试使用 execute_code_sandbox

尝试在代码沙箱中使用 Python 的 openpyxl 库创建 Excel 文件:

python
from openpyxl import Workbook

wb = Workbook()
ws = wb.active
ws.append(['Column1', 'Column2', 'Column3'])
wb.save('output.xlsx')

第 2 步:如果失败,切换到 shell_agent

将任务委派给 shell_agent,并提供清晰、全面的任务描述:

text
创建一个名为“output.xlsx”的 Excel 文件,结构如下:
- Sheet 1:数据列 [日期, 指标, 值]
- 包含具有真实值的示例数据行
- 应用基本格式(加粗标题、单元格边框)
- 添加带有合计或平均值的摘要部分
- 将文件保存在当前目录

第 3 步:让 shell_agent 处理环境

shell_agent 将会:

  • 决定使用 Python 还是 Bash
  • 按需安装依赖(例如 pip install openpyxl)
  • 编写并执行代码
  • 自动重试并修复错误(最多几轮)
  • 确认文件已成功创建

任务描述示例

基础 Excel 文件:

text
创建一个名为“sales_report.xlsx”的 Excel 文件,表头为:日期、产品、数量、价格、总计。添加 10 行示例数据,并为总计列添加公式(数量 * 价格)。

带格式的复杂 Excel:

text
创建一个包含多个工作表的 Excel 文件:
- 工作表“Summary”:关键指标和合计
- 工作表“Details”:完整交易数据,列 [ID, 日期, 客户, 金额, 状态]
- 应用条件格式高亮显示金额超过 1000 的项
- 为所有单元格添加边框并加粗标题

阶梯定价结构:

text
创建一个包含阶梯定价表的 Excel 文件:
- 列 A:数量阈值(0, 100, 500, 1000)
- 列 B:各梯次的单位价格
- 列 C:折扣百分比(例如超过 1000 单位享受 15% 折扣)
- 包含带有预测的财务摘要部分

为什么这种模式有效

shell_agent 在文件创建任务上比 execute_code_sandbox 具有若干优势:

特性execute_code_sandboxshell_agent
依赖安装仅手动/预设自主进行
错误恢复返回错误自动重试并修复
工具选择仅 PythonPython、Bash 或其他
文件系统访问受沙箱限制完整工作区访问
验证无可验证文件创建

故障排除

若 shell_agent 也遇到困难:

  1. 更具体 - 包含确切的列名、数据类型和格式要求
  2. 提供示例数据 - 包含示例行以阐明预期输出
  3. 分步进行 - 对于复杂文件,分阶段请求创建
  4. 检查权限 - 确保目标目录可写

替代方法

如果 shell_agent 不可用或不适用:

  • 使用 run_shell 并给出明确命令(如果你知道确切的语法)
  • 检查其他替代库(xlsxwriter, 基于 openpyxl 引擎的 pandas)
  • 使用 CSV 作为中间格式,然后转换为 Excel