Hi there !
My version information :
Version: 25.2.4.3 (X86_64) / LibreOffice Community
Build ID: 33e196637044ead23f5c3226cde09b47731f7e27
CPU threads: 8; OS: Windows 10 X86_64 (10.0 build 19045); UI render: Skia/Vulkan; VCL: win
Locale: fr-FR (fr_FR); UI: en-US
Calc: CL threaded
The context :
I need to clear a a few hundreds of cells at once, across a dozen sheets, by clicking a button.
I thus wrote a macro and associated it to the “button pressed” event.
I tested two ways of programming the macro and roughly timed both of them.
I think it doesn’t matter to me if the cells are filled with 0 or “”, or if they are just cleared out.
Below are examples of the two ways I used.
-
Relying upon the clearContents method
ThisComponent.Sheets.getbyname(SheetName.string).getCellRangeByName(“Cell1:Cell3”).clearcontents(1)
ThisComponent.Sheets.getbyname(SheetName.string).getCellRangeByName(“Cell4:Cell6”).clearcontents(4) -
Relying upon the setvalue/setstring methods
ThisComponent.Sheets.getbyname(SheetName.string).getCellRangeByName(“Cell1”).setvalue(0)
ThisComponent.Sheets.getbyname(SheetName.string).getCellRangeByName(“Cell2”).setvalue(0)
ThisComponent.Sheets.getbyname(SheetName.string).getCellRangeByName(“Cell3”).setvalue(0)
ThisComponent.Sheets.getbyname(SheetName.string).getCellRangeByName(“Cell4”).setstring("")
ThisComponent.Sheets.getbyname(SheetName.string).getCellRangeByName(“Cell5”).setstring("")
ThisComponent.Sheets.getbyname(SheetName.string).getCellRangeByName(“Cell6”).setstring("")
With my original file, the clearcontents method takes way more time than the setvalue/setstring method.
Futhermore, the more cells are filled at the beginning, the more the difference between those to methods seems significant.
So my question is : What would be the most efficient way to clear a great number of cells at once ?
(Be it in terms of cpu usage and/or timewise.)
Edit (2025-07-24T09:19:00Z):
Ok, here are the latest developments.
I think it’s still relevant to talk about that here, but I’m sorry if I’m wrong !
A) An inconclusive testing
I spent a good deal of time setting up a file to test the clearing speed of various methods…
I filled several cells and cell ranges with letters, among 18 sheets.
There was a total of approximately 4000 cells to clear.
I used 3 methods :
clearContents(7)
clearContents(4)
setstring("")
… Only to find out that the clearing was instantaneous, regardless of the method used.
B) The obvious question
Why would it take several seconds (6 to 20) to clear out less than 1000 cells in my original file, when 4000 cells are instantaneously cleard out in my test file ?
Unfortunately, I can’t share my orignal file, it’s kinda private. However, I made a template of how cells are distributed in it.
Moreover, the differences between original and test files are listed below.
Whereas the test file is only filled with letters, the original file contains :
- formulas, including references between sheets,
- drop down lists (based on Data Validation),
- 2 check boxes,
- conditional and direct formatting (which shouldn’t have any effect since I’m only using clearContents(4) and (1), not taking the format into account ?)
C) The available elements
The template mimicking my original file : “Original File Template”
The pieces of code I’m using to clear out the original file, and comparing the clearing speed between clearContents(1)/(4) and setvalue/setstring : “Clearing Cells - clearContents” and “Clearing Cells - setvalue+setstring”
(All variables are declared at the beginning of the macro, and initialised in another Sub.)
For my original file, the setvalue/setstring method is still way faster than the clearContents method.
I hope it is enough for you connoisseurs to go on…
Original File Template.ods (44.3 KB)
Clearing Cells - clearContents.odt (36.1 KB)
Clearing Cells - setvalue+setstring.odt (38.4 KB)