OfficeDev / office-js

A repo and NPM package for Office.js, corresponding to a copy of what gets published to the official "evergreen" Office.js CDN, at https://appsforoffice.microsoft.com/lib/1/hosted/office.js.
https://learn.microsoft.com/javascript/api/overview
Other
678 stars 95 forks source link

How to get a footnote/endnote reference number #5027

Open NataliaKravcheniaTR opened 3 hours ago

NataliaKravcheniaTR commented 3 hours ago

I'd like to get a footnote/endnote reference number, but I'm getting incorrect text. In the example the referenceNumber is just a value from a text box.

Your Environment

Expected behavior

reference.text returns a reference number text

Current behavior

I'm getting "�" text instead

Steps to reproduce

Load this script in Script Lab: Click 'Setup', 'get footnote reference' buttons

name: Get footnote reference number
description: ''
host: WORD
api_set: {}
script:
  content: |
    $("#run").on("click", () => tryCatch(run));
    $("#insert").on("click", () => tryCatch(() => insert()));

    const bodyText = "body text";
    const footnoteText = "footnote text";

    let annIds = [];

    async function run() {
      await Word.run(async (context) => {
        const body = context.document.body;
        const range = body.insertText(bodyText, Word.InsertLocation.replace);

        const footnote = range.insertFootnote(footnoteText);

        await context.sync();
      });
    }

    async function insert() {
      await Word.run(async (context) => {
        const footnotes = context.document.body.footnotes;

        footnotes.load("length,text");

        await context.sync();

        const footnote = footnotes.items[0];

        const reference = footnote.reference.getRange();

        reference.load("text");

        await context.sync();

        console.log("reference text: " + reference.text);
      });
    }
    // Default helper for invoking an action and handling errors.
    async function tryCatch(callback) {
      try {
        await callback();
      } catch (error) {
        // Note: In a production add-in, you'd want to notify the user through your add-in's UI.
        console.error(error);
      }
    }
  language: typescript
template:
  content: |
    <button id="run" class="ms-Button">
        <span class="ms-Button-label">setup</span>
    </button>

    <button id="insert" class="ms-Button">
        <span class="ms-Button-label">get footnote reference</span>
    </button>
  language: html
style:
  content: |-
    section.samples {
        margin-top: 20px;
    }

    section.samples .ms-Button, section.setup .ms-Button {
        display: block;
        margin-bottom: 5px;
        margin-left: 20px;
        min-width: 80px;
    }
  language: css
libraries: |-
  https://appsforoffice.microsoft.com/lib/1/hosted/office.js
  @types/office-js

  office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
  office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css

  core-js@2.4.1/client/core.min.js
  @types/core-js

  jquery@3.1.1
  @types/jquery@3.3.1
microsoft-github-policy-service[bot] commented 3 hours ago

Thank you for letting us know about this issue. We will take a look shortly. Thanks.