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

Setting RangeView.numberFormat errors if the visibleView is 1 cell #4452

Open ChrisWRWX opened 6 months ago

ChrisWRWX commented 6 months ago

Provide required information needed to triage your issue

When trying to set the numberFormat of a visibleView that contains only 1 cell, the follow error is produced:

RichApi.Error / InvalidArgument / "The argument is invalid or missing or has an incorrect format."

Your Environment

Expected behavior

Setting the numberFormat should behave the same for a range of 1 cell as for larger ranges.

Current behavior

When getVisibleView() reduces the visible range to a single cell, setting the numberFormat no longer works.

Steps to reproduce

This works as expected:

image

// Sample for Script Lab
$("#run").on("click", () => tryCatch(run));

async function run() {
  await Excel.run(async (context) => {
    // adjust to not include header and only get visible cells
    const range = context.workbook
    .getSelectedRange()
    .getOffsetRange(1, 0)
    .getVisibleView();

    // Set 2 cells
    range.numberFormat = [["@"],["@"]];

    await context.sync();
  });
}

/** 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);
  }
}

This does not work:

image

// Sample for Script Lab
$("#run").on("click", () => tryCatch(run));

async function run() {
  await Excel.run(async (context) => {
    // adjust to not include header and only get visible cells
    const range = context.workbook
    .getSelectedRange()
    .getOffsetRange(1, 0)
    .getVisibleView();

    // Set 1 cell
    range.numberFormat = [["@"]];

    await context.sync();
  });
}

/** 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);
  }
}

Context

This causes an unpredictable error that only surfaces when a visible range is filtered down to a single cell.

microsoft-github-policy-service[bot] commented 6 months ago

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

XuanZhouMSFT commented 6 months ago

Thanks for reporting this issue regarding RangeView.numberFormat. It has been put on our backlog using #8989182. We will let you know once we fix it.

RuizhiSunMS commented 3 days ago

Hi @ChrisWRWX, our fix has been released for a long time, does it resolve your case?