0
votes

I want to get the content of a table ,by just clicking on the table(not selecting the table) i.e placing the cursor on the table, in Word Add-in using Office Js Api.

1

1 Answers

1
votes

The Word JS API has (since release of Requirement Set 1.3) the parentTable and the ParentTableOrNullObject properties of the Range object.

Since document.GetSelection() returns a Range object, code like the following sample will evaluate whether the current selection is in a table. (It also counts the number of rows and selects the 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);