We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Clear range contents in Aspose.Cells

Hi team,
I am using Aspose.cells v17.11. I was trying to convert code from Excel interop to Aspose.Cells. I want to clear contents from the range.

So I have method in Interop named “ClearRange(Range range)”, which clears content from range with interop method “range.ClearContents()”. But I cannot find any methods for Range class in Aspose.Cells.

Please provide some inputs.

@ganaisatish14

Thanks for your posting and using Aspose APIs.

Please use the Worksheet.Cells.ClearContents() methods for your needs. It has two overloads. One, takes CellArea as a parameter and other takes integer parameters.

@shakeel.faiz How to clear content from worksheets using python

@asposeLegit
Please use https://reference.aspose.com/cells/python-java/asposecells.api/cells#clearContents(com.aspose.cells.CellArea) method.
We will enhance documents for Python, and now please check documents https://docs.aspose.com/cells/java if you are using Python via Java.

sorry @simon.zhao but i am not able to implement this even after going through doc can you please help me out.

@asposeLegit
import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook, Cells


cells = wb.getWorksheets().get(0).getCells()
cells. clearContents(0, 0, 100, 10)

Hello @simon.zhao

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import *

wb = Workbook(“file with path”)

worksheet = wb.getWorksheets().get(0)
cells = worksheet.getCells()

#getting range from workbook from which i have to delete content
maxr = cells.getMaxDisplayRange().getRefersTo()

#clearing content of sheet on the basis of this range but this giving me error
cells.clearContents(maxr)

I am trying above code using which i am clearing content of sheet on the basis of the range but i am not able to delete. So can you please help me with this?

@asposeLegit,

In clearContents() method, the parameter “range” refers to CellArea object. So, it is rightly giving error because you are supposed to put CellArea instead of string (range) of cells for clearContents method.

See the method description/details:

clearContents(range)

Clears contents of a range.

Parameters:

range: CellArea - Range to be cleared.

Please define CellArea based on your desired range of cells in code and then input that area (as parameter) for clearContents() method.

Hello @Amjad_Sahi,

actually my problem is i have to clear content of multiple sheets from multiple workbooks according to the range of the data present on the sheet, I can’t define it in a static way, I have to get the range of data of worksheet on which data is present and after getting that range i have to delete that content And i can’t define range manually because content of the file sometime is too large and sometime is small and i have to clear complete content of worksheet not a specific cell.

Thanks

@asposeLegit,

It is easy for you to define the CellArea according to the display range dynamically:

ca = CellArea()
ca.StartRow = maxr.getFirstRow()
ca.EndRow = ca.StartRow + maxr.getRowCount() - 1;
ca.StartColumn = maxr.getFirstColumn()
ca.EndColumn = ca.StartColumn + maxr.getColumnCount() - 1;
cells.clearContents(ca)