【问题标题】:Is there a function in excel office JS to insert table columns next to the selected table column?excel office JS中是否有在所选表格列旁边插入表格列的功能?
【发布时间】:2020-07-07 11:37:33
【问题描述】:

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

我知道我可以使用索引参数通过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.

但是如何在表格中找到所选列的索引?我知道如何获取选定的范围,而不是表的选定列索引。

提前感谢您的帮助。

【问题讨论】:

    标签: office-js


    【解决方案1】:

    您可以通过 getItem() 获取列对象,然后您可以从列对象中获取索引。

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

      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();
      });
    

    【讨论】:

      猜你喜欢
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 2022-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多