Office 脚本 - 无法读取未定义的属性(读取“getHeaderRowRange”)

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

尝试在我的数据集上使用下面的示例代码,但不断出现错误“无法读取未定义的属性(读取‘getHeaderRowRange’)”

将多个Excel表格中的数据合并到一个表格中 https://learn.microsoft.com/en-us/office/dev/scripts/resources/samples/copy-tables-combine

function main(workbook: ExcelScript.Workbook) {
  // Delete the "Combined" worksheet, if it's present.
  workbook.getWorksheet('Combined')?.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[0].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(-1, dataValues);
    }
  }
}

尝试清理我的数据集,使其看起来像下面的东西。没有运气

尝试通过从第一张纸复制手动创建“标题行”,不知道如何继续

const combined = workbook.getWorksheet("Combined");    combined.getRange("1:1").copyFrom(workbook.getFirstWorksheet().getRange("1:1"), ExcelScript.RangeCopyType.all, false, false); 

也尝试过将动作记录到脚本中,但是当有大量工作表时它不会工作。

excel typescript office-js excel-addins office-scripts
© www.soinside.com 2019 - 2024. All rights reserved.