Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
range.transpose() method and works on every Excel version, while the second uses cell.setDynamicArrayFormula() to write a modern dynamic array =TRANSPOSE(...) formula that spills automatically on Excel 365 or Excel 2021. The third approach uses cell.setArrayFormula() to write a classic Ctrl+Shift+Enter (CSE) array formula that is compatible with older Excel versions. This article walks through each approach with step-by-step instructions and complete code examples.
Transposing a range means rotating it so that what was a row becomes a column and what was a column becomes a row, effectively reflecting the data across its main diagonal. In Microsoft Excel, the worksheet function TRANSPOSE performs this operation, and the conceptual reference is documented at https://support.microsoft.com/en-us/excel/functions/transpose-function. This concept can be applied programmatically to a range of cells, which is useful in many business and reporting scenarios.
| Region | Europe | Asia | North America |
|---|---|---|---|
| Qtr 1 | 21704714 | 8774099 | 12094215 |
| Qtr 2 | 17987034 | 12214447 | 10873099 |
| Qtr 3 | 19485029 | 14356879 | 15689543 |
| Qtr 4 | 22567894 | 15763492 | 17456723 |
| The article then presents three different ways to transpose this data using Aspose.Cells for Node.js via C++, each suited to a different Excel version and use case. |
Use this approach whenever you want to transpose data without involving the TRANSPOSE worksheet function. It works on every version of Excel and has no dependency on dynamic arrays, which makes it the safest cross-version compatible option. It is ideal when you only need the final transposed output and do not need to keep the original TRANSPOSE formula in the workbook.
range.transpose() is an instance method on the Aspose.Cells.Range class. Calling it flips the range in place by swapping its rows and columns, so what was a row becomes a column and what was a column becomes a row. The method modifies the underlying cells directly without writing a formula.
LoadOptions set to the .xlsx format by calling new Workbook(srcFile, new LoadOptions(LoadFormat.Xlsx)).workbook.getWorksheets().get(0).worksheet.getCells().cells.createRange("A1:D5").source.transpose() to rotate the range in place, swapping rows and columns.workbook.save(outputFile).
After transposition the initial anchor range holds the rotated data. The first row reads (empty, Europe, Asia, North America) and the first column reads (empty, Qtr 1, Qtr 2, Qtr 3, Qtr 4). Each original column of sales becomes a row in the transposed range.var srcFile = "source.xlsx";
var outputFile = "transposed.xlsx";
var workbook = new AsposeCells.Workbook(srcFile, new AsposeCells.LoadOptions(AsposeCells.LoadFormat.Xlsx));
var worksheet = workbook.getWorksheets().get(0);
var cells = worksheet.getCells();
var source = cells.createRange("A1:D5");
source.transpose();
workbook.save(outputFile);
Use this approach when you want to preserve the =TRANSPOSE(A1:D5) formula as a live formula in the output workbook so the result updates automatically if the source data changes, and the target Excel file will be opened in Excel 365 / Excel 2021 or later where dynamic arrays and the spill operator are supported.
cell.setDynamicArrayFormula(string formula, FormulaParseOptions options, bool calculateValue) is a method on Aspose.Cells.Cell that sets the cell’s formula as a dynamic array formula. Excel evaluates the formula once and automatically spills the result into the surrounding cells. The third parameter, when set to true, instructs Aspose.Cells to also calculate the resulting values at write time.
new Workbook(srcFile, new LoadOptions(LoadFormat.Xlsx)).Cells collection.cells.get("A6").setDynamicArrayFormula("=TRANSPOSE(A1:D5)", null, true).null argument passes default FormulaParseOptions, and the third argument true tells Aspose.Cells to treat the formula as a dynamic array and to evaluate it so the spilled values are written to the workbook.workbook.save(outputFile).
Cell A6 holds the formula =TRANSPOSE(A1:D5) and Excel spills the result automatically into the region A6:D10, a 5-row by 4-column block equal to the transposed data.const AsposeCells = require("aspose.cells");
const srcFile = "source.xlsx";
const outFile = "output_transpose_dynamic.xlsx";
const opts = new AsposeCells.LoadOptions(AsposeCells.LoadFormat.Xlsx);
const workbook = new AsposeCells.Workbook(srcFile, opts);
const worksheet = workbook.getWorksheets().get(0);
const cells = worksheet.getCells();
cells.get("A6").setDynamicArrayFormula("=TRANSPOSE(A1:D5)", new AsposeCells.FormulaParseOptions(), true);
workbook.save(outFile, AsposeCells.SaveFormat.Xlsx);
Use this approach when you want a TRANSPOSE formula preserved in the workbook but the target Excel file may be opened in older Excel versions (pre-2021, including 2019, 2016, 2013, and so on) where dynamic array spilling is not supported. The classic CSE (Ctrl+Shift+Enter) array formula is the legacy-compatible alternative that all Excel versions can evaluate.
cell.setArrayFormula(string arrayFormula, int nRows, int nColumns) is a method on Aspose.Cells.Cell that assigns a classic array (CSE) formula to the anchor cell and declares the dimensions of the resulting array. Aspose.Cells writes the multi-cell array-formula marker so Excel evaluates the formula as a single array expression that fills the declared range.
Cells collection.cells.get("A6").setArrayFormula("=TRANSPOSE(A1:D5)", 4, 5). The second argument 4 is the number of rows of the destination array and the third argument 5 is the number of columns.workbook.save(outputFile).
Cell A6 is the anchor of the array formula and the evaluated array spans 4 rows by 5 columns starting at A6, matching the transposed dimensions of the A1:D5 source. Excel writes a single array-formula marker across the resulting range so older Excel versions evaluate it correctly.TRANSPOSE expression and this approach is universally compatible across Excel versions.
const AsposeCells = require("aspose.cells");
// Load the source workbook with xlsx LoadOptions
const srcFile = "source.xlsx";
const workbook = new AsposeCells.Workbook(srcFile, new AsposeCells.LoadOptions(AsposeCells.LoadFormat.Xlsx));
// Access the first worksheet and its Cells collection
const worksheet = workbook.getWorksheets().get(0);
const cells = worksheet.getCells();
// Set the classic CSE array formula on cell A6.
// The formula =TRANSPOSE(A1:D5) rotates the 5-row x 4-column source range
// into a 4-row x 5-column array. The second argument (4) is the number of rows
// and the third argument (5) is the number of columns of the resulting array.
// Aspose.Cells writes the CSE array-formula marker so Excel evaluates it as
// a single multi-cell array formula, compatible with older Excel versions
// (2019, 2016, 2013, etc.) that do not support dynamic array spilling.
cells.get("A6").setArrayFormula("=TRANSPOSE(A1:D5)", 4, 5);
// Save the workbook so the array-formula marker is persisted
workbook.save("output.xlsx");
| Approach | API / Method | Excel Version | Source formula preserved? | Output range |
|---|---|---|---|---|
| Approach 1 — In-place transpose | range.transpose() |
All Excel versions | No (values only) | Initial anchor range, 5×4 |
| Approach 2 — Dynamic array formula | cell.setDynamicArrayFormula |
Excel 365 / 2021+ | Yes (spills dynamically) | Spilled from anchor |
| Approach 3 — Classic array formula (CSE) | cell.setArrayFormula |
All Excel versions | Yes (multi-cell array formula) | Explicit size, 4×5 |
| Use Approach 1 when you need a quick, cross-version transformation and only need the transposed values written to the file. Use Approach 2 when modern Excel is guaranteed and you want the formula to stay live and update if the source changes. Use Approach 3 when you need the widest compatibility with a preserved formula across every Excel version, including the older releases that do not support dynamic arrays. |
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.