typescript 使用Office Excel构建包含不连续单元的阵列

4zcjmb1e  于 2023-10-22  发布在  TypeScript
关注(0)|答案(1)|浏览(115)

下面的helper函数获取一个单元格范围作为第一个参数,第二个参数是一个整数:

function identacaoGrupos(rng: object, numeroGrupos:number) {
  let arr =[rng];

  for(let i = 0; i<numeroGrupos; i++){
    arr.push(arr[i].getExtendedRange(ExcelScript.KeyboardDirection.down).getLastCell());
  };
}

我需要在给定的单元格上启动(例如:B3)并得到一个数组,其下的单元格不为空。
举例来说:

以B3单元格作为第一个参数,4作为第二个参数调用此函数,应返回包含单元格B3、B7、B11和B20的数组arr
但它返回错误:"Property getExtendedRange does not exist on type 'object'"
有什么需要帮忙的吗?

f87krz0w

f87krz0w1#

  • 将参数rng数据类型更改为Range
  • identacaoGrupos返回Range对象数组
function main(workbook: ExcelScript.Workbook) {
    let selectedSheet = workbook.getActiveWorksheet();
    let startCell: ExcelScript.Range = selectedSheet.getRange("B2");
    let numeroGrupos: number = 4;
    let dataCell = identacaoGrupos(startCell, 4);
    dataCell.forEach(cell => {
        console.log(cell.getAddress());
    });
}

function identacaoGrupos(rng: ExcelScript.Range, numeroGrupos: number): ExcelScript.Range[] {
    let arrRange: ExcelScript.Range[] = [];
    // get next non-blank cell if rng is blank
    if (!rng.getText()) {
        rng = rng.getRangeEdge(ExcelScript.KeyboardDirection.down)
    }
    for (let i = 0; i < numeroGrupos; i++) {
        // collect non-blank cell
        if (rng.getText()) { arrRange.push(rng) };
        // locate next non-blank cell
        rng = rng.getRangeEdge(ExcelScript.KeyboardDirection.down);
    };
    return arrRange;
}

相关问题