使用Office Script for Excel基于条件突出显示整行

ajsxfq5m  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(89)

我有编写vba的经验,但工作使用Excel在线多个工作人员,所以我一直在玩脚本,并坚持一些条件格式。下面是我到目前为止,但当然,它只突出显示包含文本“测试”的单元格,我希望它突出显示整行。
我是否需要添加另一行声明一个单独的范围,因为条件规则只适用于H列?我想有整个行突出显示黄色任何时候“测试”是从下拉框H列中选择我们有。

function main(workbook: ExcelScript.Workbook) {
    // Get the first column in the current worksheet.
    const currentSheet = workbook.getActiveWorksheet();
    const firstColumn = currentSheet.getRange("H:H");  

    // Add conditional formatting based on the text in the cells.
    const textConditionFormat = 
    firstColumn.addConditionalFormat(ExcelScript.ConditionalFormatType.containsText).getTextComparison();

    // Set the conditional format to provide a green fill.
    textConditionFormat.getFormat().getFill().setColor("Yellow");

    // Apply the condition rule that the text begins with "Test".
    const textRule: ExcelScript.ConditionalTextComparisonRule = {
    operator: ExcelScript.ConditionalTextOperator.beginsWith,
    text: "Test"
    };
    textConditionFormat.setRule(textRule);
}

字符串

eivnm1vs

eivnm1vs1#

您需要为所有单元格设置条件格式。请试试看。

function main(workbook: ExcelScript.Workbook) {
    let conditionalFormatting: ExcelScript.ConditionalFormat;
    let selectedSheet = workbook.getActiveWorksheet();
    conditionalFormatting = selectedSheet.getRange().addConditionalFormat(ExcelScript.ConditionalFormatType.custom);
    conditionalFormatting.getCustom().getFormat().getFill().setColor("Yellow");
    conditionalFormatting.getCustom().getRule().setFormula("=left($H1,4)=\"Test\"");
}

字符串

相关问题