Insertion and Deletion"

From Documentation
(Created page with "{{ZKSpreadsheetEssentials3PageHeader}} The <tt>Range</tt> API to insert cells. <source lang='java'> range.insert(InsertShift.RIGHT, InsertCopyOrigin.FORMAT_LEFT_ABOVE); </sou...")
 
m (correct highlight (via JWB))
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:
  
  
The <tt>Range</tt> API to insert cells.
+
The <code>Range</code> API to insert cells.
 
<source lang='java'>
 
<source lang='java'>
 
range.insert(InsertShift.RIGHT, InsertCopyOrigin.FORMAT_LEFT_ABOVE);
 
range.insert(InsertShift.RIGHT, InsertCopyOrigin.FORMAT_LEFT_ABOVE);
Line 9: Line 9:
 
</source>
 
</source>
  
In this usage, the <tt>Range</tt> object not only represents the position to insert cells but also how many cells to insert. For example, if you pass a Range that represents B1:C2, then <tt>insert()</tt> will insert 4 cells. The 2nd argument (<javadoc directory="zss">org.zkoss.zss.api.Range.InsertShift </javadoc>) controls the shift direction of original cells, and it could be <tt>DEFAULT</tt>, <tt>RIGHT</tt> or <tt>DOWN</tt>. The 3rd argument is of <javadoc directory="zss">org.zkoss.zss.api.Range.InsertCopyOrigin</javadoc> that determines from which cells inserted cell should copy styles. It could be <tt>FORMAT_LEFT_ABOVE, FORMAT_RIGHT_BELOW</tt>, or <tt>FORMAT_NONE</tt>.
+
In this usage, the <code>Range</code> object not only represents the position to insert cells but also how many cells to insert. For example, if you pass a Range that represents B1:C2, then <code>insert()</code> will insert 4 cells. The 2nd argument (<javadoc directory="zss">org.zkoss.zss.api.Range.InsertShift </javadoc>) controls the shift direction of original cells, and it could be <code>DEFAULT</code>, <code>RIGHT</code> or <code>DOWN</code>. The 3rd argument is of <javadoc directory="zss">org.zkoss.zss.api.Range.InsertCopyOrigin</javadoc> that determines from which cells inserted cell should copy styles. It could be <code>FORMAT_LEFT_ABOVE, FORMAT_RIGHT_BELOW</code>, or <code>FORMAT_NONE</code>.
  
 
To insert rows, the same method can be used, but you need to make your range become a row range as follows:
 
To insert rows, the same method can be used, but you need to make your range become a row range as follows:
Line 15: Line 15:
 
range = range.toRowRange();  
 
range = range.toRowRange();  
 
</source>
 
</source>
The same rule applies to columns with <tt>toColumnRange()</tt>.
+
The same rule applies to columns with <code>toColumnRange()</code>.
  
The method for deletion is relatively simple. Just specify how other cells shift after deletion with 2nd argument (<tt>DEFAULT, LEFT, UP</tt>):
+
The method for deletion is relatively simple. Just specify how other cells shift after deletion with 2nd argument (<code>DEFAULT, LEFT, UP</code>):
 
<source lang='java'>
 
<source lang='java'>
 
range.delete(DeleteShift.LEFT);
 
range.delete(DeleteShift.LEFT);
Line 37: Line 37:
  
 
If we want to insert 3 columns at current selection when a user click a button, we may write:
 
If we want to insert 3 columns at current selection when a user click a button, we may write:
<source lang='java' high='10,12, 13'>
+
<source lang='java' highlight='10,12, 13'>
  
 
@Listen("onClick = #insert3Columns")
 
@Listen("onClick = #insert3Columns")
 
public void onInsert3Columns(){
 
public void onInsert3Columns(){
Rect selection = ss.getSelection();
+
AreaRef selection = ss.getSelection();
 
//select the range that contains 3 column
 
//select the range that contains 3 column
 
Range range = Ranges.range(ss.getSelectedSheet(), selection.getRow(),
 
Range range = Ranges.range(ss.getSelectedSheet(), selection.getRow(),
Line 58: Line 58:
  
 
If we want to delete cells and shift other cells to the left, we may write:
 
If we want to delete cells and shift other cells to the left, we may write:
<source lang='java' high='8'>
+
<source lang='java' highlight='8'>
 
@Listen("onClick = #deleteCellLeft")
 
@Listen("onClick = #deleteCellLeft")
 
public void onDeleteCellLeft(){
 
public void onDeleteCellLeft(){
Rect selection = ss.getSelection();
+
AreaRef selection = ss.getSelection();
  
 
Range range = Ranges.range(ss.getSelectedSheet(), selection);
 
Range range = Ranges.range(ss.getSelectedSheet(), selection);

Latest revision as of 12:52, 19 January 2022



The Range API to insert cells.

range.insert(InsertShift.RIGHT, InsertCopyOrigin.FORMAT_LEFT_ABOVE);

In this usage, the Range object not only represents the position to insert cells but also how many cells to insert. For example, if you pass a Range that represents B1:C2, then insert() will insert 4 cells. The 2nd argument (Range.InsertShift) controls the shift direction of original cells, and it could be DEFAULT, RIGHT or DOWN. The 3rd argument is of Range.InsertCopyOrigin that determines from which cells inserted cell should copy styles. It could be FORMAT_LEFT_ABOVE, FORMAT_RIGHT_BELOW, or FORMAT_NONE.

To insert rows, the same method can be used, but you need to make your range become a row range as follows:

range = range.toRowRange();

The same rule applies to columns with toColumnRange().

The method for deletion is relatively simple. Just specify how other cells shift after deletion with 2nd argument (DEFAULT, LEFT, UP):

range.delete(DeleteShift.LEFT);


CellOperationUtil also can help us to insert (or delete) cells, rows, and columns. To insert cells:

CellOperationUtil.insert(Range range, InsertShift shift, InsertCopyOrigin copyOrigin);

Delete:

CellOperationUtil.delete(Range range, DeleteShift shift)


If we want to insert 3 columns at current selection when a user click a button, we may write:

	@Listen("onClick = #insert3Columns")
	public void onInsert3Columns(){
		AreaRef selection = ss.getSelection();
		//select the range that contains 3 column
		Range range = Ranges.range(ss.getSelectedSheet(), selection.getRow(),
				selection.getColumn(), selection.getRow() ,
				selection.getColumn()+2);

		//select all columns
		range = range.toColumnRange(); 
		//shift existing row right and copy style from left cells 
		CellOperationUtil.insert(range, InsertShift.RIGHT, InsertCopyOrigin.FORMAT_LEFT_ABOVE);
		ss.setMaxVisibleColumns(ss.getMaxVisibleColumns()+3);
	}
  • Line 13: If last column contains data, remember to increase the number of max visible column or it will be shifted to invisible area. The same rule applies on inserting rows.


If we want to delete cells and shift other cells to the left, we may write:

	@Listen("onClick = #deleteCellLeft")
	public void onDeleteCellLeft(){
		AreaRef selection = ss.getSelection();

		Range range = Ranges.range(ss.getSelectedSheet(), selection);
		
		//move existing (right) cells left
		CellOperationUtil.delete(range, DeleteShift.LEFT);
	}



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.