# Spreadsheets

> Read rows out of an Excel or CSV file, change cells in a workbook, and merge a daily export into a report by key, with exact rules and no AI.

Source: https://docs.go-lumen.ai/data/spreadsheets

Three steps work on spreadsheet files: one reads the rows of an Excel or CSV file, one writes values into the cells of a workbook, and one merges rows from an export into a report you keep. Each produces a new file or a list of rows. None overwrites the file it started from.

-   Excel (.xlsx) and CSV
-   3 steps
-   Merge uses no AI

These steps work on **files**. For records that Lumen itself stores, with typed columns and a key, use [Databases and tables](https://docs.go-lumen.ai/data/databases-and-tables.md) instead.

| Step | Console title | Use it to | Credits |
| --- | --- | --- | --- |
| `read-spreadsheet` | **Read a spreadsheet** | Turn the rows of an `.xlsx` or `.csv` file into fields for later steps. | 1 |
| `update-spreadsheet` | **Update a spreadsheet** | Write new values into named cells or matched rows of an `.xlsx` workbook. | 1 |
| `merge-rows` | **Merge rows into a spreadsheet** | Bring the rows of one or more exports into a workbook, matched on a key column. | 0 |

## Read a spreadsheet

[`read-spreadsheet`](https://docs.go-lumen.ai/reference/steps/read-spreadsheet.md) reads the first spreadsheet the document carries, such as an email attachment, and adds its rows as the list field `rows`. Every cell is text, as the file shows it. The step also adds `count`, `columns` (the column names), `truncated` and `headerRow`.

| Setting | What it does |
| --- | --- |
| `file` | Which file to read. Empty reads the first spreadsheet of the document. A name picks one of several attachments. A path such as `saved/Price lists/prices.xlsx` reads a file kept in Files, and `connection:<name>/prices.xlsx` reads one from a Microsoft 365 connection. |
| `newestCopy` | On by default for a path in Files: reads the newest `name (2)` copy that a **Save the file** step made, so the file a flow keeps current is the one it reads. |
| `sheet` | The worksheet of an `.xlsx`. The first when empty. |
| `headerRow` | The row that holds the column names. Row 1 when empty. |
| `findHeader` | Look for the header in the first 30 rows (the row with the most cells filled) instead of `headerRow`. Use it for exports that start with a few title rows. |
| `columns` | Keep only these columns (up to 50). |
| `where` | Keep only rows where a column holds the text. A part of the cell is enough, case and extra spaces do not count, `A|B` means either, `*` as the column searches every column, and a number ignores leading zeros (`00069841` finds `69841`). |
| `maxRows` | The most rows to bring. 1,000 when empty, 10,000 at most. When more rows exist, `truncated` is true. |

```yaml title="spreadsheets-open-rows.yaml"
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
  name: spreadsheets-open-rows
  namespace: tenant-acme
spec:
  displayName: Open rows of a price list as CSV
  triggers:
    - id: manual
      ref: {name: manual-run}
      next: [read]
  nodes:
    - id: read
      stepRef: {name: read-spreadsheet}
      config:
        findHeader: true
        where: {Status: open}
        columns: [Item, Price, Status]
      next: [csv]
    - id: csv
      stepRef: {name: make-csv}
      config: {from: rows, filename: open-rows.csv}
      next: [save]
    - id: save
      stepRef: {name: save-file}
      config: {destination: lumen, folder: "Exports/{flow}"}
```

## Update a spreadsheet

[`update-spreadsheet`](https://docs.go-lumen.ai/reference/steps/update-spreadsheet.md) sets cells of an `.xlsx` workbook and writes the result as a new file. Formulas, tables, styles and the other worksheets stay as they were. An older `.xls` must be saved as `.xlsx` first.

Each entry of `changes` (up to 500) says which cell and what value:

| Field | What it does |
| --- | --- |
| `sheet` | The worksheet. |
| `match` and `column` | Find the row where every `match` header holds its value (case and extra spaces do not count), and set the cell under `column` (a header or a letter). |
| `cell` | An A1 reference such as `C12`, instead of `match` and `column`. |
| `value` | The new value. It may hold placeholders. |
| `type` | How to write it: `auto` (a number when it reads as one, else text), `number`, `text`, or `date-text` (a date written as text in `dateFormat`). |
| `allowFormula` | Let this change write over a formula. Off by default, and only a change written in the step's settings can turn it on, never one that comes from a field. |
| `label`, `source` | What the change is and where it came from, for the list of changes and the note on the cell. |

`changesFrom` names a field that holds a list of changes, as an AI step produces them (`{extract.changes}`). Those come after the changes in the settings.

The step does not guess. A row that matches more than once goes to `ambiguous`, a row that matches nothing to `notFound`, and a cell it will not write (a formula, for example) to `refused`. Cells already holding the value go to `unchanged`. `changes` lists what was written, and `changed` and `problems` count them. When nothing changes, no new file is made.

By default the step also writes a **review copy**: the same workbook with each changed cell highlighted and given a note (the old value, the source, the day). The updated workbook stays clean. Set `mark.output` to `both` to mark the updated workbook and make no second copy, or `none` for no marks; `mark.fill` and `mark.comment` set the highlight color and the note. The review copy is for reading: **Save the file** leaves it out, and an email carries it in place of the clean file.

```yaml title="spreadsheets-price-update.yaml"
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
  name: spreadsheets-price-update
  namespace: tenant-acme
spec:
  displayName: Update a supplier price
  triggers:
    - id: manual
      ref: {name: manual-run}
      next: [update]
  nodes:
    - id: update
      stepRef: {name: update-spreadsheet}
      config:
        file: saved/Price lists/prices.xlsx
        changes:
          - {sheet: Prices, match: {Item: Paper A4}, column: Price, value: "24.90", type: number, source: Supplier email}
      next: [save]
    - id: save
      stepRef: {name: save-file}
      config: {destination: lumen, folder: "Price lists", ifExists: replace}
```

## Merge rows into a spreadsheet

[`merge-rows`](https://docs.go-lumen.ai/reference/steps/merge-rows.md) is for a report that a person updates by hand each day from system exports: the same rows, the same columns, new orders at the bottom. It matches rows on a key, copies the columns you map, and reports whatever the rules cannot settle.

> **No AI:**
> 
> `merge-rows` runs plain rules, not a model, and costs no credits. The same inputs always give the same file. What the rules cannot settle comes out as data in `problems`, so a person or an AI step can decide, and nothing is guessed.

### What it works on

-   The **workbook** to update (`target`) is an `.xlsx`: either one path in Files (`target.file`, or the name of a file of the run), or the newest file in a folder (`target.newestByDateIn`), chosen by the date in its name (`dd.mm.yyyy`, `dd.mm.yy` or `yyyy-mm-dd`). Dates compare as dates, and a copy made by **Save the file** (`24.11.2026 (2).xlsx`) wins over its original.
-   Each **source** is an `.xlsx` among the run's files, found by `match.file`, a pattern on the file name (case does not count, for example `*export*`). Exactly one file must match each source: none stops the step with `Expected a file matching ...`, and more than one stops it too. Up to 10 sources.
-   Columns are found by **header**, not position. A header matches ignoring case and extra spaces, so a column that moves in an export still maps. `targetHeader` and each source's `header` say where the header row is, by `row` or by the text of its first cell (`firstCell`).
-   `key.target` and `key.source` name the key column in the workbook and in the sources. A cell may hold several keys, separated by `key.split` (a comma when empty).
-   `map` says which source column goes to which workbook column. It must not name the key column.

### The rules

For each non-empty row of a source:

| Situation | What happens |
| --- | --- |
| The key matches one row of the workbook | The mapped cells are updated. If the source row has keys the workbook's key cell lacks, they are added to that cell, separated by commas. No second row is made. |
| The key matches no row | The row is **added** at the bottom, copying the cell styles and height of the previous last row. With `appendNew: false` it is reported in `alerts` and not added. |
| The key matches more than one row | Nothing is written for it. A `conflict-multi-row` problem names the rows. |
| The row has no key | Not copied. A `no-key` problem. |

For each mapped cell:

| Situation | What happens |
| --- | --- |
| The source value is empty | Skipped. An empty value never overwrites. |
| The source value differs from the workbook's | **The source wins.** The cell is written, and the old and new values are listed in `changes`. |
| The source value equals the workbook's | Nothing changes. (A number and the same number as text count as equal.) |
| Two sources, or two rows, give the same cell different values | Neither is written. A `conflict-values` problem lists each value and where it came from. |
| The workbook cell holds a formula | Not overwritten. An `unparsable-value` problem. |
| The source holds an Excel error such as `#N/A` | Not copied. An `unparsable-value` problem. |
| The workbook column is a date column and the source value is text | It is read as a date (`12.11.26`, `12/11/2026`, `2026-11-12`) and written as a real date. If it does not read as a date, an `unparsable-value` problem. |
| A mapped header is missing from a file | With `onMissingHeader: report` (the default) every other column is merged and a `missing-header` problem names the header and file. With `fail` the step stops. A missing key column or header row always stops it. |

**Rows are never removed.** Rows of the workbook that no source mentions are left exactly as they were, and so are columns that no `map` names, merged cells, other worksheets and column widths. The workbook you started from is never written over: the result is a new file (`file`), and saving it is a **Save the file** step.

### What it gives back

| Field | What |
| --- | --- |
| `file` | Name of the new workbook, also the run's file. |
| `changes` | Every cell written: `cell`, `column`, `key`, `old`, `new`, `source`. |
| `added` | The rows appended, with their keys, values and source row. |
| `problems` | What the rules could not settle. Each has a `kind`, a `message`, and the source and target rows involved. |
| `alerts`, `conflicts` | The same problems as plain sentences: conflicts are the two `conflict-` kinds. |
| `changed`, `problemCount` | Number of cells written and number of problems. |
| `needsReview` | True when any problem is not a `no-key` row. Branch on it to send a problem to a person. |
| `summary` | A short text in English or Hebrew (`lang`; empty follows the email). |

`sheetName` and `filename` name the new worksheet and file, and may hold `{today}` or `{today:dd.MM.yyyy}` (the date in the workspace's time zone) and the run's fields.

### An example flow

An email brings a daily export. The flow merges it into the orders workbook in Files and saves the result over the same name, keeping the previous version.

```yaml title="spreadsheets-daily-merge.yaml"
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
  name: spreadsheets-daily-merge
  namespace: tenant-acme
spec:
  displayName: Merge the daily orders export
  triggers:
    - id: email
      ref: {name: email-arrives}
      connectorRef: {name: invoices-inbox}
      config: {attachments: together, requireAttachment: true}
      next: [merge]
  nodes:
    - id: merge
      stepRef: {name: merge-rows}
      config:
        target: {file: saved/Reports/orders.xlsx}
        targetHeader: {firstCell: Order No}
        key: {target: Order No, source: Order No}
        sources:
          - match: {file: "*export*"}
            header: {firstCell: Order No}
            map: {Ship Date: Ship Date, Status: Status}
        appendNew: true
        onMissingHeader: report
        filename: orders.xlsx
      next: [save]
    - id: save
      stepRef: {name: save-file}
      config: {destination: lumen, folder: Reports, ifExists: replace}
```

If `needsReview` is true, a flow can hand `problems` to an AI step or ask a person, then apply the fixes on top of the merged file with `update-spreadsheet`, setting its `file` to `{merge.file}`. See [Branches and filters](https://docs.go-lumen.ai/build/branches-and-filters.md) for routing on a field.

When two runs of a flow like this one can overlap (two exports arriving a minute apart), make them take turns with a [file lock](https://docs.go-lumen.ai/data/file-locks.md), so the second reads what the first saved.

## Next steps

[File locksStop two runs from updating the same workbook at once.](https://docs.go-lumen.ai/data/file-locks.md)[FilesWhere workbooks are kept, and how saving and versions work.](https://docs.go-lumen.ai/data/files.md)[Databases and tablesKeep records in Lumen's own tables instead of a file.](https://docs.go-lumen.ai/data/databases-and-tables.md)[Branches and filtersSend a merge with problems to a person.](https://docs.go-lumen.ai/build/branches-and-filters.md)
