0

寻找一种从多个表中按名称获取特定列的方法。我的数据有几张表,列数不同,最多 38 列,所以我不能使用getColumnById。我只需要 7 列。

首先将所有工作表范围转换为表格,然后获取所有表格。我想要的是按名称获取特定列并将所有列合并到新工作表上的一个表中。

我遵循了 Docs 中的示例,但被困在获取每个表的列名。

我知道我的标头值,如下例所示

function main(workbook: ExcelScript.Workbook) {
    let sheets = workbook.getWorksheets();
    for (let sheet of sheets) {
        sheet.getTables()[0].convertToRange();
        sheet.addTable(sheet.getRange('A1').getUsedRange().getAddress(),true)
    }
    workbook.getWorksheet('Combined')?.delete();
    const newSheet = workbook.addWorksheet('Combined');
    const tables = workbook.getTables();
    const headerValues = [['Column1', 'Column6', 'Column8', 'Column9','Column11', 'Column16', 'Column18', 'Column19']];
    const targetRange = newSheet.getRange('A1').getResizedRange(headerValues.length - 1, headerValues[0].length - 1);
    targetRange.setValues(headerValues);
    const combinedTable = newSheet.addTable(targetRange.getAddress(), true);
    for (let table of tables) {
        let dataValues = table.getColumnByName( // this where am stuck //).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);
        }
    }

}

谢谢你的帮助。乔治

4

1 回答 1

2

一些东西:

  1. 在这种情况下的大多数情况下,我建议遍历一组特定的表对象。不幸的是,这在这里很难做到。每次您取消链接并重新创建一个新表时,Excel 可能会为您的表指定一个新名称。这使得使用该表变得困难。您可以通过在取消链接之前捕获表名、取消链接表、重新创建表并将表名设置为捕获的原始表名来解决此问题。如果你走那条路,那么你可以可靠地使用表名
  2. 因为这种情况下的表名可能有点棘手,所以我将使用工作表名称,以便可以使用包含基础表的工作表。这将允许我们使用和从表中获取数据,而不管它们在工作表中的名称是什么。

请在下面查看我的代码:

function main(workbook: ExcelScript.Workbook) {

//JSON object called SheetAndColumnNames. On the left hand side is the sheet name. 
 //On the right hand side is an array with the column names for the table in the sheet.

  //NOTE: replace the sheet and column names with your own values

  let columnNames : string[] = ["ColA","ColB","ColC"]
  const sheetAndColumnNames = {
    "Sheet1": columnNames,
    "Sheet2": columnNames
  }

//JSON object called columnNamesAndCellValues. On the left hand side is the column name. 
 //On the right hand side is an array that will hold the values for the column in the table.

//NOTE: replace these column names with your own values
  const columnNamesAndCellValues = {
    "ColA": [],
    "ColB": [],
    "ColC": []
  }


//Iterate through the sheetAndColumnNames object
  for (let sheetName in sheetAndColumnNames) {

//Use sheet name from JSON object to get sheet
      let sheet: ExcelScript.Worksheet = workbook.getWorksheet(sheetName)

//get table from the previously assigned sheet
      let table: ExcelScript.Table = sheet.getTables()[0]

//get array of column names to be iterated on the sheet
      let tableColumnNames: string[] = sheetAndColumnNames[sheetName]

//Iterate the array of table column names
      tableColumnNames.forEach(columnName=> {

//get the dataBodyRange of the tableColumn
        let tableColumn : ExcelScript.Range = table.getColumn(columnName).getRangeBetweenHeaderAndTotal()

//iterate through all of the values in the table column and add them to the columnNamesAndCellValues array for that column name
        tableColumn.getValues().forEach(value=>{
          columnNamesAndCellValues[columnName].push(value)
        })
      })
  }

  //Delete previous worksheet named Combined
  workbook.getWorksheet("Combined")?.delete()

  //Add new worksheet named Combined and assign to combinedSheet variable
  let combinedSheet : ExcelScript.Worksheet = workbook.addWorksheet("Combined")

  //Activate the combined sheet
  combinedSheet.activate()

//get the header range for the table
  let headerRange : ExcelScript.Range = combinedSheet.getRangeByIndexes(0,0,1,columnNames.length)

//set the header range to the column headers
  headerRange.setValues([columnNames])

  //iterate through the arrays returned by the columnNamesAndCellValues object to write to the Combined sheet
  columnNames.forEach((column,index)=>{
    combinedSheet.getRangeByIndexes(1, index, columnNamesAndCellValues[column].length, 1).setValues(columnNamesAndCellValues[column])
    })

  //Get the address for the current region of the data written from the tableColumnData array to the sheet
  let combinedTableAddress : string = combinedSheet.getRange("A1").getSurroundingRegion().getAddress()

  //Add the table to the sheet using the address and setting the hasHeaders boolean value to true
  combinedSheet.addTable(combinedTableAddress,true)
}
于 2021-09-26T17:54:37.357 回答