Cut, Copy, Paste"

From Documentation
Line 10: Line 10:
 
src.paste(destination);
 
src.paste(destination);
 
</source>
 
</source>
There is also a <tt>pasteSpecial()</tt> to do special pasting like pasting value only or pasting formula only. Similarly, the steps to cut a range is to copy them first, after pasting to destination, just clear the source range's content.
+
Similarly, the steps to cut a range is to copy them first, after pasting to destination, just clear the source range's content.
  
 
Cutting a range of cells is similar to copy:
 
Cutting a range of cells is similar to copy:
Line 18: Line 18:
 
src.paste(destination, true);
 
src.paste(destination, true);
 
</source>
 
</source>
 +
There is also a <tt>pasteSpecial()</tt> to do special pasting like pasting value only or pasting formula only.
  
 
+
With the help of <javadoc directory="zss">org.zkoss.zss.api.CellOperationUtil</javadoc>, we can easily perform copying and cutting, and it also provides methods for "paste special" such as <tt>pasteValue()</tt>, or <tt>pasteFormula()</tt>. These methods all require 2 <tt>Range</tt> objects as arguments. One is source and another is destination.
With the help of <javadoc directory="zss">org.zkoss.zss.api.CellOperationUtil</javadoc>, we can easily perform some user operations like copy and cut. and it also provides methods for "paste special" such as <tt>pasteValue()</tt>, or <tt>pasteFormula()</tt>. These methods all require 2 <tt>Range</tt> objects as arguments. One is source and another is destination.
 
  
 
To '''cut''' a range of cells, you should use
 
To '''cut''' a range of cells, you should use

Revision as of 06:16, 2 April 2014



Overview

To copy a range of cells, you should use Ranges to select them and call paste(Range) with another Range object for destination like:

		Range src = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
		Range destination = Ranges.range(getDestinationSheet(), ss.getSelection());
		src.paste(destination);

Similarly, the steps to cut a range is to copy them first, after pasting to destination, just clear the source range's content.

Cutting a range of cells is similar to copy:

		Range src = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
		Range destination = Ranges.range(getDestinationSheet(), ss.getSelection());
		src.paste(destination, true);

There is also a pasteSpecial() to do special pasting like pasting value only or pasting formula only.

With the help of CellOperationUtil, we can easily perform copying and cutting, and it also provides methods for "paste special" such as pasteValue(), or pasteFormula(). These methods all require 2 Range objects as arguments. One is source and another is destination.

To cut a range of cells, you should use

CellOperationUtil.cut(srcRange, destRange);

To copy a range of cells, you should use

CellOperationUtil.paste(srcRange, destRange);

The usages for pasteFormula(), pasteValue(), pasteTranspose(), and pasteAllExceptBorder() are all the same.

Example

The following codes copy a selection range to the same position of another sheet when a user clicks a button.

public class CopyCutComposer extends SelectorComposer<Component> {

	@Wire
	private Spreadsheet ss;


	@Listen("onClick = #copyButton")
	public void copyByUtil() {
		Range src = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
		Range dest = Ranges.range(getResultSheet(), ss.getSelection());
		CellOperationUtil.paste(src, dest);
	}
	
	//omitted codes...
}



All source code listed in this book is at Github.


Last Update : 2014/04/02

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.