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
648 stars 92 forks source link

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

Open ChrisWRWX opened 2 weeks ago

ChrisWRWX commented 2 weeks 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 2 weeks ago

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

XuanZhouMSFT commented 2 weeks 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.