How to Fix Office Script Destination Range Error in Excel
Question details
The user needs to resolve an Office Script error that occurs when moving data output to a new destination range.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Modifying an Office Script to output filtered data to a different location (e.g., from D2:E to V4:W) on the worksheet.
- Observed behavior
- The script fails because the new destination range does not exactly match the dimensions (rows and columns) of the filtered data array being written.
Before modifying your script, verify that your filtered data variable is properly formatted as a 2D array and confirm exactly how many columns your target destination requires.
Dynamically Size the Destination Range to Match the Array
Update your script to dynamically calculate the destination range's row count based on the array length of your data.
Office Scripts require the destination range to perfectly match the size of the 2D array you are trying to write. Hardcoding a range (like "V4:W10") often results in dimension mismatch errors if the filtered data size changes dynamically.
Navigate to the Automate tab in Excel and click on your script to open the Code Editor.
Find the line of code that defines your target destination range, such as `selectedSheet.getRange("V4:W...")`.
Replace the static range with a dynamic calculation using the `.length` of your data array. Use this formula: `let destinationRange = selectedSheet.getRange("V4:W" + (4 + newData.length - 1));`
Apply the data to the newly sized range by executing `destinationRange.setValues(newData);`.
Save your script and click Run. The script will now output the data without throwing a range dimension error.

Handling Scripts on a Protected Worksheet
Modify your script to temporarily unprotect the worksheet before writing the data and re-protect it afterward.
Discover WPS Office for Advanced Spreadsheet Automation
If you frequently struggle with complex Office Script restrictions or dimension errors in Microsoft Excel, consider trying WPS Office. WPS Spreadsheet provides an intuitive, offline-friendly environment supporting both standard VBA and JS Macros, making data manipulation and custom range automation straightforward and reliable.

Frequently Asked Questions
Why do I get a dimension mismatch error in Office Scripts?
This error occurs when the 2D array you are attempting to write to the worksheet has a different number of rows or columns than the destination range defined in your `setValues()` method. The target range dimensions must perfectly match the array dimensions.
How do I find the length of my filtered array in an Office Script?
You can determine the number of rows in your array by utilizing the `.length` property on your array variable (e.g., `newData.length`). This property returns an integer that you can use to size your destination range dynamically.
Can an Office Script run on a protected worksheet?
By default, scripts cannot modify cells within a protected worksheet. You must add code to unprotect the sheet using `sheet.getProtection().unprotect()`, perform your data modifications, and then apply `sheet.getProtection().protect()` to secure it again.




