服务端 API/云文档/电子表格/工作表 - 条件格式/创建条件格式
# 创建条件格式

该接口用于创建新的条件格式，单次最多支持增加10个条件格式，每个条件格式的设置会返回成功或者失败，失败的情况包括各种参数的校验。

## 请求

基本 | &nbsp;
---|---
HTTP URL | https://open.larksuite.com/open-apis/sheets/v2/spreadsheets/:spreadsheetToken/condition_formats/batch_create
HTTP Method | POST
支持的应用类型 | Custom App、Store App
权限要求<br>**调用该 API 所需的权限。开启其中任意一项权限即可调用**<br>开启任一权限即可 | 查看、评论、编辑和管理云空间中所有文件(drive:drive)<br>查看、评论、编辑和管理电子表格(sheets:spreadsheet)

### 请求头

名称 | 类型 | 必填 | 描述
---|---|---|---
Authorization | string | 是 | `user_access_token` 或 `tenant_access_token`<br>**值格式**："Bearer `access_token`"<br>**示例值**："Bearer u-7f1bcd13fc57d46bac21793a18e560"<br>[了解更多：获取与使用access_token](https://open.larksuite.com/document/ukTMukTMukTM/uMTNz4yM1MjLzUzM)
Content-Type | string | 是 | **固定值**："application/json; charset=utf-8"

### 路径参数

参数 | 类型 | 描述
---|---|---
spreadsheetToken | string | spreadsheet 的 token，获取方式见[在线表格开发指南](https://open.larksuite.com/document/ukTMukTMukTM/uATMzUjLwEzM14CMxMTN/overview)

### 请求体
| 参数                            |类型|必需| 说明                                       |
| ----------------------------- |-----|----| ---------------------------------------- |
|sheet_condition_formats|array<interface>|是|表格的条件格式信息|
|&emsp;∟sheet_id                      |string|是| sheet的id                                 |
|&emsp;∟condition_format              ||是| 一个条件格式的详细信息                              |
|&emsp;&emsp;∟ranges                 |array<string>| 是| 条件格式应用的范围，支持：sheetId（整表）；sheetId!1:2（整行）；sheetId!A:B（整列）；sheetId!A1:B2（普通范围）；sheetId!A1:C（应用至最后一行）。应用范围不能超过表格的行总数和列总数，sheetId要与参数的sheetId一致 |
|&emsp;&emsp;∟rule_type              |string| 是| 条件格式规则类型，目前只有7种：***containsBlanks（为空）、notContainsBlanks（不为空）、duplicateValues（重复值）、uniqueValues（唯一值）、cellIs（限定值范围）、containsText（包含内容）、timePeriod（日期）***                                 |
|&emsp;&emsp;∟attrs                  || 是| rule_type对应的具体属性信息，详见 [条件格式指南](https://open.larksuite.com/document/ukTMukTMukTM/uATMzUjLwEzM14CMxMTN/conditionformat/condition-format-guide) |
|&emsp;&emsp;&emsp;∟operator|string| 否|操作方法|
|&emsp;&emsp;&emsp;∟time_period|string| 否|时间范围|
|&emsp;&emsp;&emsp;∟formula|array<string>| 否|格式|
|&emsp;&emsp;&emsp;∟text|string| 否|文本|
|&emsp;&emsp;∟style                  ||否| 条件格式样式，只支持以下样式，以下样式每个参数都可选，但是不能设置空的style |
|&emsp;&emsp; &emsp;∟font            || 否| 字体样式                                     |
|&emsp;&emsp; &emsp;&emsp;∟bold     |bool| 否| 加粗                                       |
|&emsp;&emsp;&emsp; &emsp;∟italic   |bool| 否| 斜体                                       |
|&emsp;&emsp; &emsp;∟text_decoration |int| 否| 文本装饰 ，0 默认，1 下划线，2 删除线 ，3 下划线和删除线        |
|&emsp;&emsp; &emsp;∟fore_color      |string| 否| 字体颜色                                     |
|&emsp;&emsp; &emsp;∟back_color      |string| 否| 背景颜色                                     |

### 请求体示例

```json
{
    "sheet_condition_formats": [
        {
            "sheet_id": "40a7b0",
            "condition_format": {
                "ranges": [
                    "40a7b0!C3:C3"
                ],
                "rule_type": "timePeriod",
                "attrs": [
                    {
                        "operator": "is",
                        "time_period": "today", //rule_type为timePeriod时必填
                        "formula": [], //rule_type为cellIs时必填
                        "text": "" //rule_type为containsText时必填
                    }
                ],
                "style": {
                    "font": {
                        "bold": true,
                        "italic": true
                    },
                    "fore_color": "#faf1d1",
                    "back_color": "#d9f5d6",
                    "text_decoration": 3
                }
            }
        }
    ]
}
```
###  cURL 请求示例
```
curl --location --request POST 'https://open.larksuite.com/open-apis/sheets/v2/spreadsheets/shtcngNygNfuqhxTBf588jwgWbJ/condition_formats/batch_create' \
--header 'Authorization: Bearer t-e346617a4acfc3a11d4ed24dca0d0c0fc8e0067e' \
--header 'Content-Type: application/json' \
--data-raw '{
    "sheet_condition_formats": [
        {
            "sheet_id": "Q7PlXT",
            "condition_format": {
                "ranges": [
                    "Q7PlXT!C3:D9"
                ],
                "rule_type": "uniqueValues",
                "style": {
                    "font": {
                        "bold": true,
                        "italic": true
                    },
                    "fore_color": "#faf1d1",
                    "back_color": "#d9f5d6",
                    "text_decoration": 3
                }
            }
        }
    ]
}'
```
 ## 响应
### 响应体
| 参数       | 类型|说明                           |
| -------- |-----| ---------------------------- |
|responses|array<interface>|响应|
| &emsp;∟sheet_id |string | sheet的Id                     |
| &emsp;∟cf_id    |string| 设置成功的条件格式id                  |
| &emsp;∟res_code |int| 条件格式设置状态码，0表示成功，非0表示失败       |
| &emsp;∟res_msg  |string| 条件格式设置返回的状态信息，空表示成功，非空表示失败原因 |

### 响应体示例

```json
{
    "code": 0,
    "msg": "Success",
    "data": {
        "responses": [
            {
                "cf_id": "1gJelvenW9",
                "res_code": 0,
                "res_msg": "success",
                "sheet_id": "40a7b0"
            }
        ]
    }
}
```

### 错误码

具体可参考：[服务端错误码说明](https://open.larksuite.com/document/ukTMukTMukTM/ugjM14COyUjL4ITN)