2

I'm using the excel Javascript API and after searching through the docs, still can't find a solution for what I'm trying to achieve.

So I want to set everything to have the number format "Text" so none of Excel's auto-formatting messes with any of the cells' content (no removing leading zeros or changing date formats).

The docs suggest changing the format via:

Worksheets("Sheet1").Range("A17").NumberFormat = "General"

But it seems the sheet object doesn't contain a Range method.

Instead I tried to use the getRange method on my table like this:

constructionTable.getRange().NumberFormat = "Text"

This didn't throw any errors, but didn't seem to do anything.

Formatting the table range had previously worked for stuff like this:

constructionTable.getRange().format.autofitColumns()

So I also checked whether constructionTable.getRange().range.format contains a method called NumberFormat or numberFormat, but it does not :(

Has anyone had any success doing this?

BigBen
  • 46,229
  • 7
  • 24
  • 40
Tluther
  • 87
  • 1
  • 5
  • Side note: `Worksheets("Sheet1").Range("A17").NumberFormat = "General"` is VBA, not the JS API. – BigBen Apr 24 '20 at 14:50

2 Answers2

5

Welcome to Office JS, Tluther. Yes, worksheet dont have numberFormat API. if you want to set the whole sheet, you could use getUsedRange() to get the range that used in the worksheet. You can use range.numberFormat = '@' to set it to text;

Here is the sample code:

  await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");

    const range = sheet.getUsedRange();
    range.numberFormat = '@';
    await context.sync();
  });

Document can be found at here

Raymond Lu
  • 2,178
  • 1
  • 6
  • 19
-1

Now, the API numberFormat now should use format like '[][]', So how can l set the 'accounting' or 'general' by JS API.

Xiao
  • 1