如何使用Office.js api在Word中获取当前活动的table(当前光标所在的位置)?

How to get the current active table (where currently the cursor is positioned) in Word using Office.js api?

我想获取 table 的内容,只需单击 table( 而不是选择 table)即在使用 Office Js Api 的 Word 加载项中将光标放在 table 上。

Word JS API 具有(自 Requirement Set 1.3 发布以来)Range 对象的 parentTableParentTableOrNullObject 属性。

由于 document.GetSelection() returns 一个 Range 对象,像下面示例这样的代码将评估当前选择是否在 table 中。 (它还会计算行数并选择 table。)

    const range = context.document.getSelection();
    range.load("parentTableOrNullObject");
    await context.sync();
     var isInTable = range.parentTableOrNullObject;

    if (isInTable.isNullObject) {
      messageText = "The selection is not in a table."

    }
    else {
      var tblRows = isInTable.rowCount;
      isInTable.load("rowCount");

      await context.sync();
      var messageText = 
         "The selection is in a table with " + tblRows.toString() + " rows.";
      isInTable.select();
    }

    console.log(messageText);