diff --git a/blazor/spreadsheet/clipboard.md b/blazor/spreadsheet/clipboard.md index cf9b29e7f2..3417bebfda 100644 --- a/blazor/spreadsheet/clipboard.md +++ b/blazor/spreadsheet/clipboard.md @@ -9,78 +9,391 @@ documentation: ug # Clipboard in Blazor Spreadsheet component -The Spreadsheet provides support for clipboard operations such as **Cut**, **Copy**, and **Paste**. These operations can be enabled or disabled by setting the [`EnableClipboard`](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_EnableClipboard) property in the Spreadsheet component. By default, the `EnableClipboard` property is set to **true**. +The Spreadsheet supports clipboard operations such as **Cut**, **Copy**, and **Paste**. These operations can be enabled or disabled using the [EnableClipboard](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_EnableClipboard) property of the Spreadsheet component. By default, the `EnableClipboard` property is set to **true**. + +The keyboard shortcuts are available to perform clipboard operations efficiently within the Spreadsheet component. `Ctrl+C` copies the selected cells, `Ctrl+X` cuts the selected cells, and `Ctrl+V` pastes the content from the clipboard. + +When `EnableClipboard` is set to **false**, the **Cut** and **Copy** options are removed from the user interface (Ribbon and Context Menu). In addition, shortcut keys (**Ctrl+C, Ctrl+X, Ctrl+V**) and API methods will not work. If the worksheet is protected, clipboard operations such as cut and paste are also disabled. For more information on worksheet protection, refer [here](https://blazor.syncfusion.com/documentation/spreadsheet/protection#protect-sheet). ## Cut -The cut operation removes data from a selected range of cells, rows, or columns in a sheet and places it on the clipboard for use elsewhere. +The **Cut** operation removes data from selected cells, rows, or columns within a worksheet and temporarily stores the content on the clipboard. When the content is pasted to a new location, the original data is deleted from its source. This behavior enables the relocation of content within the Spreadsheet. + +### Cut operations via UI + +The **Cut** operation can be performed through the user interface (UI) using any of the following methods: + +**Using the Ribbon** + +- Select a cell or range of cells to cut the content. +- Click the **Cut** button in the **Home** tab of the **Ribbon** toolbar. This action removes the selected cell or range of cells and places the content on the clipboard. + +![Cut - Ribbon](images/cut-copy.png) + +**Using the Context Menu** + +- Select a cell or range of cells to cut the content. +- Right-click on the selected cell or range of cells. +- Choose the **Cut** option from the context menu. + +![Cut - Context Menu](images/contextmenu-cut-copy.png) + +### Cut operations programmatically + +The [CutCellAsync](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_CutCellAsync_System_String_) method allows performing cut operations within any sheet. This method copies the specified cell or range and its properties (including value, format, style, etc.) to the clipboard and removes it from the sheet. It supports multiple scenarios for cutting cells or ranges. Below are the details for each scenario, including code examples and parameter information. + +> The **Cut** operation will not execute if invalid or out-of-boundary cell ranges are specified. All cell references must fall within the defined worksheet boundaries to ensure successful execution of the operation. + +**Cut active range** + +When `CutCellAsync` method is invoked without any parameters, the content is automatically cut from the most recently selected range, provided an active selection exists. If no range is currently selected, the method defaults to cutting the content from the active cell. + +{% tabs %} +{% highlight razor %} + +@using Syncfusion.Blazor.Spreadsheet + + + + + + + +@code { + public byte[] DataSourceBytes { get; set; } + public SfSpreadsheet SpreadsheetInstance; + + protected override void OnInitialized() + { + string filePath = "wwwroot/Sample.xlsx"; + DataSourceBytes = File.ReadAllBytes(filePath); + } + + public async Task CutActiveCell() + { + // Cuts content from the currently active cell or selected range in the active worksheet. + await SpreadsheetInstance.CutCellAsync(); + } +} + +{% endhighlight %} +{% endtabs %} + +**Cut specific range in active sheet** + +To cut content from specific cells in the active worksheet, a cell address or a range of cell addresses must be passed as a parameter to the `CutCellAsync` method. When a valid cell or range is specified, the Spreadsheet component cuts the corresponding content and places it on the clipboard, making it available for pasting in another location. + +The available parameters in the `CutCellAsync` method are: + +| Parameter | Type | Description | +|-------------|-------------------|-------------| +| cellAddress | string (optional) | Specifies the target cell or range of cells to be cut. Accepts either a single cell reference (for example, **"A1"**) or a range (for example, **"A1:B5"**) from the active worksheet. If no parameter is provided, the currently selected cell or range will be used for the **Cut** operation. | + +{% tabs %} +{% highlight razor %} + +@using Syncfusion.Blazor.Spreadsheet + + + + + + + + +@code { + public byte[] DataSourceBytes { get; set; } + public SfSpreadsheet SpreadsheetInstance; + + protected override void OnInitialized() + { + string filePath = "wwwroot/Sample.xlsx"; + DataSourceBytes = File.ReadAllBytes(filePath); + } + + public async Task CutCell() + { + // Cuts the specified cell from the active worksheet. + await SpreadsheetInstance.CutCellAsync("A2"); + } + + public async Task CutRange() + { + // Cuts a specified range of cells from the active worksheet. + await SpreadsheetInstance.CutCellAsync("A1:D10"); + } +} + +{% endhighlight %} +{% endtabs %} + +**Cut specific range in different sheet** + +To cut content from a specific worksheet, the source sheet name must be included along with the cell reference in the parameter passed to the `CutCellAsync` method. When specifying a sheet name, an exclamation mark (**!**) must be used to separate the sheet name from the cell reference. Upon execution, the Spreadsheet component cuts the designated content and places it on the clipboard, making it available for pasting in another location. + +The available parameters in the `CutCellAsync` method are: + +| Parameter | Type | Description | +|-------------|-------------------|-------------| +| cellAddress | string (optional) | Specifies the cell or range of cells to be cut. Accepts either a single cell reference (for example, **"Sheet1!A1"**) or a range (for example, **"Sheet2!A1:C5"**) from a specific worksheet. If no parameter is provided, the currently selected cell or range from the active worksheet will be used for the cut operation. | + +{% tabs %} +{% highlight razor %} + +@using Syncfusion.Blazor.Spreadsheet + + + + + + + + +@code { + public byte[] DataSourceBytes { get; set; } + public SfSpreadsheet SpreadsheetInstance; + + protected override void OnInitialized() + { + string filePath = "wwwroot/Sample.xlsx"; + DataSourceBytes = File.ReadAllBytes(filePath); + } -**Cutting Data in Spreadsheet** + public async Task CutCellFromSpecificSheet() + { + + // Specifies the address, along with the worksheet name, from which the content will be cut. + await SpreadsheetInstance.CutCellAsync("Sheet1!B2"); + } -The cut operation can be performed through the following methods: + public async Task CutRangeFromSpecificSheet() + { + // Cuts a specified range from a specific worksheet. + await SpreadsheetInstance.CutCellAsync("Sheet2!B3:E8"); + } +} -* Select the **Cut** button in the **HOME** tab of the Ribbon toolbar to execute the cut operation. -* Right-click and select the **Cut** option from the context menu. -* Press the keyboard shortcut `Ctrl + X`(Windows) or `Command + X`(Mac). -* Using the [`CutCellAsync`](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_CutCellAsync_System_String_) method. +{% endhighlight %} +{% endtabs %} ## Copy -The copy operation copies data from a selected range of cells, rows, or columns in a sheet and places it on the clipboard for use elsewhere. +The **Copy** operation duplicates data from a selected range of cells, rows, or columns within a worksheet and temporarily stores it on the clipboard. Unlike the **Cut** operation, copying retains the original content in its source location, allowing the data to be reused without modification. + +### Copy operations via UI + +The copy operation can be performed through the user interface (UI) using any of the following methods: + +**Using the Ribbon** + +- Select the cell or range of cells to copy. +- Click the **Copy** button in the **Home** tab of the **Ribbon** toolbar. This action duplicates the selected cell or range of cells and places the content on the clipboard. + +![Copy - Ribbon](images/cut-copy.png) + +**Using the Context Menu** + +- Select the cell or range of cells to copy. +- Right-click on the selected cell or range of cells. +- Choose the **Copy** option from the context menu. + +![Copy - Context Menu](images/contextmenu-cut-copy.png) + +### Copy operations programmatically + +The [CopyCellAsync](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_CopyCellAsync_System_String_) method enables performing copy operations within any sheet. This method copies the specified cell or range of cells along with its properties (including value, format, style, etc.) to the clipboard. It supports multiple scenarios for copying cells or ranges. Below are the details for each scenario, including code examples and parameter information. + +> The **Copy** operation will not execute if invalid or out-of-boundary cell ranges are specified. All cell addresses must fall within the valid boundaries of the worksheet to ensure successful execution of the operation. + +**Copy active range** + +When `CopyCellAsync` method is invoked without any parameters, the content is automatically copied from the most recently selected range, provided an active selection exists. If no range is selected, the method defaults to copying the content from the active cell. + +{% tabs %} +{% highlight razor %} + +@using Syncfusion.Blazor.Spreadsheet + + + + + + + +@code { + public byte[] DataSourceBytes { get; set; } + public SfSpreadsheet SpreadsheetInstance; + + protected override void OnInitialized() + { + string filePath = "wwwroot/Sample.xlsx"; + DataSourceBytes = File.ReadAllBytes(filePath); + } + + public async Task CopyActiveCell() + { + // Copies content from the currently active cell or selected range in the active worksheet. + await SpreadsheetInstance.CopyCellAsync(); + } +} + +{% endhighlight %} +{% endtabs %} + +**Copy specific range in active sheet** + +To copy content from specific cells in the active worksheet, a cell address or a range of cell addresses must be provided as a parameter to the `CopyCellAsync` method. When a valid cell or range is specified, the Spreadsheet component copies the corresponding content and places it on the clipboard, making it available for pasting in another location. + +The available parameters in the `CopyCellAsync` method are: + +| Parameter | Type | Description | +|-------------|-------------------|-------------| +| cellAddress | string (optional) | Specifies the cell or range of cells to be copied. Accepts either a single cell reference from the active worksheet (for example, **"A1"**) or a range (for example, **"A1:B5"**). If no parameter is provided, the currently selected cell or range will be used for the copy operation. | + +{% tabs %} +{% highlight razor %} + +@using Syncfusion.Blazor.Spreadsheet + + + + + + + + +@code { + public byte[] DataSourceBytes { get; set; } + public SfSpreadsheet SpreadsheetInstance; -**Copying Data in Spreadsheet** + protected override void OnInitialized() + { + string filePath = "wwwroot/Sample.xlsx"; + DataSourceBytes = File.ReadAllBytes(filePath); + } -The copy operation can be performed through the following methods: + public async Task CopyCell() + { + // The specified cell is copied from the active worksheet. + await SpreadsheetInstance.CopyCellAsync("A2"); + } -* Select the **Copy** button in the **HOME** tab of the Ribbon toolbar to execute the copy operation. -* Right-click and select the **Copy** option from the context menu. -* Press the keyboard shortcut `Ctrl + C` (Windows) or `Command + C` (Mac). -* Using the [`CopyCellAsync`](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_CopyCellAsync_System_String_) method. + public async Task CopyRange() + { + // A specified range of cells is copied from the active worksheet. + await SpreadsheetInstance.CopyCellAsync("A1:D10"); + } +} + +{% endhighlight %} +{% endtabs %} + +**Copy specific range in different sheet** + +To copy content from a specific worksheet, the source sheet name must be included along with the cell reference in the parameter passed to the `CopyCellAsync` method. When specifying the sheet name, an exclamation mark (**!**) is used to separate it from the cell reference. The Spreadsheet component performs the copy operation and places the content on the clipboard, making it available for pasting into another location. + +The available parameters in the `CopyCellAsync` method are: + +| Parameter | Type | Description | +|-------------|-------------------|-------------| +| cellAddress | string (optional) | Specifies the cell or range of cells to be copied. Accepts either a single cell reference from a specific worksheet (for example, **"Sheet1!A1"**) or a range of cells (for example, **"Sheet2!A1:C5"**). If no value is provided, the currently selected cell or range from the active worksheet will be used for the copy operation. | + +{% tabs %} +{% highlight razor %} + +@using Syncfusion.Blazor.Spreadsheet + + + + + + + + +@code { + public byte[] DataSourceBytes { get; set; } + public SfSpreadsheet SpreadsheetInstance; + + protected override void OnInitialized() + { + string filePath = "wwwroot/Sample.xlsx"; + DataSourceBytes = File.ReadAllBytes(filePath); + } + + public async Task CopyCellFromSpecificSheet() + { + // The cell address, including the sheet name, is copied from the specified worksheet. + await SpreadsheetInstance.CopyCellAsync("Sheet1!B2"); + } + + public async Task CopyRangeFromSpecificSheet() + { + // A specified range of cells is copied from the designated worksheet. + await SpreadsheetInstance.CopyCellAsync("Sheet2!B3:E8"); + } +} + +{% endhighlight %} +{% endtabs %} ## Paste -The paste operation pastes data from the clipboard into a selected range of cells, rows, or columns. This includes all information such as values and formatting. +The paste operation inserts data from the clipboard into a selected range of cells, rows, or columns, retaining all relevant details such as values, formats, and styles. When performing a **Cut** followed by **Paste**, the clipboard is cleared after the data is transferred. In contrast, with a **Copy** followed by **Paste**, the clipboard contents remain available for reuse. + +**External clipboard** support is provided, allowing content to be pasted not only from within the spreadsheet but also from external sources such as Google Sheets, Microsoft Excel, Word documents, plain text files, and web pages. + +### Paste operations via UI + +The paste operation can be performed through the user interface (UI) using any of the following methods: + +**Using the Ribbon** + +- A cell must be selected or a range of cells highlighted before initiating the paste operation. +- The **Paste** button located in the **Home** tab of the **Ribbon** toolbar must be clicked to perform the paste action. +- The values previously cut or copied from the clipboard will be inserted into the selected range. +- If the clipboard does not contain any values, the **Paste** option will remain disabled. + +![Paste - Ribbon](./images/cutcopypaste.png) -External clipboard functionality is supported. When using cut and paste, the clipboard is cleared after pasting. With copy and paste, the clipboard contents remain unchanged. +**Using the Context Menu** -**Pasting Data in Spreadsheet**: +- A cell must be clicked or a range of cells selected before initiating the paste operation. +- The **Paste** option must be selected from the context menu accessed via right-click. +- The values previously cut or copied from the clipboard will be inserted into the selected range. +- If the clipboard does not contain any values, the **Paste** option will remain disabled. -The paste operation can be performed through the following methods: +![Paste - Context Menu](./images/contextcutcopypaste.png) -* Select the **Paste** button in the **HOME** tab of the Ribbon toolbar to execute the paste operation. -* Right-click and select the **Paste** option from the context menu. -* Press the keyboard shortcut `Ctrl + V` (Windows) or `Command + V` (Mac). -* Using the [`PasteCellAsync`](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_PasteCellAsync_System_String_) method. +### Paste operations programmatically -> When using the keyboard shortcut keys for cut (`Ctrl + X`) or copy (`Ctrl + C`) from external sources, use the `Ctrl + V` shortcut to paste content into the Spreadsheet. +The [PasteCellAsync](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_PasteCellAsync_System_String_) method pastes the clipboard content into a specified cell or range of cells and preserves all associated properties (including value, format, style, etc.). When the source range is larger than the specified target range, all data from the source will still be pasted. The paste operation automatically extends beyond the defined target boundaries and overrides the content in the expanded area to match the full dimensions of the source. -![UI showing context menu cut, copy and paste option](./images/contextcutcopypaste.png) +**Example** +- Source Range: **"Sheet1!A1:C3"** (3 rows × 3 columns) +- Target Range: **"Sheet2!B2"** (single cell) -![UI showing ribbon cut, copy and paste option](./images/cutcopypaste.png) +In this case, although only a single cell is selected as the target, the paste operation overrides the range **"Sheet2!B2:D4"** to match the full 3×3 source content. + +> The **Paste** operation will not be executed if invalid or out-of-boundary cell ranges are specified. All cell addresses must fall within the valid boundaries of the worksheet to ensure successful execution of the paste action. + +**Paste to active range** + +When the `PasteCellAsync` method is invoked without any parameters, the content is automatically pasted into the last selected range, provided an active selection exists. If no range is selected, the content is pasted into the active cell. {% tabs %} -{% highlight razor tabtitle="Index.razor" %} - +{% highlight razor %} + @using Syncfusion.Blazor.Spreadsheet -@using Syncfusion.Blazor.SplitButtons - - - - - - - - - - - - + + + + - + @code { public byte[] DataSourceBytes { get; set; } - public SfSpreadsheet SpreadsheetRef { get; set; } + public SfSpreadsheet SpreadsheetInstance; protected override void OnInitialized() { @@ -88,57 +401,201 @@ The paste operation can be performed through the following methods: DataSourceBytes = File.ReadAllBytes(filePath); } - private async Task ItemSelected(MenuEventArgs args) + public async Task PasteActiveCell() { - if (args.Item.Text == "Cut") - { - await SpreadsheetRef.CutCellAsync(); - } + // The content is pasted into the currently active cell or range. + await SpreadsheetInstance.PasteCellAsync(); + } +} - if (args.Item.Text == "Copy") - { - await SpreadsheetRef.CopyCellAsync(); - } +{% endhighlight %} +{% endtabs %} - if (args.Item.Text == "Paste") - { - await SpreadsheetRef.PasteCellAsync(); - } +**Paste to specific range in active sheet** + +To paste content into specific range in the current active sheet, provide the target cell address as a parameter to the `PasteCellAsync` method. A valid cell selection must exist prior to executing the paste operation. Either a single cell or a range of cells can be specified as the destination. + +The available parameters in the `PasteCellAsync` method are: + +| Parameter | Type | Description | +|-------------|-------------------|-------------| +| cellAddress | string (optional) | Specifies the target cell or range of cells for pasting clipboard content. Accepts either a single cell reference (for example, **"A1"**) or a range of cells (for example, **"A1:B5"**) from the active worksheet. A valid cell selection must exist prior to executing the paste operation. If no parameter is provided, the currently selected cell or range will be used as the paste destination. | + +{% tabs %} +{% highlight razor %} + +@using Syncfusion.Blazor.Spreadsheet + + + + + + + + + +@code { + public byte[] DataSourceBytes { get; set; } + public SfSpreadsheet SpreadsheetInstance; + + protected override void OnInitialized() + { + string filePath = "wwwroot/Sample.xlsx"; + DataSourceBytes = File.ReadAllBytes(filePath); + } + + public async Task PasteCell() + { + // The clipboard content is pasted into the specified cell. + await SpreadsheetInstance.PasteCellAsync("A2"); + } + + public async Task PasteRange() + { + // The clipboard content is pasted into the specified range of cells. + await SpreadsheetInstance.PasteCellAsync("A2:B5"); + } + + // A larger source range is copied and pasted into a smaller target range. + public async Task CopyAndPasteOversizedRange() + { + // A range containing 7 rows is copied from the source. + await SpreadsheetInstance.CopyCellAsync("F3:F9"); + + // The content is pasted into a smaller 3-row target range. All 7 rows will be pasted, extending beyond the specified range. + await SpreadsheetInstance.PasteCellAsync("A5:A7"); } } - + {% endhighlight %} {% endtabs %} -### Prevent pasting +**Paste to specific range in different sheet** + +To paste content into a specific sheet, include the target sheet name along with the cell reference as a parameter to the `PasteCellAsync` method. When specifying a sheet name, use an exclamation mark (**!**) to separate it from the cell reference. + +The available parameters in the `PasteCellAsync` method are: -The following example illustrates how to prevent the paste action in the Spreadsheet. In the [`Pasting`](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.PastingEventArgs.html) event, setting the `Cancel` argument to **true** prevents the paste operation. +| Parameter | Type | Description | +|-------------|-------------------|-------------| +| cellAddress | string (optional) | Specifies the target cell or range of cells for pasting clipboard content. Accepts either a single cell reference from a specific worksheet (for example, **"Sheet1!A1"**) or a range of cells (for example, **"Sheet2!A1:C5"**). A valid cell selection must exist before executing the paste operation. If no parameter is provided, the currently selected cell or range will be used as the paste destination. | + +{% tabs %} +{% highlight razor %} + +@using Syncfusion.Blazor.Spreadsheet + + + + + + + +@code { + public byte[] DataSourceBytes { get; set; } + public SfSpreadsheet SpreadsheetInstance; + + protected override void OnInitialized() + { + string filePath = "wwwroot/Sample.xlsx"; + DataSourceBytes = File.ReadAllBytes(filePath); + } + + public async Task PasteCellToTargetSheet() + { + // The cell address, including the sheet name, is used as the paste destination + await SpreadsheetInstance.PasteCellAsync("Sheet1!B2"); + } +} + +{% endhighlight %} +{% endtabs %} + +## Events + +The Blazor Spreadsheet provides events that are triggered during the clipboard action such as [CutCopyActionBegin](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.CutCopyActionBeginEventArgs.html) and [Pasting](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.PastingEventArgs.html). These events can be used to perform any custom actions before the clipboard action starts or interacted with, allowing for validation, customization, and response handling. + +* **CutCopyActionBegin** - `CutCopyActionBegin` event is triggered before a cut or copy operation is initiated. +* **Pasting** - `Pasting` event is triggered prior to the initiation of a paste operation. + +### CutCopyActionBegin + +The `CutCopyActionBegin` event is triggered before a copy or cut operation is performed in the spreadsheet. This event allows inspection, validation, or cancellation of the operation based on custom business logic. + +**Purpose** + +This event addresses scenarios that require monitoring or restriction of clipboard operations, such as preventing sensitive data from being copied, logging clipboard activities for audit and compliance purposes, enforcing custom validation rules for designated cells or ranges, and restricting cut operations while allowing copy functionality. + +**Event Arguments** + +The `CutCopyActionBeginEventArgs` includes the following properties: + +| Event Arguments | Description | +|----------------|-------------| +| ClipboardAction | Specifies the type of clipboard operation in progress. Returns a value from the **ClipboardAction** enumeration, such as **ClipboardAction.Cut** or **ClipboardAction.Copy**. | +| CopiedRange | Represents the full address of the cell range involved in the clipboard operation. Includes the worksheet name and range in A1 notation (e.g., **"Sheet1!A1:B5"**). | +| Cancel | Indicates whether the clipboard operation should be cancelled. Set to **true** to prevent the cut or copy action from proceeding. | {% tabs %} {% highlight razor tabtitle="Index.razor" %} @using Syncfusion.Blazor.Spreadsheet -@using Syncfusion.Blazor.SplitButtons - - - - - - - - - - - - + + - + @code { public byte[] DataSourceBytes { get; set; } + + protected override void OnInitialized() + { + string filePath = "wwwroot/Sample.xlsx"; + DataSourceBytes = File.ReadAllBytes(filePath); + } + + public void OnCutCopyActionBegin(CutCopyActionBeginEventArgs args) + { + // Cancels the cut or copy operation. + args.Cancel = true; + } +} + +{% endhighlight %} +{% endtabs %} + +### Pasting + +The `Pasting` event is triggered before data is pasted into the spreadsheet. This event allows inspection and validation of the paste operation before it is completed, with options to modify or cancel the operation entirely. + +**Purpose** - public SfSpreadsheet SpreadsheetRef { get; set; } +This event is applicable in scenarios that require control over paste operations, such as validating data before allowing paste actions, restricting paste functionality to specific worksheets or cell ranges, applying data transformation rules during paste execution, and enforcing data integrity through oversight of paste operations. + +**Event Arguments** + +The `PastingEventArgs` includes the following properties: + +| Event Arguments | Description | +|----------------|-------------| +| ExternalClipboardData | An array of strings containing raw text data from external sources (like Excel or Google Sheets), with each element representing a row of data. Set to **null** when copying from within the workbook. | +| CopiedRange | A string in the format **"SheetName!Range"** (e.g., **"Sheet1!A1:A10"**) representing the source location of the copied or cut content. Set to **null** when pasting external content. | +| PasteRange | A string in the format **"SheetName!Range"** specifying the target cell range where content will be pasted. | +| Cancel | A boolean value that can be set to **true** to prevent the paste operation, allowing event handlers to control the paste behavior. The default value is **false**. | + +{% tabs %} +{% highlight razor tabtitle="Index.razor" %} + +@using Syncfusion.Blazor.Spreadsheet + + + + + + +@code { + public byte[] DataSourceBytes { get; set; } protected override void OnInitialized() { @@ -146,28 +603,26 @@ The following example illustrates how to prevent the paste action in the Spreads DataSourceBytes = File.ReadAllBytes(filePath); } - private async Task ItemSelected(MenuEventArgs args) + public void OnPasting(PastingEventArgs args) { - if (args.Item.Text == "Cut") + // Cancels the paste operation if the target range includes "A1:B5". + if (args.PasteRange.Contains("A1:B5")) { - await SpreadsheetRef.CutCellAsync(); + args.Cancel = true; } - if (args.Item.Text == "Copy") + // Checks external clipboard data for restricted content. + if (args.ExternalClipboardData != null) { - await SpreadsheetRef.CopyCellAsync(); + foreach (var line in args.ExternalClipboardData) + { + if (line.Contains("Confidential")) + { + args.Cancel = true; + break; + } + } } - - if (args.Item.Text == "Paste") - { - await SpreadsheetRef.PasteCellAsync(); - } - } - - public void OnBeforePasteHandler(PastingEventArgs args) - { - // To cancel the paste action. - args.Cancel = true; } } diff --git a/blazor/spreadsheet/images/contextmenu-cut-copy.png b/blazor/spreadsheet/images/contextmenu-cut-copy.png new file mode 100644 index 0000000000..7ec37449a4 Binary files /dev/null and b/blazor/spreadsheet/images/contextmenu-cut-copy.png differ diff --git a/blazor/spreadsheet/images/contextmenu-paste.png b/blazor/spreadsheet/images/contextmenu-paste.png new file mode 100644 index 0000000000..539532155a Binary files /dev/null and b/blazor/spreadsheet/images/contextmenu-paste.png differ diff --git a/blazor/spreadsheet/images/cut-copy.png b/blazor/spreadsheet/images/cut-copy.png new file mode 100644 index 0000000000..b06b8c0e2f Binary files /dev/null and b/blazor/spreadsheet/images/cut-copy.png differ diff --git a/blazor/spreadsheet/images/paste.png b/blazor/spreadsheet/images/paste.png new file mode 100644 index 0000000000..4308a6e0cb Binary files /dev/null and b/blazor/spreadsheet/images/paste.png differ