TX Sheet Control

Copy, Paste and Fill

Topic

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 of the browser editor:

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 writes the heading row and the item into A1:B2 as typed values.
  • Range.copyTo 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 merges A4:B4, and Cell.setText writes the note through the anchor cell A4.
  • Range.unmerge removes the merges that intersect the range.

Choose a Copy Mode

PasteOptions describes the supported modes, including all content, values, formulas, formatting, transpose and live cell links. Consult PasteMode for the exact spellings:

modeCopies
allAll content, the default
valuesThe values
formulasThe formulas
formattingThe formatting
transposeThe copied rectangle with its axes swapped
linkLive 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, define the source pattern and the destination deliberately. Numeric and date sequences and formulas are different patterns. FillOptions 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 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.

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: write typed values, literal text and formulas instead of copying them.
  • Formulas: check the references and array results of the formulas you copy and fill.
  • Events and Cancellation: react to changes before and after they are applied.
  • copyTo: look up the parameters of Range.copyTo in the TypeScript reference.