OfficeDev/office-js

Setting RangeView.numberFormat errors if the visibleView is 1 cell

Opened this issue · 2 comments

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

  • Platform [PC desktop, Mac, iOS, Office on the web]: PC Desktop
  • Host [Excel, Word, PowerPoint, etc.]: Excel
  • Office version number: Version 2406
  • Operating System: Windows 10
  • Browser (if using Office on the web): N/A

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:

  • Add some data and set a filter such that more than 1 row remains.
  • Run code below.
  • The two cells are set to Text format.

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:

  • Add some data and set a filter such that only 1 row remains.
  • Run code below.
  • An error is thrown.

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.

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

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.