Search results

Clipboard in Angular Spreadsheet component

15 Jun 2021 / 4 minutes to read

The Spreadsheet provides support for the clipboard operations (cut, copy, and paste). Clipboard operations can be enabled or disabled by setting the enableClipboard property in Spreadsheet.

By default, the enableClipboard property is true.

Cut

It is used to cut the data from selected range of cells, rows or columns in a spreadsheet and make it available in the clipboard.

User Interface:

Cut can be done in one of the following ways.

  • Using Cut button in the Ribbon’s HOME tab to perform cut operation.
  • Using Cut option in the Context Menu.
  • Using Ctrl + X | Command + X keyboard shortcut.
  • Using the cut method.

Copy

It is used to copy the data from selected range of cells, rows or columns in a spreadsheet and make it available in the clipboard.

User Interface:

Copy can be done in one of the following ways.

  • Using Copy button in the Ribbon’s HOME tab to perform copy operation.
  • Using Copy option in the Context Menu.
  • Using Ctrl + C | Command + C keyboard shortcut.
  • Using the copy method.

Paste

It is used to paste the clipboard data to the selected range, rows or columns. You have the following options in Paste,

  • Paste Special - You can paste the values with formatting.
  • Paste - You can paste only the values without formatting.

It also performs for external clipboard operation. If you perform cut and paste, clipboard data will be cleared, whereas in copy and paste the clipboard contents will be maintained. If you perform paste inside the copied range, the clipboard data will be cleared.

User Interface:

Paste can be done in one of the following ways.

  • Using Paste button in the Ribbon’s HOME tab to perform paste operation.
  • Using Paste option in the Context Menu.
  • Using Ctrl + V | Command + V keyboard shortcut.
  • Using the paste method.

If you use the Keyboard shortcut key for cut (Ctrl + X) | copy (Ctrl + C) from other sources, you should use Ctrl + V shortcut while pasting into the spreadsheet.

Source
Preview
app.component.ts
app.module.ts
datasource.ts
main.ts
Copied to clipboard
import { Component, ViewChild } from '@angular/core';
import { SpreadsheetComponent } from '@syncfusion/ej2-angular-spreadsheet';
import { enableRipple } from '@syncfusion/ej2-base';
import { dataSource1 } from './datasource';

enableRipple(true);

@Component({
selector: 'app-container',
template: `<button ejs-dropdownbutton [items]='items' content='Clipboard' (select)='itemSelect($event)'></button>
<ejs-spreadsheet #spreadsheet (created)="created()" [enableClipboard]="true">
            <e-sheets>
              <e-sheet name="Price Details">
                <e-ranges>
                  <e-range [dataSource]="priceData"></e-range>
                </e-ranges>
                <e-columns>
                  <e-column [width]=130></e-column>
                  <e-column [width]=92></e-column>
                  <e-column [width]=96></e-column>
                </e-columns>
              </e-sheet>
            </e-sheets>
          </ejs-spreadsheet>`
})
export class AppComponent {
@ViewChild('spreadsheet')
spreadsheetObj: SpreadsheetComponent;

priceData: object[] = dataSource1;
public items: ItemModel[] = [
    {
      text: "Copy"
    },
    {
      text: "Cut"
    },
    {
      text: "Paste"
    }];

created() {
    this.spreadsheetObj.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'A1:H1');
}
public itemSelect(args: MenuEventArgs) {
if (args.item.text === 'Copy')
  spreadsheet.copy();
if (args.item.text === 'Cut')
  spreadsheet.cut();
if (args.item.text === 'Paste')
  spreadsheet.paste();
  }
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { SpreadsheetAllModule } from '@syncfusion/ej2-angular-spreadsheet';
import { AppComponent } from './app.component';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        SpreadsheetAllModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
Copied to clipboard
/**
 * Find and replace data sources
 */
export let dataSource1: Object[] = [
  {
      'Item Name': 'Casual Shoes',
      'Date': '02/14/2019',
      'Time': '11:34:32 AM',
      'Quantity': 10,
      'Price': 20,
      'Amount': '=D2*E2',
      'Discount': 1,
      'Profit': 10
  },
  {
      'Item Name': 'Sports Shoes',
      'Date': '06/11/2019',
      'Time': '05:56:32 AM',
      'Quantity': 20,
      'Price': 30,
      'Amount': '=D3*E3',
      'Discount': 5,
      'Profit': 50
  },
  {
      'Item Name': 'Formal Shoes',
      'Date': '07/27/2019',
      'Time': '03:32:44 AM',
      'Quantity': 20,
      'Price': 15,
      'Amount': '=D4*E4',
      'Discount': 7,
      'Profit': 27
  },
  {
      'Item Name': 'Sandals & Floaters',
      'Date': '11/21/2019',
      'Time': '06:23:54 AM',
      'Quantity': 15,
      'Price': 20,
      'Amount': '=D5*E5',
      'Discount': 11,
      'Profit': 67
  },
  {
      'Item Name': 'Flip- Flops & Slippers',
      'Date': '06/23/2019',
      'Time': '12:43:59 AM',
      'Quantity': 30,
      'Price': 10,
      'Amount': '=D6*E6',
      'Discount': 10,
      'Profit': 70
  },
  {
      'Item Name': 'Sneakers',
      'Date': '07/22/2019',
      'Time': '10:55:53 AM',
      'Quantity': 40,
      'Price': 20,
      'Amount': '=D7*E7',
      'Discount': 13,
      'Profit': 66
  },
  {
      'Item Name': 'Running Shoes',
      'Date': '02/04/2019',
      'Time': '03:44:34 AM',
      'Quantity': 20,
      'Price': 10,
      'Amount': '=D8*E8',
      'Discount': 3,
      'Profit': 14
  },
  {
      'Item Name': 'Loafers',
      'Date': '11/30/2019',
      'Time': '03:12:52 AM',
      'Quantity': 31,
      'Price': 10,
      'Amount': '=D9*E9',
      'Discount': 6,
      'Profit': 29
  },
  {
      'Item Name': 'Cricket Shoes',
      'Date': '07/09/2019',
      'Time': '11:32:14 AM',
      'Quantity': 41,
      'Price': 30,
      'Amount': '=D10*E10',
      'Discount': 12,
      'Profit': 166
  },
  {
      'Item Name': 'T-Shirts',
      'Date': '10/31/2019',
      'Time': '12:01:44 AM',
      'Quantity': 50,
      'Price': 10,
      'Amount': '=D11*E11',
      'Discount': 9,
      'Profit': 55
  }
];

export let dataSource2: Object[] = [
  {
    'Expense Type': 'Housing',
    'Projected Cost': 7000,
    'Actual Cost': 7500,
    'Difference': -500,
  },
  {
    'Expense Type': 'Transportation',
    'Projected Cost': 500,
    'Actual Cost': 500,
    'Difference': 0,
  },
  {
    'Expense Type': 'Insurance',
    'Projected Cost': 1000,
    'Actual Cost': 1000,
    'Difference': 0,
  },
  {
    'Expense Type': 'Food',
    'Projected Cost': 2000,
    'Actual Cost': 1800,
    'Difference': 200,
  },
  {
    'Expense Type': 'Pets',
    'Projected Cost': 300,
    'Actual Cost': 200,
    'Difference': 100,
  },
  {
    'Expense Type': 'Personel Care',
    'Projected Cost': 500,
    'Actual Cost': 500,
    'Difference': 0,
  },
  {
    'Expense Type': 'Loan',
    'Projected Cost': 1000,
    'Actual Cost': 1000,
    'Difference': 0,
  },
  {
    'Expense Type': 'Tax',
    'Projected Cost': 200,
    'Actual Cost': 200,
    'Difference': 0,
  },
  {
    'Expense Type': 'Savings',
    'Projected Cost': 1000,
    'Actual Cost': 900,
    'Difference': 100,
  },
  {
    'Expense Type': 'Total',
    'Projected Cost': 13500,
    'Actual Cost': 13600,
    'Difference': -100,
  }
];
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

Prevent the paste functionality

The following example shows, how to prevent the paste action in spreadsheet. In actionBegin event, you can set cancel argument as false in paste request type.

Source
Preview
app.component.ts
app.module.ts
datasource.ts
main.ts
Copied to clipboard
import { Component, ViewChild } from '@angular/core';
import { SpreadsheetComponent } from '@syncfusion/ej2-angular-spreadsheet';
import { enableRipple } from '@syncfusion/ej2-base';
import { dataSource1 } from './datasource';

enableRipple(true);

@Component({
selector: 'app-container',
template: `<button ejs-dropdownbutton [items]='items' content='Clipboard' (select)='itemSelect($event)'></button>
<ejs-spreadsheet #spreadsheet (created)="created()" (actionBegin)="actionBeginHandler()" [enableClipboard]="true">
            <e-sheets>
              <e-sheet name="Price Details">
                <e-ranges>
                  <e-range [dataSource]="priceData"></e-range>
                </e-ranges>
                <e-columns>
                  <e-column [width]=130></e-column>
                  <e-column [width]=92></e-column>
                  <e-column [width]=96></e-column>
                </e-columns>
              </e-sheet>
            </e-sheets>
          </ejs-spreadsheet>`
})
export class AppComponent {
@ViewChild('spreadsheet')
spreadsheetObj: SpreadsheetComponent;

priceData: object[] = dataSource1;
public items: ItemModel[] = [
    {
      text: "Copy"
    },
    {
      text: "Cut"
    },
    {
      text: "Paste"
    }];

created() {
    this.spreadsheetObj.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'A1:H1');
}
// Triggers before the action begins.
actionBeginHandler(pasteArgs) {
  // To cancel the paste action.
    if (pasteArgs.args.eventArgs.requestType === 'paste') {
        pasteArgs.args.eventArgs.cancel = true;
    }
}
public itemSelect(args: MenuEventArgs) {
if (args.item.text === 'Copy')
  spreadsheet.copy();
if (args.item.text === 'Cut')
  spreadsheet.cut();
if (args.item.text === 'Paste')
  spreadsheet.paste();
  }
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { SpreadsheetAllModule } from '@syncfusion/ej2-angular-spreadsheet';
import { AppComponent } from './app.component';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        SpreadsheetAllModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
Copied to clipboard
/**
 * Find and replace data sources
 */
export let dataSource1: Object[] = [
  {
      'Item Name': 'Casual Shoes',
      'Date': '02/14/2019',
      'Time': '11:34:32 AM',
      'Quantity': 10,
      'Price': 20,
      'Amount': '=D2*E2',
      'Discount': 1,
      'Profit': 10
  },
  {
      'Item Name': 'Sports Shoes',
      'Date': '06/11/2019',
      'Time': '05:56:32 AM',
      'Quantity': 20,
      'Price': 30,
      'Amount': '=D3*E3',
      'Discount': 5,
      'Profit': 50
  },
  {
      'Item Name': 'Formal Shoes',
      'Date': '07/27/2019',
      'Time': '03:32:44 AM',
      'Quantity': 20,
      'Price': 15,
      'Amount': '=D4*E4',
      'Discount': 7,
      'Profit': 27
  },
  {
      'Item Name': 'Sandals & Floaters',
      'Date': '11/21/2019',
      'Time': '06:23:54 AM',
      'Quantity': 15,
      'Price': 20,
      'Amount': '=D5*E5',
      'Discount': 11,
      'Profit': 67
  },
  {
      'Item Name': 'Flip- Flops & Slippers',
      'Date': '06/23/2019',
      'Time': '12:43:59 AM',
      'Quantity': 30,
      'Price': 10,
      'Amount': '=D6*E6',
      'Discount': 10,
      'Profit': 70
  },
  {
      'Item Name': 'Sneakers',
      'Date': '07/22/2019',
      'Time': '10:55:53 AM',
      'Quantity': 40,
      'Price': 20,
      'Amount': '=D7*E7',
      'Discount': 13,
      'Profit': 66
  },
  {
      'Item Name': 'Running Shoes',
      'Date': '02/04/2019',
      'Time': '03:44:34 AM',
      'Quantity': 20,
      'Price': 10,
      'Amount': '=D8*E8',
      'Discount': 3,
      'Profit': 14
  },
  {
      'Item Name': 'Loafers',
      'Date': '11/30/2019',
      'Time': '03:12:52 AM',
      'Quantity': 31,
      'Price': 10,
      'Amount': '=D9*E9',
      'Discount': 6,
      'Profit': 29
  },
  {
      'Item Name': 'Cricket Shoes',
      'Date': '07/09/2019',
      'Time': '11:32:14 AM',
      'Quantity': 41,
      'Price': 30,
      'Amount': '=D10*E10',
      'Discount': 12,
      'Profit': 166
  },
  {
      'Item Name': 'T-Shirts',
      'Date': '10/31/2019',
      'Time': '12:01:44 AM',
      'Quantity': 50,
      'Price': 10,
      'Amount': '=D11*E11',
      'Discount': 9,
      'Profit': 55
  }
];

export let dataSource2: Object[] = [
  {
    'Expense Type': 'Housing',
    'Projected Cost': 7000,
    'Actual Cost': 7500,
    'Difference': -500,
  },
  {
    'Expense Type': 'Transportation',
    'Projected Cost': 500,
    'Actual Cost': 500,
    'Difference': 0,
  },
  {
    'Expense Type': 'Insurance',
    'Projected Cost': 1000,
    'Actual Cost': 1000,
    'Difference': 0,
  },
  {
    'Expense Type': 'Food',
    'Projected Cost': 2000,
    'Actual Cost': 1800,
    'Difference': 200,
  },
  {
    'Expense Type': 'Pets',
    'Projected Cost': 300,
    'Actual Cost': 200,
    'Difference': 100,
  },
  {
    'Expense Type': 'Personel Care',
    'Projected Cost': 500,
    'Actual Cost': 500,
    'Difference': 0,
  },
  {
    'Expense Type': 'Loan',
    'Projected Cost': 1000,
    'Actual Cost': 1000,
    'Difference': 0,
  },
  {
    'Expense Type': 'Tax',
    'Projected Cost': 200,
    'Actual Cost': 200,
    'Difference': 0,
  },
  {
    'Expense Type': 'Savings',
    'Projected Cost': 1000,
    'Actual Cost': 900,
    'Difference': 100,
  },
  {
    'Expense Type': 'Total',
    'Projected Cost': 13500,
    'Actual Cost': 13600,
    'Difference': -100,
  }
];
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

Limitations

  • External clipboard is not fully supported while copying data from another source and pasting into a spreadsheet, it only works with basic supports (Values, Number, cell, and Text formatting).
  • If you copy =SUM(A2,B2) and paste, the formula reference will change depending on the pasted cell address but we don’t have support for nested formula(formula reference will be same).
  • Clipboard is not supported with conditional formatting (values only pasting).
  • We have limitation while copying the whole sheet data and pasting it into another sheet.