代码之家  ›  专栏  ›  技术社区  ›  laoda

Office Script Office JS错误:第17行:范围设置值:

  •  0
  • laoda  · 技术社区  · 2 年前

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

    我有两个脚本,称它们为“convert”和“combine”。如果我把它们分开运行,它们就会工作。我要么把它们放在同一个脚本中,要么在Power Automate流中一起运行,这是我的期望。我在第17行出错 我们无法运行脚本。请再试一次。 Office JS错误:第17行:范围设置值:表格中的列和冒号的名称与瘟疫的尾部和尺寸相对应。“

    尝试禁用第17行“//targetRange.setValues(headerValues);”,然后在第31行出现错误 “我们无法运行脚本。请重试。 Office JS错误:第31行:表格添加行:表格中的列和冒号的名称与瘟疫的尾部和尺寸相对应。“

    冲突在哪里?

    转换

    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);
        }      
      }  
    } 
    
    1 回复  |  直到 2 年前
        1
  •  0
  •   Max    2 年前

    该错误表明您试图设置的值范围与值的形状不匹配。值数组中的元素太少或太多,有些类似。

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

    这是一个猜测,因为不确定代码的哪些部分代表什么,但如果这是唯一的代码,那么可能是您的硬编码选择范围 "A1:AJ2" 没有考虑到上述范围删除。