|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Clipboard in Blazor Spreadsheet component | Syncfusion |
| 4 | +description: Checkout and learn here all about the clipboard functionalities in the Syncfusion Blazor Spreadsheet component and more. |
| 5 | +platform: Blazor |
| 6 | +control: Spreadsheet |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# Clipboard in Blazor Spreadsheet component |
| 11 | + |
| 12 | +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**. |
| 13 | + |
| 14 | +## Cut |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +**Cutting Data in Spreadsheet** |
| 19 | + |
| 20 | +The cut operation can be performed through the following methods: |
| 21 | + |
| 22 | +* Select the **Cut** button in the **HOME** tab of the Ribbon toolbar to execute the cut operation. |
| 23 | +* Right-click and select the **Cut** option from the context menu. |
| 24 | +* Press the keyboard shortcut `Ctrl + X`(Windows) or `Command + X`(Mac). |
| 25 | +* Using the [`CutCellAsync`](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_CutCellAsync_System_String_) method. |
| 26 | + |
| 27 | +## Copy |
| 28 | + |
| 29 | +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. |
| 30 | + |
| 31 | +**Copying Data in Spreadsheet** |
| 32 | + |
| 33 | +The copy operation can be performed through the following methods: |
| 34 | + |
| 35 | +* Select the **Copy** button in the **HOME** tab of the Ribbon toolbar to execute the copy operation. |
| 36 | +* Right-click and select the **Copy** option from the context menu. |
| 37 | +* Press the keyboard shortcut `Ctrl + C` (Windows) or `Command + C` (Mac). |
| 38 | +* Using the [`CopyCellAsync`](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_CopyCellAsync_System_String_) method. |
| 39 | + |
| 40 | +## Paste |
| 41 | + |
| 42 | +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. |
| 43 | + |
| 44 | +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. |
| 45 | + |
| 46 | +**Pasting Data in Spreadsheet**: |
| 47 | + |
| 48 | +The paste operation can be performed through the following methods: |
| 49 | + |
| 50 | +* Select the **Paste** button in the **HOME** tab of the Ribbon toolbar to execute the paste operation. |
| 51 | +* Right-click and select the **Paste** option from the context menu. |
| 52 | +* Press the keyboard shortcut `Ctrl + V` (Windows) or `Command + V` (Mac). |
| 53 | +* Using the [`PasteCellAsync`](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_PasteCellAsync_System_String_) method. |
| 54 | + |
| 55 | +> 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. |
| 56 | +
|
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +{% tabs %} |
| 62 | +{% highlight razor tabtitle="Index.razor" %} |
| 63 | + |
| 64 | +@using Syncfusion.Blazor.Spreadsheet |
| 65 | +@using Syncfusion.Blazor.SplitButtons |
| 66 | + |
| 67 | +<SfDropDownButton Content="Clipboard"> |
| 68 | + <DropDownMenuItems> |
| 69 | + <DropDownMenuItem Text="Copy"></DropDownMenuItem> |
| 70 | + <DropDownMenuItem Text="Cut"></DropDownMenuItem> |
| 71 | + <DropDownMenuItem Text="Paste"></DropDownMenuItem> |
| 72 | + </DropDownMenuItems> |
| 73 | + <DropDownButtonEvents ItemSelected="ItemSelected"> |
| 74 | + </DropDownButtonEvents> |
| 75 | +</SfDropDownButton> |
| 76 | + |
| 77 | +<SfSpreadsheet @ref="SpreadsheetRef" DataSource="DataSourceBytes"> |
| 78 | + <SpreadsheetRibbon></SpreadsheetRibbon> |
| 79 | +</SfSpreadsheet> |
| 80 | + |
| 81 | +@code { |
| 82 | + public byte[] DataSourceBytes { get; set; } |
| 83 | + public SfSpreadsheet SpreadsheetRef { get; set; } |
| 84 | + |
| 85 | + protected override void OnInitialized() |
| 86 | + { |
| 87 | + string filePath = "wwwroot/Sample.xlsx"; |
| 88 | + DataSourceBytes = File.ReadAllBytes(filePath); |
| 89 | + } |
| 90 | + |
| 91 | + private async Task ItemSelected(MenuEventArgs args) |
| 92 | + { |
| 93 | + if (args.Item.Text == "Cut") |
| 94 | + { |
| 95 | + await SpreadsheetRef.CutCellAsync(); |
| 96 | + } |
| 97 | + |
| 98 | + if (args.Item.Text == "Copy") |
| 99 | + { |
| 100 | + await SpreadsheetRef.CopyCellAsync(); |
| 101 | + } |
| 102 | + |
| 103 | + if (args.Item.Text == "Paste") |
| 104 | + { |
| 105 | + await SpreadsheetRef.PasteCellAsync(); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +{% endhighlight %} |
| 111 | +{% endtabs %} |
| 112 | + |
| 113 | +### Prevent pasting |
| 114 | + |
| 115 | +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. |
| 116 | + |
| 117 | +{% tabs %} |
| 118 | +{% highlight razor tabtitle="Index.razor" %} |
| 119 | + |
| 120 | +@using Syncfusion.Blazor.Spreadsheet |
| 121 | +@using Syncfusion.Blazor.SplitButtons |
| 122 | + |
| 123 | +<SfDropDownButton Content="Clipboard"> |
| 124 | + <DropDownMenuItems> |
| 125 | + <DropDownMenuItem Text="Copy"></DropDownMenuItem> |
| 126 | + <DropDownMenuItem Text="Cut"></DropDownMenuItem> |
| 127 | + <DropDownMenuItem Text="Paste"></DropDownMenuItem> |
| 128 | + </DropDownMenuItems> |
| 129 | + <DropDownButtonEvents ItemSelected="ItemSelected"> |
| 130 | + </DropDownButtonEvents> |
| 131 | +</SfDropDownButton> |
| 132 | + |
| 133 | +<SfSpreadsheet @ref="SpreadsheetRef" DataSource="DataSourceBytes"> |
| 134 | + <SpreadsheetRibbon></SpreadsheetRibbon> |
| 135 | + <SpreadsheetEvents Pasting="OnBeforePasteHandler"></SpreadsheetEvents> |
| 136 | +</SfSpreadsheet> |
| 137 | + |
| 138 | +@code { |
| 139 | + public byte[] DataSourceBytes { get; set; } |
| 140 | + |
| 141 | + public SfSpreadsheet SpreadsheetRef { get; set; } |
| 142 | + |
| 143 | + protected override void OnInitialized() |
| 144 | + { |
| 145 | + string filePath = "wwwroot/Sample.xlsx"; |
| 146 | + DataSourceBytes = File.ReadAllBytes(filePath); |
| 147 | + } |
| 148 | + |
| 149 | + private async Task ItemSelected(MenuEventArgs args) |
| 150 | + { |
| 151 | + if (args.Item.Text == "Cut") |
| 152 | + { |
| 153 | + await SpreadsheetRef.CutCellAsync(); |
| 154 | + } |
| 155 | + |
| 156 | + if (args.Item.Text == "Copy") |
| 157 | + { |
| 158 | + await SpreadsheetRef.CopyCellAsync(); |
| 159 | + } |
| 160 | + |
| 161 | + if (args.Item.Text == "Paste") |
| 162 | + { |
| 163 | + await SpreadsheetRef.PasteCellAsync(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + public void OnBeforePasteHandler(PastingEventArgs args) |
| 168 | + { |
| 169 | + // To cancel the paste action. |
| 170 | + args.Cancel = true; |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +{% endhighlight %} |
| 175 | +{% endtabs %} |
0 commit comments