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

Excel Online - refresh setting wipes out data #594

Closed GeorgeHung closed 5 years ago

GeorgeHung commented 5 years ago

Running Office.context.document.settings.refreshAsync causes data in setting to be removed.

Expected Behavior

RefreshAsync should get data saved with Office.context.document.settings.saveAsync

Current Behavior

Running Office.context.document.settings.refreshAsync caused data in setting to be removed.

Steps to Reproduce, or Live Example

  1. Import the YAML below into script lab.
  2. Go to run
  3. Click on "Save Setting" to save data into settings. In the console, it shows the running saveAsync is success.
  4. Click on "Read" to read from setting, and the data is "abc" as expected.
  5. Click on "Refresh then Read Setting", the data is removed because the value is null.
name: Basic API call (JavaScript)
description: Performs a basic Excel API call using plain JavaScript & Promises.
host: EXCEL
api_set: {}
script:
  content: |
    $("#save").click(() => tryCatch(run));

    function run() {
      Office.context.document.settings.set("testSetting", "abc");

      Office.context.document.settings.saveAsync(function(asyncResult) {
        console.log("Settings saved with status: " + asyncResult.status);
      });
    }

    $("#read").click(() => tryCatch(read));

    function read() {
      console.log("testSetting is " + Office.context.document.settings.get("testSetting"));
    }

    $("#readWR").click(() => tryCatch(readWR));

    function readWR() {
      Office.context.document.settings.refreshAsync(function(asyncResult) {
        console.log("Settings refresh with status: " + asyncResult.status);

        console.log("testSetting is " + Office.context.document.settings.get("testSetting"));
      });
    }

    /** Default helper for invoking an action and handling errors. */
    function tryCatch(callback) {
      Promise.resolve()
        .then(callback)
        .catch(function(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: "<section class=\"ms-font-m\">\n\t<p class=\"ms-font-m\">This sample demonstrates basic Excel API calls.</p>\n</section>\n\n<section class=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\t<p class=\"ms-font-m\">Test Settings</p>\n\t<button id=\"save\" class=\"ms-Button\">\n        <span class=\"ms-Button-label\">Save Setting</span>\n    </button>\n\t<button id=\"read\" class=\"ms-Button\">\n\t\t\t        <span class=\"ms-Button-label\">Read Setting </span>\n\t\t\t    </button>\n\t<button id=\"readWR\" class=\"ms-Button\">\n\t        <span class=\"ms-Button-label\">Refreh then Read Setting</span>\n\t    </button>\n\n\t\n</section>"
  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

Context

We use settings to save data in the documents.

Your Environment

catchingyu commented 5 years ago

@GeorgeHung can you share with us your "Refresh then Read Setting" code also ? It would be great that you could share with us your gist link .

GeorgeHung commented 5 years ago

Please find the code below. It is also in the YAML which I included in the issue request.

function readWR() {
  Office.context.document.settings.refreshAsync(function(asyncResult) {
    console.log("Settings refresh with status: " + asyncResult.status);
    console.log("testSetting is " + Office.context.document.settings.get("testSetting"));
  });
}
catchingyu commented 5 years ago

@GeorgeHung is it possible for you to try API https://docs.microsoft.com/en-us/javascript/api/excel/excel.setting?view=office-js, this is new API and should work as you expected.

GeorgeHung commented 5 years ago

The new api seems to allow me to read the values from the settings. However, I am having issue using the new api to write to the settings. You can close this bug. I will file another one for the other issue.

Rinzwind commented 5 years ago

Shouldn’t this get fixed (even if there’s another API that can be used)? I'm encountering what seems to be the same problem.

I tried the Script Lab example given above. It works as expected in Excel for Mac (first screenshot), but in Office Online, the setting seems to get lost upon doing “refreshASync” (second screenshot).

The console on each screenshot shows the output of using “Read Setting”, “Save Setting”, “Read Setting”, “Refre(s)h then Read Setting”, “Read Setting”.

Versions used: Excel 16.28 (19081202) and Chrome 78.0.3904.108 on macOS 10.13.6

Excel for Mac:

Excel Mac

Office Online:

Excel Office Online