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?

1
  • Side note: Worksheets("Sheet1").Range("A17").NumberFormat = "General" is VBA, not the JS API. Commented Apr 24, 2020 at 14:50

2 Answers 2

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

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

2 Comments

@ worked fine, but I can't seem to get it to do anything else how about numbers or currency can you give one or two more examples?
range.numberFormat = '0'; also worked for me for #'s thanks!
-1

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

Comments

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.