Open Jeroendevr opened 1 year ago
Hi @Jeroendevr ,
Thanks for reporting this, I've tried to repro this issue, but seems not same behavior with yours.
I think this should be related about the difference Excel culture setting, could you share how you've set the culture before running the script? That would help us a lot to locate the cause.
Thanks.
Hi @donlvMSFT, yes the culture settings are different indeed. This is what I meant with locale but that was not entirely clear perhaps.
The macOS system is set to
Nederlands
(a.k.a. Dutch
)Nederland
(a.k.a. The Netherlands
)
As I understand correctly from the Excel Help file, Microsoft Office automatically uses the primary language of the system. It does so correctly because console.log(shortDatePattern)
yields
dd-mm-jjjj
in my case m/d/yyyy
in your reproduction (probably set to us
or similar)P.S.
btw only changing the interface language of Excel keeps the console.log(shortDatePattern)
to dd-mm-jjjj
which I find correct as that is the system settings. So when you try to reproduce setting the Excel interface to Dutch
is not sufficient it has to be the system language.
Also on my VM I have the same results as on macOS, settings are:
English (United States)
Netherlands
Dutch (Netherlands)
Got you @Jeroendevr , I did some validation under your setting and found this is a tricky scenario.
So in you sample code,
// Determine if the date should start with the month or day.
if (shortDatePattern.startsWith("m")) {
cell.setValue("1-2-2023");
console.log("Starst with m")
} else {
cell.setValue("2-1-2023");
console.log("starts with d")
}
if
and else
actually use different dates, if
uses Jan-2, else
actually uses Feb-1, which shows as 1-2-2023 as format d-m-jjjj
.
Thanks for explaining the behaviour. So you are saying that all dates set to cells should follow US culture. When set automatically formatting according to the default culture is applied? Can you give me a reference in the documentation where this behaviour is defined?
This is not what I read in DatetimeFormat documentation. There it states that the value should be set according to the culture settings, or is this documentation incorrect?
A small update which maybe helps
Creating a Date
object en converting pasting setting the string value to a cell also assumes US culture
function main(workbook: ExcelScript.Workbook) {
// 2023-01-02 on Cell A1
let dateRange = workbook.getActiveWorksheet().getRange("A1")
let date = new Date(2023, 0, 2)
dateRange.setValue(date.toLocaleDateString())
}
This means not only string values, but also values derived from the Date
object cannot be converted to Locale formats.
Hi @donlvMSFT, just a heads-up. Can you confirm it is correct or incorrect behaviour?
Provide required information needed to triage your issue
When pasting dates from Office Scripts into Excel which has locale
nl-NL
it assumes the dates are inmm-dd-yyyy
format and therefore dates are being converted incorrectly.Your Environment
Expected behavior
Using the DatetimeFormat Documentation Example
Current behavior
The value of the cell becomes
01-02-2023
which is first of february.This is also the case If I replace the
/
with-
Result
So the correct evaluation took place and the correct date was pasted, however Excel intepreted it incorrectly.