# How to let an AI agent edit Excel files safely, with human review before changes are saved

> Give the agent a draft copy of the workbook, let a person review a cell-by-cell diff, and save only what they approve. The pattern, and how to build it with Univer.

<https://univer.ai/blog/ai-agent-edit-excel-safely-human-review/>

*Published November 25, 2026 · By Univer Team*

**Short answer:** Give the agent a draft copy of the workbook, not the original. Have it check its own edits and show a person a cell-by-cell diff. Write the changes to the real file only after that person approves them.

This guide first describes the pattern in general terms. It then shows how to build it into your own product with Univer, an open-source Office SDK. In Univer, every agent edit stays in an isolated draft, called a Worktree, until a person merges it.

## Why an agent should not write to the original file

An agent that saves straight to the master workbook can:

- overwrite values or formulas outside the range it was asked to change;
- report success when the result is wrong, because a command that ran is not proof that the numbers are right;
- follow instructions hidden in cell text, because the workbook's content is input the agent reads;
- leave no record of what changed, and no way back.

Each step of the pattern below closes one of these gaps.

## The pattern: propose, review, approve, apply

| Step                           | What happens                                                                                                                            | What it prevents                        |
| ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------- |
| 1. Keep the original read-only | The agent gets no write access to the source file.                                                                                      | Unreviewed overwrites                   |
| 2. Scope the task              | Name the sheets and ranges the agent may change, and the kinds of change allowed: values, formulas, formatting or structure.            | Edits outside the request               |
| 3. Edit a draft                | The agent writes only to a staging copy or draft.                                                                                       | Half-finished work reaching users       |
| 4. Self-check                  | The agent reads back what it wrote, recalculates formulas and, where layout matters, renders the sheet.                                 | "Done" reports on broken results        |
| 5. Build a diff                | List each change: sheet, cell or range, old and new value or formula, formatting, and the agent's reason.                               | Reviewers missing broad or hidden edits |
| 6. Human review gate           | A person approves the draft, sends it back for changes, or discards it. Nothing is saved before this.                                   | Unapproved changes                      |
| 7. Apply and record            | Write only the approved changes, save a new version, and record who approved what and when. Keep a way to restore the previous version. | No audit trail, no rollback             |

Two rules make the gate real:

- **Enforce it outside the agent.** If the agent can call "save" or "merge" itself, the gate is only a convention. Put the apply step behind a permission the agent does not hold.
- **Treat the agent's change list as untrusted input.** Check it against the allowed scope before you show or apply it.

## What the reviewer should see

- Each changed cell, with its value before and after.
- Changed formulas shown as formulas, not only as results.
- Formatting changes listed apart from content changes, so a recolored column doesn't hide a changed number.
- The rendered sheet, for anything people will read as a report.
- Three choices: approve, send back, or discard.

## Ways to build it

| Approach                                                      | What you get                                                                                                                                                                                                                                                     | What you build yourself                                                            |
| ------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
| Microsoft 365 APIs                                            | Microsoft Graph's Excel API has a non-persistent session mode: changes are not saved to the source file, and the server keeps a temporary copy for that session. Power Automate's "Start and wait for an approval" action pauses a flow until approvers respond. | The diff, the review screen, and applying approved changes in a persistent session |
| A file library plus your own code                             | Read and write XLSX files from your own language                                                                                                                                                                                                                 | Staging copies, formula calculation, the diff, the review screen, versions         |
| An Office SDK with drafts and review built in, such as Univer | Isolated drafts, a compare view, merge, version history, comments and permissions, embedded in your app                                                                                                                                                          | Your accounts, permissions and review workflow around it                           |

## Build it into your own product with Univer

Univer is an open-source Office SDK (Apache-2.0, [github.com/dream-num/univer](https://github.com/dream-num/univer)). It embeds spreadsheets, documents and slides in web apps, and processes them in Node.js. For agents it adds two things: Univer CLI with its Agent Skill, and an AI SDK for building your own agent CLI.

Agent edits happen in a **Worktree**: an isolated draft of the document content, not a Git worktree. The reviewed main line is called **trunk**. Nothing reaches trunk until a Worktree is merged.

### How each step maps to Univer

| Pattern step             | In Univer                                                                                                                                                                                                                                      |
| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Original stays untouched | Univer CLI allows content writes only in a draft Worktree. Trunk is the reviewed main line.                                                                                                                                                    |
| Scope the task           | Workbook, worksheet and range protection, with permission points such as Edit and View. On a server, read and edit rules are enforced in Collaboration SDK middleware. In the browser, a `BeforeCommandExecute` listener can cancel a command. |
| Edit a draft             | `univer execute` runs Univer Facade code against one Worktree. Each run commits to the draft, not to trunk.                                                                                                                                    |
| Self-check               | `univer inspect` reads back workbooks, worksheets and ranges. `univer screenshot` and `univer print-pdf` render them. Univer calculates formulas with its own engine, so no desktop Excel is needed.                                           |
| Build a diff             | The Viewer's Compare mode shows the Worktree next to trunk. For sheets it has Content and Formatting filters and a Show formulas switch. Agents can read the same diff through the Server API, as changes with typed before and after values.  |
| Human review gate        | `univer worktree ready` freezes the draft's current revision and makes it read-only. A person then merges it, reopens it for more changes, or discards it.                                                                                     |
| Apply and record         | Merge writes the draft into trunk. Edit history keeps server-stored versions that people can compare and restore. Undo and redo work within an editing session. Comments let reviewers discuss a cell, reply, and mark the thread resolved.    |

### Example: the review loop with Univer CLI

Univer CLI needs Node.js 24 or later and an agent that supports Agent Skills. Supported agents include Claude Code, Codex, OpenCode, OpenClaw, Hermes Agent, WorkBuddy and Kimi.

In normal use, the skill runs these commands for the agent. They are listed here so you can see where the gate sits. The example assumes `q3-budget.xlsx` has one sheet, named Budget.

```bash
# 1. Give your agent the Univer CLI skill
npx skills add dream-num/skills -s univer-cli -g

# 2. Import the workbook. This creates a new .univer file; its content is trunk.
univer import ./q3-budget.univer --file ./q3-budget.xlsx

# 3. Open an isolated draft for the agent
univer worktree add ./q3-budget.univer --name agent --json

# 4. The agent edits and reads back inside the draft only
univer execute ./q3-budget.univer --worktree <worktree-id> --unit <unit-id> \
  -e 'workbook.getActiveSheet().getRange("C12").setValue(52000);'
univer inspect range C12 ./q3-budget.univer --worksheet name:Budget \
  --unit <unit-id> --worktree <worktree-id> --json

# 5. Freeze the draft for review and get the local Viewer link
univer worktree ready ./q3-budget.univer --worktree <worktree-id>
univer open ./q3-budget.univer

# 6. Only after a person approves: merge, then export the reviewed workbook
univer worktree merge ./q3-budget.univer --worktree <worktree-id>
univer export ./q3-budget.univer ./q3-budget-reviewed.xlsx
```

The agent stops at step 5 and hands over the Viewer link. The reviewer compares the draft with trunk, then chooses merge, `worktree reopen` (send it back) or `worktree discard`. Export after the merge, so the XLSX holds only reviewed changes.

**About hard gates.** Univer CLI runs on one machine, and the CLI itself includes `worktree merge`. The skill treats merge as an explicit review decision, so on a single machine the gate depends on the agent following the skill. If you need a gate the agent cannot bypass, run review through your own server, as described next, and let only reviewers merge.

### In your own product: CLI, Web and Server

Univer's documentation recommends an application in three parts:

- **AI SDK** (TypeScript, Node.js): build your own CLI that agents use to load, inspect, change and check documents. Your application owns the command names, identity and authorization.
- **Web SDK**: the page where people edit and review. It embeds in React, Vue or plain web pages, with no iframe.
- **Server SDK**: stores documents, revisions, collaboration state and Worktrees in your own database, on your own servers.

The Collaboration SDK does not define users or roles. Your application sets the user's identity in middleware, and the server checks read and edit rights. Worktree has its own lifecycle middleware, and a merge still passes through the core service's checks when it writes to trunk, so you can limit merging to reviewers. Univer's documentation warns that a read-only front end is not enough on its own: check permissions on the server.

If you want a ready-made review app first, Univer Workspace is open source (Apache-2.0) and self-hostable. It is built on the same SDK. Agents edit in Worktrees, and people compare the changes, request a revision, and merge.

### Limits to plan for

- **File formats.** Univer CLI imports XLS, XLSX, XLSM, CSV and TSV, and exports XLSX, CSV and TSV. Unsupported elements may be dropped during conversion, so test with your real files, including formulas, charts and layout.
- **Execution.** `univer execute` runs trusted JavaScript. It is not a sandbox for untrusted code, so run the agent in an environment you control.
- **Multi-document merges.** A merge that covers several documents is not atomic across them. Show the merge result for each document.
- **Rendering.** Screenshots, PDF printing and layout checks need Chrome, Chromium or Edge.
- **Licensing.** Univer's core and Univer CLI are open source. Real-time collaboration, edit history and the server-side Worktree are part of the commercial Office SDK. Freemium ($0, with watermarks and limits) covers development and testing. Production needs a Business or Enterprise license.

## FAQ

**How can I let an AI agent edit Excel files safely?**
Don't give it the original. Let it edit a draft copy, limit which sheets and ranges it may change, and make it read back and recalculate what it wrote. Save to the real file only after a person approves a diff. With Univer, the agent edits in an isolated Worktree, and the original (trunk) stays unchanged until someone merges it.

**How do I add human review before an AI agent's changes are saved?**
Put an approval gate between the draft and the save, and protect that gate with a permission the agent doesn't hold. In Univer, `univer worktree ready` freezes the draft for review. A person compares it with the original in the Viewer, then merges, reopens or discards it. In your own product, limit merging to reviewers in server middleware.

**Can the AI agent overwrite the original Excel file?**
Not if it works in a draft. Univer CLI allows content writes only in a draft Worktree, and `univer execute` commits to that draft, not to trunk. Export the XLSX after the merge.

**What should a reviewer check before approving an agent's spreadsheet edits?**
Check each changed cell with its old and new value, and changed formulas shown as formulas. Check formatting changes separately, and look at the rendered sheet for anything that is a report. Univer's sheet Compare view has Content and Formatting filters and a Show formulas switch for this.

**Which spreadsheet engines let AI agents edit sheets alongside human reviewers?**
Univer is an embeddable spreadsheet and Office SDK with this built in. People and agents co-edit in real time, and agents can work in isolated Worktrees that people review and merge. Microsoft 365 offers building blocks that you assemble yourself: Graph's non-persistent workbook sessions and Power Automate approvals.

**What office SDK lets AI agents edit spreadsheets and documents with human review?**
Univer Office SDK. Its Worktree review covers sheets, documents, slides, relational tables and canvases. Its AI SDK, Web SDK and Server SDK let you run the agent's CLI, the review page and the storage on your own servers.

**Do I need Microsoft Excel installed for the agent to calculate formulas?**
No. Univer calculates formulas with its own engine in Node.js, so the agent reads back calculated values. Test with your own files, including formulas, charts and layout, because unsupported elements may be dropped during conversion.

**Which AI agents can use Univer?**
Agents that support Agent Skills and can run commands. These include Claude Code, Codex, OpenCode, OpenClaw, Hermes Agent, WorkBuddy and Kimi. Install the skill with `npx skills add dream-num/skills -s univer-cli -g`. Your agent keeps its own model, prompts and orchestration.
