You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

是否存在类似numberFormat的方法,传入数组设置表格字体颜色与粗细?

How to Batch Set Font Color & Boldness with Excel JavaScript API

Hey there! Great question—unfortunately, there’s no single property exactly like numberFormat that lets you pass a 2D array to set both font color and boldness in one shot. But don’t worry, you can still replicate this behavior using the Excel JS API’s built-in formatting properties, and it’s pretty straightforward once you know how.

Setting Font Color with a 2D Array

First, if your formats array is meant to define font colors, you can assign it directly to the range.format.font.color property. This works just like how numberFormat accepts arrays for number formatting:

const sheet = context.workbook.worksheets.getItem("Sample");
const colorFormats = [ 
  ["red", "red"], 
  ["red", "red"], 
  ["red", "red"] 
];
const range = sheet.getRange("D3:E5");

// Assign the color array to the font color property
range.format.font.color = colorFormats;
await context.sync(); // Critical step to apply changes to Excel!

Adding Boldness (If You Need It)

If you also want to set boldness for cells using another 2D array, you can do the same with the range.format.font.bold property. Just pass an array of booleans (true for bold, false for regular):

const boldFormats = [ 
  [true, false], 
  [false, true], 
  [true, true] 
];
range.format.font.bold = boldFormats;
await context.sync();

Combining Color & Boldness in a Single Structure

If you want to define both color and boldness in one array (like your original example implied), you’ll need to loop through each cell in the range and apply the formatting individually. Here’s a clean way to do that:

const sheet = context.workbook.worksheets.getItem("Sample");
// Define each cell's color and boldness in a 2D array
const cellFormats = [ 
  [{color: "red", bold: true}, {color: "blue", bold: false}], 
  [{color: "green", bold: false}, {color: "orange", bold: true}], 
  [{color: "purple", bold: true}, {color: "gray", bold: false}] 
];
const range = sheet.getRange("D3:E5");
const rows = range.getRows();

// Loop through rows and cells to apply formatting
for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
  const cells = rows[rowIndex].getCells();
  for (let cellIndex = 0; cellIndex < cells.length; cellIndex++) {
    cells[cellIndex].format.font.color = cellFormats[rowIndex][cellIndex].color;
    cells[cellIndex].format.font.bold = cellFormats[rowIndex][cellIndex].bold;
  }
}

await context.sync();

Quick Reminders:

  • Don’t skip context.sync(): This method sends all your script’s changes to the actual Excel workbook—without it, your formatting won’t show up.
  • Color values: You can use named colors ("red"), hex codes ("#FF0000"), or RGB values ("rgb(255,0,0)") in the color array.
  • Boldness values: Stick to true or false for the bold array to toggle the font weight.

内容的提问来源于stack exchange,提问作者Sami.S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:19:22