Check Cell formatting in JavaScript for Excel

99 Views Asked by At

Within an Office Excel JavaScript Add in, I would like to use conditional formatting to format a "Check Cell".

I have written the following code:

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

Updated code base

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

    conditionalFormatting = range.addConditionalFormat(Excel.ConditionalFormatType.cellValue);
    conditionalFormatting.getCellValue().setRule({ operator: Excel.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(Excel.ConditionalFormatType.cellValue);
    conditionalFormatting.getCellValue().setRule({ operator: Excel.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)

}

1

There are 1 best solutions below

4
On

Just remove the type declaration from the line let conditionalFormatting: ExcelScript.ConditionalFormat;.