# Copy, Paste and Fill

> Copy ranges with copyTo and a paste mode such as values, transpose or link, fill patterns with fillTo, merge and unmerge cells and write to the anchor cell.

- **Product:** TX Sheet Control
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Sheet Control > Spreadsheet Documents > Copy, Paste and Fill
- **1128 words**
- **Web URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheets/copy-fill/
- **LLMs URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheets/copy-fill/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheets/copy-fill/llms-full.txt

---

Writing an application value and copying cells are different operations with different effects on formulas and formatting. Use typed writes for application values and explicit copy and fill operations for spreadsheet authoring. Merged cells, which show the content of one anchor cell, follow rules of their own.

### Copy Values and Merge Cells

The following code writes a heading row and one item, copies their values to `D1:E2`, then merges `A4:B4` for a note and unmerges it again. `editor` is the [`Spreadsheet`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/spreadsheet/llms-full.txt) of the browser editor:

```javascript
const sheet = editor.workbook.sheets.at(0);
sheet.range('A1:B2').setValues([['Item', 'Amount'], ['Service', 120]]);
sheet.range('A1:B2').copyTo(sheet.range('D1:E2'), { mode: 'values' });
sheet.range('A4:B4').merge();
sheet.cell('A4').setText('Invoice notes');
sheet.range('A4:B4').unmerge();
```

The code works as follows:

- [`Range.setValues`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/range/setvalues-method/llms-full.txt) writes the heading row and the item into `A1:B2` as typed values.
- [`Range.copyTo`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/range/copyto-method/llms-full.txt) copies `A1:B2` to the range `D1:E2` of the same workbook in one undoable operation. The option `mode: 'values'` copies the values only.
- [`Range.merge`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/range/merge-method/llms-full.txt) merges `A4:B4`, and [`Cell.setText`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/cell/settext-method/llms-full.txt) writes the note through the anchor cell `A4`.
- [`Range.unmerge`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/range/unmerge-method/llms-full.txt) removes the merges that intersect the range.

### Choose a Copy Mode

[`PasteOptions`](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/pasteoptions/llms-full.txt) describes the supported modes, including all content, values, formulas, formatting, transpose and live cell links. Consult [`PasteMode`](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/pastemode/llms-full.txt) for the exact spellings:

| `mode` | Copies |
|---|---|
| `all` | All content, the default |
| `values` | The values |
| `formulas` | The formulas |
| `formatting` | The formatting |
| `transpose` | The copied rectangle with its axes swapped |
| `link` | Live cell links, which require the same live workbook |

Use destination dimensions appropriate to the chosen mode and a destination from the same workbook. A values-only copy is appropriate when a report needs calculated results without copying formulas.

### Fill a Range From a Pattern

For a fill operation with [`Range.fillTo`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/range/fillto-method/llms-full.txt), define the source pattern and the destination deliberately. Numeric and date sequences and formulas are different patterns. [`FillOptions`](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/filloptions/llms-full.txt) selects the mode `auto`, `copy` or `series`, and the automatic mode detects numeric, date and text-number patterns.

Review the resulting formula references, absolute references and boundaries instead of assuming that every arbitrary source pattern is inferable.

### Clipboard Access and Pending Edits

Browser clipboard access depends on browser permissions and secure-context rules. A programmatic in-workbook `copyTo()` does not require access to the system clipboard.

Commit pending cell edits before triggering application saves when the application expects the typed value to be included. [`EditorUi.commitEdit`](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/editorui/commitedit-method/llms-full.txt) commits a pending cell edit and returns `false` when validation keeps the editor open.

### Write Into Merged Cells

Merged cells display the content of the anchor cell.

> Important
>
> **No Text on a Merged Range.** Merging does not provide a `Range.setText()` operation: write through the anchor [`Cell`](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/cell/llms-full.txt).

Test copy and fill operations across merges, protected cells and tables. Handle rejected operations rather than applying an undocumented partial edit.

### Next Steps

- [Cells and Ranges](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/cells/llms-full.txt): write typed values, literal text and formulas instead of copying them.
- [Formulas](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/formulas/llms-full.txt): check the references and array results of the formulas you copy and fill.
- [Events and Cancellation](https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/events/llms-full.txt): react to changes before and after they are applied.
- [copyTo](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/range/copyto-method/llms-full.txt): look up the parameters of `Range.copyTo` in the TypeScript reference.

---

## Nearby

- **Previous:** [Rows and Columns](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/rows-columns/llms-full.txt)
- **Next:** [Formatting and Number Display](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/formatting/llms-full.txt)
