0

I've got a range that is originally formatted as "General" or @ and I've tried to convert it back to numbers. Technically, it's working, the cell format shows as "numbers" in Excel. But, the only way I can get the numbers to behave correctly (show the sum in the bottom) is by using the "Convert to Number" function in Excel.

How can I access this functionality programmaticaly in Javasciprt/Excel API?

enter image description here

Here is what I'm using currently:

var ws = context.workbook.worksheets.getActiveWorksheet();
var Used_Rng_And_Props = await Ranges.Get_Used_Rng_And_Props(context, ws, false)

//Set Revcd_Wt as Number
var recvdwt_col_index = await Ranges.Get_Header_Col_Index(context, Used_Rng_And_Props, "Recvd_wt")
console.log('recvdwt_col_index:' + recvdwt_col_index)
var rng = await Ranges.Get_Entire_Col_Rng(ws, recvdwt_col_index)
rng.numberFormat = "0.00"
rng.select()
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
FreeSoftwareServers
  • 2,271
  • 1
  • 33
  • 57

1 Answers1

0

Interstingly, I found this --> https://learn.microsoft.com/en-us/javascript/api/excel/excel.range?view=excel-js-preview#excel-excel-range-convertdatatypetotext-member(1)

But I need the opposite. After trying Excel online, I noticed the same functionality didn't exist (no "Convert to Number") and I am guessing this isn't possible via the Excel API as it has to be consistent between versions. But, perhaps I'm wrong, so I'll leave it to better minds.

As it stands, I basically wrote a function to get the rng.values, switch the rng.numberFormat back to 0.00 then re-wrote the values. This resolved the issue, but will add latency as I added extra context.sync.

//Set Revcd_Wt back to Number
var recvdwt_col_index = await Ranges.Get_Header_Col_Index(context, Used_Rng_And_Props, "Recvd_wt")
var rng = ws.getRangeByIndexes(1, recvdwt_col_index, Used_Rng_And_Props.rowCount - 1, 1)
rng.load('values')
await context.sync()
var rng_vals = rng.values
rng.numberFormat = '0.00'
rng.values = rng_vals
FreeSoftwareServers
  • 2,271
  • 1
  • 33
  • 57