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
690 stars 95 forks source link

Reading annotations throws error #4624

Closed gion-andri closed 5 months ago

gion-andri commented 5 months ago

Provide required information needed to triage your issue

Your Environment

Expected behavior

It should be possible to read the annotations of a paragraph

Current behavior

When reading the annotations of a paragraph (paragraph.getAnnotations()), officejs throws a GeneralException error.

Steps to reproduce

  1. Run the following snippet in the ScriptLab

    
    name: Blank snippet
    description: Creates a new snippet from a blank template.
    host: WORD
    api_set: {}
    script:
    content: |
    $("#run").on("click", () => tryCatch(run));
    
    async function run() {
      await Word.run(async (context) => {
        const body = context.document.body;
        context.load(body.paragraphs);
        await context.sync();
        const paragraphCollection = body.paragraphs.load({
          uniqueLocalId: true,
          text: true,
        });
    
        for (const paragraph of paragraphCollection.items) {
          console.log(paragraph.text);
        }
    
        for (const paragraph of paragraphCollection.items) {
          await spellcheckParagraph(paragraph);
        }
      });
    }
    
    // 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);
      }
    }
    
    async function spellcheckParagraph(paragraph: Word.Paragraph) {
      const annotations = paragraph.getAnnotations();
      annotations.load("id");
      await paragraph.context.sync();
    }
    language: typescript
    template:
    content: |
    <button id="run" class="ms-Button">
        <span class="ms-Button-label">Run</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

2. Click the "Run" button
shanshanzheng-dev commented 5 months ago

Hi @gion-andri Thanks for reporting this issue. I can repro and we'll be looking into this problem. We'll report back if we have a suggestion for you.

cbouzmsft commented 5 months ago

Hi. As far as I can tell, there is a bug in the sample: await context.sync() is missing after calling body.paragraphs.load.

gion-andri commented 5 months ago

True. The missing context sync was the issue. Thank you.