在 Excel 的 JavaScript 中检查单元格格式

问题描述 投票:0回答:1

Office Excel JavaScript 添加中,我想使用条件格式来格式化“检查单元格”。

我写了以下代码:

function CLCC() {
    Excel.run(function (ctx) {
        var range = ctx.workbook.getSelectedRange();
        range.format.horizontalAlignment = "Center";
        range.format.verticalAlignment = "Center";

        let conditionalFormatting: ExcelScript.ConditionalFormat;

        conditionalFormatting = range.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue);
        conditionalFormatting.getCellValue().setRule({ operator: ExcelScript.ConditionalCellValueOperator.equalTo, formula1: "=TRUE" });
        conditionalFormatting.getCellValue().getFormat().getFont().setColor("#808080");
        conditionalFormatting.getCellValue().getFormat().getFont().setBold(false);
        conditionalFormatting.getCellValue().getFormat().getFont().setItalic(true);
        conditionalFormatting.setStopIfTrue(false);
        conditionalFormatting.setPriority(0);

        conditionalFormatting = range.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue);
        conditionalFormatting.getCellValue().setRule({ operator: ExcelScript.ConditionalCellValueOperator.equalTo, formula1: "=FALSE" });
        conditionalFormatting.getCellValue().getFormat().getFill().setColor("#ffabab");
        conditionalFormatting.getCellValue().getFormat().getFont().setColor("#000000");
        conditionalFormatting.getCellValue().getFormat().getFont().setBold(true);
        conditionalFormatting.getCellValue().getFormat().getFont().setItalic(false);
        conditionalFormatting.setStopIfTrue(false);
        conditionalFormatting.setPriority(0);

        return ctx.sync()
    }).genericErrorHandler(error)
}

However, this code does not seem to work due to the following error: "Type annotations can only be used in TypeScript files" >> How can I rewrite this code to properly execute in an JS function file for an office add-in? I am expecting the code to execute as part of a custom ribbon excel add-in
excel office-js office-addins excel-web-addins
1个回答
0
投票

只需从行

let conditionalFormatting: ExcelScript.ConditionalFormat;
中删除类型声明即可。

© www.soinside.com 2019 - 2024. All rights reserved.