Clear just data from Web Excel worksheet with formulas using Office Script

dgold

New Member
Joined
Feb 3, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I am trying to clear all data from a worksheet in a web version of Excel. The VBA code I use on my desktop version is
Sub sbClearEntireSheetOnlyData()
Sheets("Sheet4").Cells.ClearContents
End Sub
Can someone "translate" this to Office Scripts? Thanks in advance.
Dave
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I think you want to look to Range.Clear(applyTo). Using the example from that link, it was pretty easy to translate it to your requirement:
JavaScript:
function main(workbook: ExcelScript.Workbook) {
    let range = workbook.getWorksheet("Sheet4").getRange();
    range.clear(ExcelScript.ClearApplyTo.contents);
}
 
Upvote 0
For some reason, this script works to clear everything including the formulas. Is that expected?
 
Upvote 0
OK, thanks. I'm assuming that there isn't an easy way just to clear the data and retain the formulas. I guess one option would be to have a blank template and automate duplication and deleting.
 
Upvote 0
I'm assuming that there isn't an easy way just to clear the data and retain the formulas
That depends if you know the data you want to clear will be in predictable locations each time (i.e. if there is some sort of template or table users are filling in). If there will be consistent locations with values, you can look at clearing only those ranges instead of the entire sheet.

If you don't expect consistency of where you are going to find the values, you could still loop over every cell in the workbook to check if it has a formula. However, for obvious reasons that is likely not going to be a performant solution.
 
Upvote 0

Forum statistics

Threads
1,223,362
Messages
6,171,642
Members
452,415
Latest member
mansoorali

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top