Office 脚本超出最大调用堆栈大小

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

继续我的最后一个问题,我已经将我的数据从工作表转换为表格,但我遇到了另一个问题。

我有两个脚本,称它们为“转换”和“组合”。如果我单独运行它们,它们就可以工作。我要么将它们放入同一个脚本中,要么在 Power Automate 流程中一起运行它们,这是我的期望。我在第 17 行遇到错误“我们无法运行脚本。请再试一次。 Office JS 错误:第 17 行:范围设置值:Le nombre de lignes ou de colonnes dans le tableau d’entrée ne correspond pas à la taille ou aux dimensions de la plage。”

尝试禁用第 17 行“//targetRange.setValues(headerValues);”,然后在第 31 行出现错误 “我们无法运行脚本。请重试。 Office JS 错误:第 31 行:表格添加行:Le nombre de lignes ou de colonnes dans le tableau d’entrée ne correspond pas à la taille ou aux dimensions de la plage。”

冲突在哪里?

转换

function main(workbook: ExcelScript.Workbook) {
        
    let worksheets = workbook.getWorksheets();
    for (let i = 1; i < worksheets.length; i++) {
    let selectedSheet = workbook.getWorksheets()[i];
        selectedSheet.getRange("1:1").delete(ExcelScript.DeleteShiftDirection.up);
        let newTable = workbook.addTable(selectedSheet.getRange("A1:AJ2"), true);
    }
    workbook.getWorksheet('Feuil1')?.delete();
}

结合

function main(workbook: ExcelScript.Workbook) {
  // Delete the "Combined" worksheet, if it's present.
     workbook.getWorksheet('Combined')?.delete(); 
     workbook.getWorksheet('Feuil1')?.delete();

  // Create a new worksheet named "Combined" for the combined table.
     const newSheet = workbook.addWorksheet('Combined');

  // Get the header values for the first table in the workbook.
  // This also saves the table list before we add the new, combined table.
    const tables = workbook.getTables();
    const headerValues = tables[1].getHeaderRowRange().getTexts();
    console.log(headerValues);
    
  // Copy the headers on a new worksheet to an equal-sized range.
 
    const targetRange = newSheet.getRange('A1').getResizedRange(headerValues.length-1, headerValues[0].length-1);

  //targetRange.setValues(headerValues); 

  // Add the data from each table in the workbook to the new table.
     const combinedTable = newSheet.addTable(targetRange.getAddress(), true);
     for (let table of tables) {
     let dataValues = table.getRangeBetweenHeaderAndTotal().getTexts();
        let rowCount = table.getRowCount();

    // If the table is not empty, add its rows to the combined table.
     if (rowCount > 0) {
         combinedTable.addRows(0, dataValues);
    }      
  }  
} 
javascript excel typescript office-js power-automate
1个回答
0
投票

错误表明您正在尝试设置不符合值形状的值范围。值数组的元素太少或太多,符合这些原则。

不确定您提供的代码是否包括 combine convert 等。但是无论哪种方式,如果它们单独工作,我猜一个的输出与另一个的输入不兼容。

这是一个猜测,因为不确定代码的哪些部分代表什么,但如果这是唯一的代码,则可能是您的硬编码选择范围

"A1:AJ2"
没有考虑到上述范围删除。

© www.soinside.com 2019 - 2024. All rights reserved.