Cut, Copy, Paste"

From Documentation
m (correct highlight (via JWB))
 
Line 4: Line 4:
  
 
=Overview=  
 
=Overview=  
To copy a range of cells, you should use <javadoc directory="zss">org.zkoss.zss.api.Ranges</javadoc> to select them and call <tt>paste(Range)</tt> with another <tt>Range</tt> object for destination like:
+
To copy a range of cells, you should use <javadoc directory="zss">org.zkoss.zss.api.Ranges</javadoc> to select them and call <code>paste(Range)</code> with another <code>Range</code> object for destination like:
<source lang='java' high='3'>
+
<source lang='java' highlight='3'>
 
Range src = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
 
Range src = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
 
Range destination = Ranges.range(getDestinationSheet(), ss.getSelection());
 
Range destination = Ranges.range(getDestinationSheet(), ss.getSelection());
Line 13: Line 13:
  
 
Cutting a range of cells is similar to copy:
 
Cutting a range of cells is similar to copy:
<source lang='java' high='3'>
+
<source lang='java' highlight='3'>
 
Range src = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
 
Range src = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
 
Range destination = Ranges.range(getDestinationSheet(), ss.getSelection());
 
Range destination = Ranges.range(getDestinationSheet(), ss.getSelection());
 
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.
+
There is also a <code>pasteSpecial()</code> 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 copying and cutting, and it also provides methods for "paste special" such as <code>pasteValue()</code>, or <code>pasteFormula()</code>. These methods all require 2 <code>Range</code> 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
Line 32: Line 32:
 
</source>
 
</source>
  
The usages for <tt>pasteFormula()</tt>, <tt>pasteValue()</tt>, <tt>pasteTranspose()</tt>, and <tt>pasteAllExceptBorder()</tt> are all the same.
+
The usages for <code>pasteFormula()</code>, <code>pasteValue()</code>, <code>pasteTranspose()</code>, and <code>pasteAllExceptBorder()</code> are all the same.
  
 
= Example =
 
= Example =
 
The following codes copy a selection range to the same position of another sheet when a user clicks a button.
 
The following codes copy a selection range to the same position of another sheet when a user clicks a button.
  
<source lang='java' high='11'>
+
<source lang='java' highlight='11'>
  
 
public class CopyCutComposer extends SelectorComposer<Component> {
 
public class CopyCutComposer extends SelectorComposer<Component> {

Latest revision as of 12:52, 19 January 2022



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 : 2022/01/19

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