How to Efficiently Loop and Fill Multiple Excel Columns with Office Scripts
Question details
The user is looking for an efficient way to automate the process of filling data across multiple spreadsheet columns (e.g., A through D) using Office Scripts, specifically seeking loop-based methods to avoid repetitive copy operations.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating spreadsheet data entry and extending values or formulas down multiple specific columns simultaneously.
- Observed behavior
- The user requires an optimized Office Script routine that uses range-based iteration or an array of cell addresses to cleanly copy and resize data ranges.
Ensure your Excel workbook is saved to SharePoint or OneDrive, and verify that your Microsoft 365 account has the correct permissions to create and run Office Scripts.
Use an Array Loop to Resize and Copy Ranges
Define specific cell addresses in an array and use a loop to iterate through them, systematically copying the values down to the end of your used range.
This method is highly efficient because it isolates the specific columns you want to target without hardcoding repetitive operations for each individual column.
Navigate to the 'Automate' tab on the Excel ribbon and click 'New Script' to open the Office Scripts editor.
Within your main function, declare the active worksheet using `workbook.getActiveWorksheet()`. Calculate the total rows to fill by taking `getUsedRange().getRowCount()` and subtracting your header rows.
Declare an array of your target starting cells, such as `const cells = ["A2", "B2", "C2", "D2"];`. Set up a `for (const cell of cells)` loop to iterate through them.
Inside the loop, use `selectedSheet.getRange(cell).getResizedRange(fillRows, 0).copyFrom(cell, ExcelScript.RangeCopyType.values);` to populate the columns dynamically.

Iterate Through a Defined Range Object
Instead of defining an array of strings, grab a continuous range object (like A2:D2) and iterate through its specific cell properties.
Automate Column Filling Offline with WPS JS Macros
WPS Office features a powerful built-in JavaScript Macro editor (JSA) that allows you to automate repetitive tasks like filling multiple columns. It works completely offline and is fully compatible with classic Excel file formats.
- 1. Access the JS Macro Editor: Open your workbook in WPS Spreadsheet, navigate to the 'Tools' tab, and click on 'JS Macro'.
- 2. Create a New Module: In the WPS Macro Editor, insert a new module to begin writing your JavaScript automation.
- 3. Write the Loop Array: Define your target columns in an array, such as `let cols = ["A", "B", "C", "D"];`, and set up a standard `for` loop.
- 4. Apply the AutoFill Method: Within the loop, utilize the `Range.AutoFill` method to dynamically populate the rows beneath your header.
- 5. Run Your Script: Save your code and click the 'Run' button to instantly execute the script and fill your spreadsheet columns.

Frequently Asked Questions
Why should I use a loop instead of copying columns individually in Office Scripts?
Using a loop reduces code duplication and streamlines execution. It makes your script significantly shorter and much easier to update if you need to add, modify, or remove target columns in the future.
How do I dynamically find the last row to fill in Office Scripts?
You can calculate the total number of populated rows by calling `selectedSheet.getUsedRange().getRowCount()`. Subtracting the number of header rows from this total gives you the exact parameter needed for the `getResizedRange` method.
Does getResizedRange copy formulas or just plain values?
By default, the `copyFrom` method's behavior depends on the parameters you set. If you specify `ExcelScript.RangeCopyType.values`, it will only paste plain values. To copy formulas, you should use `ExcelScript.RangeCopyType.formulas` or omit the specific type parameter to copy everything.
Does WPS Office support Microsoft Office Scripts?
WPS Office does not natively run Microsoft Office Scripts, which are cloud-based and rely on TypeScript for Microsoft 365. However, WPS provides a highly capable offline JavaScript Macro (JSA) environment and standard VBA support to accomplish identical automation tasks.




