শর্তসাপেক্ষ বিন্যাসন

কন্ডিশনাল ফরম্যাটিং আপনাকে সেলগুলোকে এমনভাবে ফরম্যাট করতে দেয়, যাতে সেগুলোর ভেতরের মান বা অন্য সেলের মানের ওপর ভিত্তি করে সেলগুলোর চেহারা গতিশীলভাবে পরিবর্তিত হয়। কন্ডিশনাল ফরম্যাটিং-এর অনেক সম্ভাব্য প্রয়োগ রয়েছে, যার মধ্যে নিম্নলিখিত ব্যবহারগুলো অন্তর্ভুক্ত:

  • একটি নির্দিষ্ট সীমার উপরের সেলগুলো হাইলাইট করুন (উদাহরণস্বরূপ, $২,০০০-এর বেশি মূল্যের সমস্ত লেনদেনের জন্য বোল্ড টেক্সট ব্যবহার করে)।
  • সেলগুলোকে এমনভাবে ফরম্যাট করুন যাতে তাদের মানের সাথে রঙের পরিবর্তন হয় (উদাহরণস্বরূপ, $২,০০০-এর বেশি পরিমাণ বাড়ার সাথে সাথে আরও গাঢ় লাল ব্যাকগ্রাউন্ড প্রয়োগ করা)।
  • অন্যান্য সেলের বিষয়বস্তুর উপর ভিত্তি করে সেলগুলোকে গতিশীলভাবে ফরম্যাট করুন (উদাহরণস্বরূপ, সেইসব প্রপার্টির ঠিকানা হাইলাইট করা, যেগুলোর 'টাইম অন মার্কেট' ফিল্ডের মান ৯০ দিনের বেশি)।

আপনি এমনকি সেলগুলোর মান এবং অন্যান্য সেলের মানের উপর ভিত্তি করেও সেল ফরম্যাট করতে পারেন। উদাহরণস্বরূপ, আপনি একটি সেল রেঞ্জের মধ্যমা মানের সাথে তাদের মানের তুলনা করে সেল রেঞ্জটি ফরম্যাট করতে পারেন:

মধ্যকের উপরে বা নীচে মানগুলি হাইলাইট করার জন্য বিন্যাস বয়স

চিত্র ১. মধ্যক বয়সের উপরে বা নীচের মানগুলিকে হাইলাইট করার জন্য বিন্যাস।

এই উদাহরণে, প্রতিটি সারির সেলগুলোকে তাদের age কলামের মান সমস্ত বয়সের মধ্যক মানের সাথে তুলনা করে ফরম্যাট করা হয়েছে। যে সারিগুলোর বয়স মধ্যকের উপরে, সেগুলোর লেখা লাল রঙের এবং যেগুলোর বয়স মধ্যকের নিচে, সেগুলোর পটভূমি লাল রঙের। দুটি সারির age মান মধ্যক বয়সের (৪৮) সাথে মিলে যায় এবং এই সেলগুলোতে কোনো বিশেষ ফরম্যাটিং করা হয় না। (এই শর্তসাপেক্ষ ফরম্যাটিং তৈরি করার সোর্স কোডের জন্য, নিচের উদাহরণটি দেখুন।)

শর্তসাপেক্ষ ফরম্যাটিং নিয়ম

শর্তসাপেক্ষ ফরম্যাটিং, ফরম্যাটিং নিয়ম ব্যবহার করে প্রকাশ করা হয়। প্রতিটি স্প্রেডশিট এই নিয়মগুলোর একটি তালিকা সংরক্ষণ করে এবং তালিকায় যে ক্রমে সেগুলো থাকে, ঠিক সেই ক্রমেই প্রয়োগ করে। গুগল শিটস এপিআই আপনাকে এই ফরম্যাটিং নিয়মগুলো যোগ করতে, আপডেট করতে এবং মুছে ফেলতে দেয়।

প্রতিটি নিয়মে একটি লক্ষ্যমাত্রা পরিসীমা, নিয়মের ধরণ, নিয়মটি কার্যকর হওয়ার শর্তাবলী এবং প্রযোজ্য যেকোনো বিন্যাস নির্দিষ্ট করা থাকে।

লক্ষ্য পরিসর — এটি একটি একক সেল, একাধিক সেলের একটি পরিসর, বা একাধিক পরিসর হতে পারে।

নিয়মের প্রকারভেদ — নিয়ম দুই প্রকারের হয়:

পরবর্তী বিভাগগুলিতে যেমন বিস্তারিতভাবে বর্ণনা করা হয়েছে, এই প্রতিটি নিয়মের প্রকারের জন্য যে শর্তগুলি মূল্যায়ন করা হয় এবং যে বিন্যাসগুলি আপনি প্রয়োগ করতে পারেন, তা ভিন্ন ভিন্ন।

বুলিয়ান নিয়ম

একটি BooleanRule নির্ধারণ করে যে একটি নির্দিষ্ট ফরম্যাট প্রয়োগ করা হবে কিনা, যা একটি BooleanCondition উপর ভিত্তি করে নির্ধারিত হয় এবং এই Condition-টির মান true বা false । একটি বুলিয়ান রুলের গঠনটি হলো:

{
  "condition": {
    object(BooleanCondition)
  },
  "format": {
    object(CellFormat)
  },
}

শর্তটি নির্ধারণের জন্য বিল্ট-ইন ConditionType ব্যবহার করা যেতে পারে, অথবা আরও জটিল মূল্যায়নের জন্য একটি কাস্টম ফর্মুলাও ব্যবহার করা যেতে পারে।

বিল্ট-ইন টাইপগুলো আপনাকে সাংখ্যিক সীমা, টেক্সট তুলনা, বা সেলটিতে ডেটা আছে কিনা তার উপর ভিত্তি করে ফরম্যাটিং প্রয়োগ করতে দেয়। উদাহরণস্বরূপ, NUMBER_GREATER মানে হলো সেলটির মান অবশ্যই শর্তের মানের চেয়ে বেশি হতে হবে। নিয়মগুলো সর্বদা টার্গেট সেলের সাপেক্ষে মূল্যায়ন করা হয়।

কাস্টম ফর্মুলা হলো এক বিশেষ ধরনের কন্ডিশন, যা আপনাকে যেকোনো এক্সপ্রেশন অনুযায়ী ফরম্যাটিং প্রয়োগ করতে দেয় এবং শুধু টার্গেট সেল নয়, যেকোনো সেলের ইভ্যালুয়েশনও অনুমোদন করে। কন্ডিশনটির ফর্মুলার ইভ্যালুয়েশন অবশ্যই ' true হতে হবে।

একটি বুলিয়ান নিয়ম দ্বারা প্রয়োগ করা ফরম্যাটিং সংজ্ঞায়িত করতে, আপনি CellFormat টাইপের একটি উপসেট ব্যবহার করেন:

  • সেলের ভেতরের লেখাটি বোল্ড, ইটালিক, বা স্ট্রাইকথ্রু কিনা।
  • সেলের ভেতরের লেখার রঙ।
  • সেলটির পটভূমির রঙ।

গ্রেডিয়েন্ট নিয়ম

একটি GradientRule বিভিন্ন মানের সাথে সঙ্গতিপূর্ণ রঙের একটি পরিসর নির্ধারণ করে। একটি গ্রেডিয়েন্ট রুলের গঠনটি হলো:

{
  "minpoint": {
    object(InterpolationPoint)
  },
  "midpoint": {
    object(InterpolationPoint)
  },
  "maxpoint": {
    object(InterpolationPoint)
  },
}

প্রতিটি InterpolationPoint একটি রঙ এবং তার সংশ্লিষ্ট মান নির্ধারণ করে। তিনটি পয়েন্টের একটি সেট একটি রঙের গ্রেডিয়েন্ট তৈরি করে।

শর্তসাপেক্ষ ফরম্যাটিং নিয়মগুলি পরিচালনা করুন

কন্ডিশনাল ফরম্যাটিং নিয়ম তৈরি, পরিবর্তন বা মুছে ফেলার জন্য, উপযুক্ত রিকোয়েস্ট টাইপ সহ spreadsheets.batchUpdate মেথডটি ব্যবহার করুন:

  • AddConditionalFormatRuleRequest ব্যবহার করে প্রদত্ত ইন্ডেক্সে তালিকায় নিয়মগুলো যোগ করুন।

  • UpdateConditionalFormatRuleRequest ব্যবহার করে তালিকার নির্দিষ্ট ইন্ডেক্সে থাকা নিয়মগুলো প্রতিস্থাপন বা পুনর্বিন্যাস করুন।

  • ` DeleteConditionalFormatRuleRequest ব্যবহার করে প্রদত্ত ইন্ডেক্স থেকে তালিকা থেকে নিয়মগুলি মুছে ফেলুন।

উদাহরণ

নিম্নলিখিত উদাহরণে দেখানো হয়েছে কীভাবে এই পৃষ্ঠার শীর্ষে থাকা স্ক্রিনশটে প্রদর্শিত কন্ডিশনাল ফরম্যাটিং তৈরি করতে হয়। আরও উদাহরণের জন্য, ‘কন্ডিশনাল ফরম্যাটিং স্যাম্পলস’ পৃষ্ঠাটি দেখুন।

অ্যাপস স্ক্রিপ্ট

sheets/api/spreadsheet_snippets.gs
/**
 * conditional formatting
 * @param {string} spreadsheetId spreadsheet ID
 * @returns {*} spreadsheet
 */
Snippets.prototype.conditionalFormatting = (spreadsheetId) => {
  try {
    const myRange = Sheets.newGridRange();
    myRange.sheetId = 0;
    myRange.startRowIndex = 0;
    myRange.endRowIndex = 11;
    myRange.startColumnIndex = 0;
    myRange.endColumnIndex = 4;

    // Request 1
    const rule1ConditionalValue = Sheets.newConditionValue();
    rule1ConditionalValue.userEnteredValue = "=GT($D2,median($D$2:$D$11))";

    const rule1ConditionFormat = Sheets.newCellFormat();
    rule1ConditionFormat.textFormat = Sheets.newTextFormat();
    rule1ConditionFormat.textFormat.foregroundColor = Sheets.newColor();
    rule1ConditionFormat.textFormat.foregroundColor.red = 0.8;

    const rule1Condition = Sheets.newBooleanCondition();
    rule1Condition.type = "CUSTOM_FORMULA";
    rule1Condition.values = [rule1ConditionalValue];

    const rule1BooleanRule = Sheets.newBooleanRule();
    rule1BooleanRule.condition = rule1Condition;
    rule1BooleanRule.format = rule1ConditionFormat;

    const rule1 = Sheets.newConditionalFormatRule();
    rule1.ranges = [myRange];
    rule1.booleanRule = rule1BooleanRule;

    const request1 = Sheets.newRequest();
    const addConditionalFormatRuleRequest1 =
      Sheets.newAddConditionalFormatRuleRequest();
    addConditionalFormatRuleRequest1.rule = rule1;
    addConditionalFormatRuleRequest1.index = 0;
    request1.addConditionalFormatRule = addConditionalFormatRuleRequest1;

    // Request 2
    const rule2ConditionalValue = Sheets.newConditionValue();
    rule2ConditionalValue.userEnteredValue = "=LT($D2,median($D$2:$D$11))";

    const rule2ConditionFormat = Sheets.newCellFormat();
    rule2ConditionFormat.textFormat = Sheets.newTextFormat();
    rule2ConditionFormat.textFormat.foregroundColor = Sheets.newColor();
    rule2ConditionFormat.textFormat.foregroundColor.red = 1;
    rule2ConditionFormat.textFormat.foregroundColor.green = 0.4;
    rule2ConditionFormat.textFormat.foregroundColor.blue = 0.4;

    const rule2Condition = Sheets.newBooleanCondition();
    rule2Condition.type = "CUSTOM_FORMULA";
    rule2Condition.values = [rule2ConditionalValue];

    const rule2BooleanRule = Sheets.newBooleanRule();
    rule2BooleanRule.condition = rule2Condition;
    rule2BooleanRule.format = rule2ConditionFormat;

    const rule2 = Sheets.newConditionalFormatRule();
    rule2.ranges = [myRange];
    rule2.booleanRule = rule2BooleanRule;

    const request2 = Sheets.newRequest();
    const addConditionalFormatRuleRequest2 =
      Sheets.newAddConditionalFormatRuleRequest();
    addConditionalFormatRuleRequest2.rule = rule2;
    addConditionalFormatRuleRequest2.index = 0;
    request2.addConditionalFormatRule = addConditionalFormatRuleRequest2;

    // Batch send the requests
    const requests = [request1, request2];
    const batchUpdate = Sheets.newBatchUpdateSpreadsheetRequest();
    batchUpdate.requests = requests;
    const response = Sheets.Spreadsheets.batchUpdate(
      batchUpdate,
      spreadsheetId,
    );
    return response;
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log("Failed with error %s", err.message);
  }
};

জাভা

sheets/snippets/src/main/java/ConditionalFormatting.java
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.AddConditionalFormatRuleRequest;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetResponse;
import com.google.api.services.sheets.v4.model.BooleanCondition;
import com.google.api.services.sheets.v4.model.BooleanRule;
import com.google.api.services.sheets.v4.model.CellFormat;
import com.google.api.services.sheets.v4.model.Color;
import com.google.api.services.sheets.v4.model.ConditionValue;
import com.google.api.services.sheets.v4.model.ConditionalFormatRule;
import com.google.api.services.sheets.v4.model.GridRange;
import com.google.api.services.sheets.v4.model.Request;
import com.google.api.services.sheets.v4.model.TextFormat;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/* Class to demonstrate the use of Spreadsheet Conditional Formatting API */
public class ConditionalFormatting {
  /**
   * Create conditional formatting.
   *
   * @param spreadsheetId - Id of the spreadsheet.
   * @return updated changes count.
   * @throws IOException - if credentials file not found.
   */
  public static BatchUpdateSpreadsheetResponse conditionalFormat(String spreadsheetId)
      throws IOException {
        /* Load pre-authorized user credentials from the environment.
           TODO(developer) - See https://developers.google.com/identity for
            guides on implementing OAuth2 for your application. */
    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
        .createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS));
    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
        credentials);

    // Create the sheets API client
    Sheets service = new Sheets.Builder(new NetHttpTransport(),
        GsonFactory.getDefaultInstance(),
        requestInitializer)
        .setApplicationName("Sheets samples")
        .build();

    List<GridRange> ranges = Collections.singletonList(new GridRange()
        .setSheetId(0)
        .setStartRowIndex(1)
        .setEndRowIndex(11)
        .setStartColumnIndex(0)
        .setEndColumnIndex(4)
    );
    List<Request> requests = Arrays.asList(
        new Request().setAddConditionalFormatRule(new AddConditionalFormatRuleRequest()
            .setRule(new ConditionalFormatRule()
                .setRanges(ranges)
                .setBooleanRule(new BooleanRule()
                    .setCondition(new BooleanCondition()
                        .setType("CUSTOM_FORMULA")
                        .setValues(Collections.singletonList(
                            new ConditionValue().setUserEnteredValue(
                                "=GT($D2,median($D$2:$D$11))")
                        ))
                    )
                    .setFormat(new CellFormat().setTextFormat(
                        new TextFormat().setForegroundColor(
                            new Color().setRed(0.8f))
                    ))
                )
            )
            .setIndex(0)
        ),
        new Request().setAddConditionalFormatRule(new AddConditionalFormatRuleRequest()
            .setRule(new ConditionalFormatRule()
                .setRanges(ranges)
                .setBooleanRule(new BooleanRule()
                    .setCondition(new BooleanCondition()
                        .setType("CUSTOM_FORMULA")
                        .setValues(Collections.singletonList(
                            new ConditionValue().setUserEnteredValue(
                                "=LT($D2,median($D$2:$D$11))")
                        ))
                    )
                    .setFormat(new CellFormat().setBackgroundColor(
                        new Color().setRed(1f).setGreen(0.4f).setBlue(0.4f)
                    ))
                )
            )
            .setIndex(0)
        )
    );

    BatchUpdateSpreadsheetResponse result = null;
    try {
      // Execute the requests.
      BatchUpdateSpreadsheetRequest body =
          new BatchUpdateSpreadsheetRequest()
              .setRequests(requests);
      result = service.spreadsheets()
          .batchUpdate(spreadsheetId, body)
          .execute();
      System.out.printf("%d cells updated.", result.getReplies().size());
    } catch (GoogleJsonResponseException e) {
      // TODO(developer) - handle error appropriately
      GoogleJsonError error = e.getDetails();
      if (error.getCode() == 404) {
        System.out.printf("Spreadsheet not found with id '%s'.\n", spreadsheetId);
      } else {
        throw e;
      }
    }
    return result;
  }
}

জাভাস্ক্রিপ্ট

sheets/snippets/sheets_conditional_formatting.js
function conditionalFormatting(spreadsheetId, callback) {
  const myRange = {
    sheetId: 0,
    startRowIndex: 1,
    endRowIndex: 11,
    startColumnIndex: 0,
    endColumnIndex: 4,
  };
  const requests = [{
    addConditionalFormatRule: {
      rule: {
        ranges: [myRange],
        booleanRule: {
          condition: {
            type: 'CUSTOM_FORMULA',
            values: [{userEnteredValue: '=GT($D2,median($D$2:$D$11))'}],
          },
          format: {
            textFormat: {foregroundColor: {red: 0.8}},
          },
        },
      },
      index: 0,
    },
  }, {
    addConditionalFormatRule: {
      rule: {
        ranges: [myRange],
        booleanRule: {
          condition: {
            type: 'CUSTOM_FORMULA',
            values: [{userEnteredValue: '=LT($D2,median($D$2:$D$11))'}],
          },
          format: {
            backgroundColor: {red: 1, green: 0.4, blue: 0.4},
          },
        },
      },
      index: 0,
    },
  }];

  const body = {
    requests,
  };
  try {
    gapi.client.sheets.spreadsheets.batchUpdate({
      spreadsheetId: spreadsheetId,
      resource: body,
    }).then((response) => {
      const result = response.result;
      console.log(`${result.replies.length} cells updated.`);
      if (callback) callback(response);
    });
  } catch (err) {
    document.getElementById('content').innerText = err.message;
    return;
  }
}

নোড.জেএস

sheets/snippets/sheets_conditional_formatting.js
import {GoogleAuth} from 'google-auth-library';
import {google} from 'googleapis';

/**
 * Applies conditional formatting to a spreadsheet.
 * @param {string} spreadsheetId The ID of the spreadsheet.
 * @return {Promise<object>} The response from the batch update.
 */
async function conditionalFormatting(spreadsheetId) {
  // Authenticate with Google and get an authorized client.
  const auth = new GoogleAuth({
    scopes: 'https://www.googleapis.com/auth/spreadsheets',
  });

  // Create a new Sheets API client.
  const service = google.sheets({version: 'v4', auth});

  // The range to apply the conditional formatting to.
  const myRange = {
    sheetId: 0,
    startRowIndex: 1,
    endRowIndex: 11,
    startColumnIndex: 0,
    endColumnIndex: 4,
  };

  // The requests to apply conditional formatting.
  const requests = [
    {
      addConditionalFormatRule: {
        rule: {
          ranges: [myRange],
          booleanRule: {
            condition: {
              type: 'CUSTOM_FORMULA',
              values: [{userEnteredValue: '=GT($D2,median($D$2:$D$11))'}],
            },
            format: {
              textFormat: {foregroundColor: {red: 0.8}},
            },
          },
        },
        index: 0,
      },
    },
    {
      addConditionalFormatRule: {
        rule: {
          ranges: [myRange],
          booleanRule: {
            condition: {
              type: 'CUSTOM_FORMULA',
              values: [{userEnteredValue: '=LT($D2,median($D$2:$D$11))'}],
            },
            format: {
              backgroundColor: {red: 1, green: 0.4, blue: 0.4},
            },
          },
        },
        index: 0,
      },
    },
  ];

  // Create the batch update request.
  const resource = {
    requests,
  };

  // Execute the batch update request.
  const response = await service.spreadsheets.batchUpdate({
    spreadsheetId,
    resource,
  });
  console.log(`${response.data.replies.length} cells updated.`);
  return response;
}

পিএইচপি

sheets/snippets/src/SpreadsheetConditionalFormatting.php
<?php
use Google\Client;
use Google\Service\Drive;
use Google\Service\Sheets\BatchUpdateSpreadsheetRequest;
use Google\Service\Sheets\Request;

function conditionalFormatting($spreadsheetId)
    {
        /* Load pre-authorized user credentials from the environment.
           TODO(developer) - See https://developers.google.com/identity for
            guides on implementing OAuth2 for your application. */
        $client = new Google\Client();
        $client->useApplicationDefaultCredentials();
        $client->addScope(Google\Service\Drive::DRIVE);
        $service = new Google_Service_Sheets($client);

        try{
            $myRange = [
                'sheetId' => 0,
                'startRowIndex' => 1,
                'endRowIndex' => 11,
                'startColumnIndex' => 0,
                'endColumnIndex' => 4,
            ];
            //execute the request
            $requests = [
                new Google_Service_Sheets_Request([
                'addConditionalFormatRule' => [
                    'rule' => [
                        'ranges' => [ $myRange ],
                        'booleanRule' => [
                            'condition' => [
                                'type' => 'CUSTOM_FORMULA',
                                'values' => [ [ 'userEnteredValue' => '=GT($D2,median($D$2:$D$11))' ] ]
                            ],
                            'format' => [
                                'textFormat' => [ 'foregroundColor' => [ 'red' => 0.8 ] ]
                                ]
                                ]
                            ],
                            'index' => 0
                            ]
                        ]),
                        new Google_Service_Sheets_Request([
                'addConditionalFormatRule' => [
                    'rule' => [
                        'ranges' => [ $myRange ],
                        'booleanRule' => [
                            'condition' => [
                                'type' => 'CUSTOM_FORMULA',
                                'values' => [ [ 'userEnteredValue' => '=LT($D2,median($D$2:$D$11))' ] ]
                            ],
                            'format' => [
                                'backgroundColor' => [ 'red' => 1, 'green' => 0.4, 'blue' => 0.4 ]
                            ]
                            ]
                        ],
                    'index' => 0
                ]
                ])
        ];

        $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
            'requests' => $requests
        ]);
        $response = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);
        printf("%d cells updated.", count($response->getReplies()));
        return $response;
    }
    catch(Exception $e) {
        // TODO(developer) - handle error appropriately
        echo 'Message: ' .$e->getMessage();
    }
}

পাইথন

sheets/snippets/sheets_conditional_formatting.py
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def conditional_formatting(spreadsheet_id):
  """
  Creates the batch_update the user has access to.
  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """
  creds, _ = google.auth.default()
  # pylint: disable=maybe-no-member
  try:
    service = build("sheets", "v4", credentials=creds)

    my_range = {
        "sheetId": 0,
        "startRowIndex": 1,
        "endRowIndex": 11,
        "startColumnIndex": 0,
        "endColumnIndex": 4,
    }
    requests = [
        {
            "addConditionalFormatRule": {
                "rule": {
                    "ranges": [my_range],
                    "booleanRule": {
                        "condition": {
                            "type": "CUSTOM_FORMULA",
                            "values": [
                                {
                                    "userEnteredValue": (
                                        "=GT($D2,median($D$2:$D$11))"
                                    )
                                }
                            ],
                        },
                        "format": {
                            "textFormat": {"foregroundColor": {"red": 0.8}}
                        },
                    },
                },
                "index": 0,
            }
        },
        {
            "addConditionalFormatRule": {
                "rule": {
                    "ranges": [my_range],
                    "booleanRule": {
                        "condition": {
                            "type": "CUSTOM_FORMULA",
                            "values": [
                                {
                                    "userEnteredValue": (
                                        "=LT($D2,median($D$2:$D$11))"
                                    )
                                }
                            ],
                        },
                        "format": {
                            "backgroundColor": {
                                "red": 1,
                                "green": 0.4,
                                "blue": 0.4,
                            }
                        },
                    },
                },
                "index": 0,
            }
        },
    ]
    body = {"requests": requests}
    response = (
        service.spreadsheets()
        .batchUpdate(spreadsheetId=spreadsheet_id, body=body)
        .execute()
    )
    print(f"{(len(response.get('replies')))} cells updated.")
    return response

  except HttpError as error:
    print(f"An error occurred: {error}")
    return error


if __name__ == "__main__":
  # Pass: spreadsheet_id
  conditional_formatting("1CM29gwKIzeXsAppeNwrc8lbYaVMmUclprLuLYuHog4k")

রুবি

sheets/snippets/lib/spreadsheet_snippets.rb
my_range = {
  sheet_id:           0,
  start_row_index:    1,
  end_row_index:      11,
  start_column_index: 0,
  end_column_index:   4
}
requests = [{
  add_conditional_format_rule: {
    rule:  {
      ranges:       [my_range],
      boolean_rule: {
        condition: {
          type:   'CUSTOM_FORMULA',
          values: [{ user_entered_value: '=GT($D2,median($D$2:$D$11))' }]
        },
        format:    {
          text_format: { foreground_color: { red: 0.8 } }
        }
      }
    },
    index: 0
  }
}, {
  add_conditional_format_rule: {
    rule:  {
      ranges:       [my_range],
      boolean_rule: {
        condition: {
          type:   'CUSTOM_FORMULA',
          values: [{ user_entered_value: '=LT($D2,median($D$2:$D$11))' }]
        },
        format:    {
          background_color: { red: 1, green: 0.4, blue: 0.4 }
        }
      }
    },
    index: 0
  }
}]
body = {
  requests: requests
}
batch_update = Google::Apis::SheetsV4::BatchUpdateSpreadsheetRequest.new
batch_update.requests = requests
result = service.batch_update_spreadsheet(spreadsheet_id, batch_update)
puts "#{result.replies.length} cells updated."