2

I have an excel js add-in with custom functions, some of them should return date and here I have a problem:

  • I can format and display this date as a string but in this case, I can't use this date as a date in formulas. or
  • I can cast it '.ToOADate', and use it everywhere as date, but it looks like a number but not date until I set correct number format manually.

so here is the question: can I set a 'short date' number format programmatically as it happens when I use '=DATE' function?

2 Answers 2

1

You can use the range.numberFormat to set the shortFormat date. But differently from the answer from Raymond Lu, it is an array of array of strings. You cannot just simply set a string, but you need to set the formatting for each individual cell in the range.

If your range has only one cell you could do:

myRange.numberFormat = [["m/d/yyyy"]];

Please check more information in the official Microsoft documentation:

https://learn.microsoft.com/en-us/javascript/api/excel/excel.range?view=excel-js-preview#excel-excel-range-numberformat-member

https://learn.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-ranges-set-format#set-number-format

Sign up to request clarification or add additional context in comments.

Comments

-1

Welcome to Office JS!

I think you could use range.numberFormat API to set the format for the range. the short date number format in en-us is "m/d/yyyy"

range.numberFormat = "m/d/yyyy";

Meanwhile, there is a feature request item in Custom Function's backlog. We would suggest that you could submit your request in uservoice and upvote this feature

9 Comments

sounds like a plan, but how to do it in case of custom functions? when I have a construction like export async function fName (params) { return apiRequestResult; //here I have a date }
Let me ask around and invite Custom Function team to share their view on it.
i would be happy to any advice or new idea =) one moment - we are expecting that users will call dozens or hundreds of functions per time, so solutions, like calling the context and looking for addresses of each cell and just then setting format, doesn't look fast enough.
From my understanding the column usually is the same number format, therefore, you don't have to set each individual cells, but you can set the whole column. As a result, the custom function return value can be shown as a date.
i agree in most cases it is true, but one more time, imagine: user placed some function like "=myNamespace.myFunc(params)" and make row or column with this function, size of this row, or column I do not know. and what we have: - the function which will be called as many time as many user wanted, - the function works asynchronously so in what moment and how i should try to calculate range and set numberFormat? ps sorry but this really eludes me
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.