Copy, Paste and Fill
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.setValueswrites the heading row and the item intoA1:B2as typed values.Range.copyTocopiesA1:B2to the rangeD1:E2of the same workbook in one undoable operation. The optionmode: 'values'copies the values only.Range.mergemergesA4:B4, andCell.setTextwrites the note through the anchor cellA4.Range.unmergeremoves 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:
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, 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 anchorCell.
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.copyToin the TypeScript reference.