Forum Discussion
MarkPoechman
Mar 18, 2024Copper Contributor
Move columns with excel office script
I have a table with a number of columns, and I want to use the column names to reorganize them. Specifically moving the "Property Class" column to position 0 in the table, and the "Property Descripti...
ahhmoh
Jul 18, 2025Copper Contributor
As of July 2025, moving columns' values only with office script can be done by:
- Saving the target column values
- Deleting the target column
- Inserting it back into the table at the desired index
Here is a sample showing how it works:
function main(workbook: ExcelScript.Workbook) {
const selectedSheet = workbook.getActiveWorksheet();
const table = selectedSheet.getTable("yourTableName");
moveColumn(table, "Property Class", 0);
moveColumn(table, "Property Description", 1);
}
function moveColumn(table: ExcelScript.Table, colName: string, index: number): void {
const column = table.getColumn(colName);
if(!column) {
return;
}
const values = column.getRange().getValues().map(row => row[0]);
column.delete();
table.addColumn(index, values);
}
SergeiBaklan
Jul 19, 2025Diamond Contributor
The only, it moves values, not formulae.
As variant
function main(workbook: ExcelScript.Workbook) {
const table = workbook.getTable("Table1")
moveColumn( table, "Property Class", 0)
moveColumn( table, "Property Description", 1)
}
const moveColumn = ( table: ExcelScript.Table, colName: string, index: number): void => {
const column = table.getColumn(colName)
if (!column || index >= table.getColumns().length ) { return }
const n = index + +(index > table.getColumnByName("C").getIndex())
table.addColumn(n)
const source = table.getColumnByName(colName)
source.getRange().moveTo(table.getColumns()[n].getRange())
source.delete()
}