The Office JS range.formulas API should preserve the leading single quote.
We have an Excel add-in used in both Excel O365 Desktop and Excel O365 Web. Our add-in processes formulas in cells, including custom functions, and sometimes needs to remove prepended namespaces. The relevant code is:
const range = context.workbook.getSelectedRange();
range.load("formulas");
await context.sync();
console.log('The range formulas', range.formulas);
const newFormulas = range.formulas;
range.formulas = newFormulas;
Problem:
Some users enter informational strings in cells that resemble formulas like
'=AVERAGE([cell start]:[cell end])
To ensure excel treats these as plain text, they start the entry with a single quote ('). Excel correctly stores these as strings and does not throw any formula error.
However, when we use the Office JS API to read and reapply these values, the leading single quote is stripped. The API returns:
'=AVERAGE([cell start]:[cell end])'
When we attempt to set this value back using range.formulas = newFormulas, excel throws the following error:
RichApi.Error: The argument is invalid or missing or has an incorrect format.
at new n (excel-web-16.00.js:25:328325)
at i.processRequestExecutorResponseMessage (excel-web-16.00.js:25:393560)
at excel-web-16.00.js:25:391621
The Office JS API does not preserve the leading single quote, so Excel interprets the value as a formula, which is invalid in this context.
This behavior is inconsistent with Excel’s native handling, where the single quote ensures the value is treated as text.
1 Comment
- Panthers_12345678SudoCopper Contributor
this was raised as an incident with microsoft already -https://github.com/OfficeDev/office-js/issues/6687