how to copy formulas into a new sheet using office-script

I am new to office-script and I have tried to work on this issue with ChatGPT, but this was not helpful at all. 🙁

  1. I am trying to copy rows from one sheet to another with the code below, but this code does not include the formulas. I have no idea how to implement this into the code.

  2. Within the next step I would need the same logic to move rows to a new sheet (including formulas).

A row can contain just data and formulas, some cells are empty at this stage. I had a problem with the format of the rows when I started. It always took over the format of the table header when the target table was empty. But that seems to be gone.

Some columns (different) are hidden in both sheets (target and source), but this was not a problem with the script below, it was only a problem with a solution from ChatGPT, which also did some strange things like insert the rows twice with overwriting existing rows. Strange AI! 😉

Any help welcome!

here is the “copy code” so far:

/*

This script does the following:

Selects rows from the source table where the value in a column is equal to some value (FILTER_VALUE in the script).

Moves all selected rows into the target table in another worksheet.

Reapplies the relevant filters to the source table.

*/

function main(workbook: ExcelScript.Workbook) {

// You can change these names to match the data in your workbook.

const TARGET_TABLE_NAME = "Start";

const SOURCE_TABLE_NAME = "BasicInfo";




// Select what will be moved between tables.

const FILTER_COLUMN_INDEX = 5;

const FILTER_VALUE = "to be processed";




// Get the Table objects.

let targetTable = workbook.getTable(TARGET_TABLE_NAME);

let sourceTable = workbook.getTable(SOURCE_TABLE_NAME);




// If either table is missing, report that information and stop the script.

if (!targetTable || !sourceTable) {

    console.log(

        `Tables missing - Check to make sure both source (${TARGET_TABLE_NAME}) and target table (${SOURCE_TABLE_NAME}) are present before running the script. `

    );

    return;

}




// Save the filter criteria currently on the source table.

const originalTableFilters = {};

// For each table column, collect the filter criteria on that column.

sourceTable.getColumns().forEach((column) => {

    let originalColumnFilter = column.getFilter().getCriteria();

    if (originalColumnFilter) {

        originalTableFilters[column.getName()] = originalColumnFilter;

    }

});




// Get all the data from the table.

const sourceRange = sourceTable.getRangeBetweenHeaderAndTotal();

const dataRows: (

    | number

    | string

    | boolean

)[][] = sourceTable.getRangeBetweenHeaderAndTotal().getValues();




// Create variables to hold the rows to be moved and their addresses.

let rowsToMoveValues: (number | string | boolean)[][] = [];

let rowAddressToRemove: string[] = [];




// Get the data values from the source table.

for (let i = 0; i < dataRows.length; i++) {

    if (dataRows[i][FILTER_COLUMN_INDEX] === FILTER_VALUE) {

        rowsToMoveValues.push(dataRows[i]);




        // Get the intersection between table address and the entire row where we found the match. This provides the address of the range to remove.

        let address = sourceRange

            .getIntersection(sourceRange.getCell(i, 0).getEntireRow())

            .getAddress();

        rowAddressToRemove.push(address);

    }

}




// If there are no data rows to process, end the script.

if (rowsToMoveValues.length < 1) {

    console.log(

        "No rows selected from the source table match the filter criteria."

    );

    return;

}




console.log(`Adding ${rowsToMoveValues.length} rows to target table.`);




// Insert rows at the end of target table.

targetTable.addRows(-1, rowsToMoveValues);






// Reapply the original filters.

Object.keys(originalTableFilters).forEach((columnName) => {

    sourceTable

        .getColumnByName(columnName)

        .getFilter()

        .apply(originalTableFilters[columnName]);

});

}

see above within my problem description

New contributor

Eik Moriben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật