如何使用office脚本将公式复制到新工作表中

问题描述 投票:0回答:1

我是office-script新手,我尝试使用ChatGPT解决这个问题,但这根本没有帮助。 :-(

  1. 我正在尝试使用下面的代码将行从一张纸复制到另一张纸,但此代码不包含公式。我不知道如何将其实现到代码中。

  2. 在下一步中,我需要相同的逻辑将行移动到新工作表(包括公式)。

一行只能包含数据和公式,某些单元格在此阶段为空。当我开始时,我遇到了行格式的问题。当目标表为空时,它总是接管表头的格式。但好像已经没有了。

一些列(不同)隐藏在两个工作表(目标和源)中,但这不是下面脚本的问题,这只是 ChatGPT 解决方案的问题,它还做了一些奇怪的事情,例如插入行两次覆盖现有行。奇怪的人工智能! ;-)

欢迎任何帮助!

这是迄今为止的“复制代码”:

/*

此脚本执行以下操作:

从源表中选择行,其中列中的值等于某个值(脚本中的 FILTER_VALUE)。

将所有选定的行移至另一个工作表中的目标表中。

将相关过滤器重新应用到源表。

*/


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]);

    });

}

请参阅上面我的问题描述

excel copy-paste move office-scripts
1个回答
0
投票
  • 人工智能编码是一个很好的起点,但你绝对不能依赖它来生成完美的代码。就你而言,人工智能使事情变得过于复杂。
function main(workbook: ExcelScript.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";
    let desTab = workbook.getTable(TARGET_TABLE_NAME);
    let srcTab = workbook.getTable(SOURCE_TABLE_NAME);
    // Clear auto filter on table srcTab
    srcTab.getAutoFilter().clearCriteria();
    // Apply checked items filter on table srcTab column Col5
    srcTab.getColumnById(FILTER_COLUMN_INDEX).getFilter().applyValuesFilter([FILTER_VALUE]);
    const filterRange = srcTab.getRangeBetweenHeaderAndTotal().getSpecialCells(ExcelScript.SpecialCellType.visible);
    let anchorCell = desTab.getColumnById(1).getRange().getLastCell().getOffsetRange(1, 0)
    // console.log(anchorCell.getAddress())
    anchorCell.copyFrom(filterRange);
    srcTab.getAutoFilter().clearCriteria();
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.