办公室js条件格式

我目前正试图使用​​Office JS API 1.6在Excel中实现条件格式。我已经写了下面的代码来实现文本比较格式。

function textComparisonFormatting() { // Run a batch operation against the Excel object model Excel.run(function (ctx) { // Create a proxy object for the active worksheet var sheet = ctx.workbook.worksheets.getActiveWorksheet(); //Queue a command to write the sample data to the specified range //in the worksheet and bold the header row var range = sheet.getRange("A2:E8"); var conditionalFormat = range.conditionalFormats.add(Excel.ConditionalFormatType.textComparison); conditionalFormat.textComparison.load(["rule","format/*","format/fill"]); //Run the queued commands, and return a promise to indicate task completion return ctx.sync(conditionalFormat).then(function(conditionalFormat){ conditionalFormat.textComparison.rule.text = "Qtr"; conditionalFormat.textComparison.rule.operator = "BeginsWith"; conditionalFormat.textComparisonformat.fill.color = "red"; }); }) .then(function () { app.showNotification("Success"); console.log("Success!"); }) .catch(function (error) { // Always be sure to catch any accumulated errors that bubble up from the Excel.run execution app.showNotification("Error: " + error); console.log("Error: " + error); if (error instanceof OfficeExtension.Error) { console.log("Debug info: " + JSON.stringify(error.debugInfo)); } }); } 

代码抛出InvalidObjectPath错误,而我想设置颜色。 如果我尝试设置Excel.Run()内的颜色,那么它将无法正常工作,因为我无法访问对象属性。 有什么方法可以解决这些问题?

您应该对代码进行一些更改:

  1. 您不需要加载任何内容并进行同步,因为您正在写入属性,而不是读取它们。

  2. 没有ConditionalFormatType.textComparison 。 您需要ConditionalFormatType.containsText

  3. 运营商是骆驼式的: beginsWith ,而不是BeginsWith

  4. 应该有一个“。” textComparisonformat之间。

这个片段的作品:

 function applyTextFormat() { Excel.run(function (context) { var sheet = ctx.workbook.worksheets.getActiveWorksheet(); var range = sheet.getRange("A2:E8"); var conditionalFormat = range.conditionalFormats .add(Excel.ConditionalFormatType.containsText); conditionalFormat.textComparison.format.fill.color = "red"; conditionalFormat.textComparison.rule = { operator: Excel.ConditionalTextOperator.beginsWith, text: "Qtr" }; return context.sync(); }); } 

更新 :由于OP请求:文档尚未发布在dev.office.com上。 你需要去GitHub上的office-js-docs回购的特殊分支。 打开此页面,查看所有forms为conditional * .md的文件: ExcelJs_OpenSpec / reference / excel