Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Office js excel office JS中是否有在所选表格列旁边插入表格列的功能?_Office Js - Fatal编程技术网

Office js excel office JS中是否有在所选表格列旁边插入表格列的功能?

Office js excel office JS中是否有在所选表格列旁边插入表格列的功能?,office-js,Office Js,我试图通过office JS模拟excel本身提供的以下功能,在这里我可以在excel中选择一个表列并在左侧插入一个新列: 我知道我可以使用index参数通过Excel.TableColumnCollection.add(index,values,name)方法插入列: index number Optional. Specifies the relative position of the new column. If null or -1, the addition happens at

我试图通过office JS模拟excel本身提供的以下功能,在这里我可以在excel中选择一个表列并在左侧插入一个新列:

我知道我可以使用index参数通过
Excel.TableColumnCollection.add(index,values,name)
方法插入列:

index
number
Optional. Specifies the relative position of the new column. 
If null or -1, the addition happens at the end. 
Columns with a higher index will be shifted to the side. Zero-indexed.
但是如何在表中找到所选列的索引呢?我知道如何获取选定范围,但不知道如何获取表的选定列索引


提前感谢您的帮助。

您可以通过getItem()获取column对象,并且可以从column对象获取索引

这是示例代码,它将在所选列标题的左侧插入列

  await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const expensesTable = sheet.tables.getItem("ExpensesTable");
    var range = context.workbook.getSelectedRange();
    range.load("values");
    await context.sync();
    var colname = range.values[0][0];
    var col = expensesTable.columns.getItem(colname);
    col.load("index");
    await context.sync();
    
    expensesTable.columns.add(col.index, [
      ["Deductable?"],
      ["Yes"],
      ["Yes"],
      ["No"],
      ["No"],
      ["Yes"],
      ["Yes"],
      ["No"],
      ["Yes"],
      ["Yes"],
      ["No"]
    ]);

    sheet.getUsedRange().format.autofitColumns();
    sheet.getUsedRange().format.autofitRows();

    await context.sync();
  });

可以通过getItem()获取column对象,也可以从column对象获取索引

这是示例代码,它将在所选列标题的左侧插入列

  await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const expensesTable = sheet.tables.getItem("ExpensesTable");
    var range = context.workbook.getSelectedRange();
    range.load("values");
    await context.sync();
    var colname = range.values[0][0];
    var col = expensesTable.columns.getItem(colname);
    col.load("index");
    await context.sync();
    
    expensesTable.columns.add(col.index, [
      ["Deductable?"],
      ["Yes"],
      ["Yes"],
      ["No"],
      ["No"],
      ["Yes"],
      ["Yes"],
      ["No"],
      ["Yes"],
      ["Yes"],
      ["No"]
    ]);

    sheet.getUsedRange().format.autofitColumns();
    sheet.getUsedRange().format.autofitRows();

    await context.sync();
  });