简体   繁体   English

无法使用 Excel JavaScript API 设置 NumberFormat

[英]Can't set NumberFormat with Excel JavaScript API

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.我正在使用 excel Javascript API 在搜索文档后,仍然找不到我想要实现的解决方案。

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).因此,我想将所有内容设置为数字格式“文本”,因此 Excel 的自动格式不会与任何单元格的内容混淆(不会删除前导零或更改日期格式)。

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.但似乎工作表 object 不包含 Range 方法。

Instead I tried to use the getRange method on my table like this:相反,我尝试在我的表上使用 getRange 方法,如下所示:

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:(所以我还检查了constructionTable.getRange().range.format是否包含一个名为NumberFormat或numberFormat的方法,但它没有:(

Has anyone had any success doing this?有没有人成功做到这一点?

Welcome to Office JS, Tluther.欢迎来到 Office JS,Tluther。 Yes, worksheet dont have numberFormat API.是的,工作表没有numberFormat API。 if you want to set the whole sheet, you could use getUsedRange() to get the range that used in the worksheet.如果要设置整个工作表,可以使用getUsedRange()来获取工作表中使用的范围。 You can use range.numberFormat = '@' to set it to text;您可以使用 range.numberFormat = '@' 将其设置为文本;

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文件可以在这里找到

Now, the API numberFormat now should use format like '[][]', So how can l set the 'accounting' or 'general' by JS API.现在,API numberFormat 现在应该使用 '[][]' 之类的格式,那么如何通过 JS API 设置 'accounting' 或 'general'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM