Search code examples
office-scriptsms-office-script

is there a function to fetch date values in excel using office script that will preserve the format?


main program objective : skim through a column of dates and identify the row of current date

below code snippet objective: to print the cell value in date format

function main(workbook: ExcelScript.Workbook){
let sheet = workbook.getActiveworksheet("rainbow")  // rainbow is the sheet name
let range = sheet.getRange("A1:A10) // taking only 10 dates for checking
let values=range.getValues();

for(let i=0; i<10; i++){
    console.log(values[i][0]) 

   }

}

The values of the column A are in the format:

enter image description here

when I expected the date to be printed as it is, the printed answers are

45495

45496

45497 etc

Can someone guide me where i am making mistake?

I tried searching in documentation but couldn;t find any function that can help me


Solution

  • Use range.getTexts() not range.getValues(), it will give you the value you see on screen, not the technical value behind the scenes.