Search code examples
exceltypescriptoffice-scriptsms-office-script

How to copy data from one column to another column using Office Scripts?


I'm currently working on automating some tasks in Microsoft Excel using Office Scripts, and I'm having trouble figuring out how to copy data from one column to another.

enter image description here

I want to copy Date column(D) data to New_Date column(F).

Any help or code snippets would be greatly appreciated. Thank you!


Solution

  • Below code copy Col Date to Col NewDate.

    function main(workbook: ExcelScript.Workbook) {
        // modify table name as needed
        let table1 = workbook.getTable("Table1"); 
        let srcRange = table1.getColumn("Date").getRangeBetweenHeaderAndTotal();
        let targetCell = table1.getColumn("New_Date").getRangeBetweenHeaderAndTotal().getRow(0);
        targetCell.copyFrom(srcRange, ExcelScript.RangeCopyType.all, false, false);
    }