Home > OS >  How can i apply cell format dynamically by columns name?
How can i apply cell format dynamically by columns name?

Time:11-17

good day, my current problems here is to apply date and decimal format based on columns name.

`

            //Date Format
            new \Google\Service\Sheets\Request([
                "repeatCell" => [
                    "range" => [
                        "sheetId" => $sheet_id,
                        "startColumnIndex" => 1,
                        "endColumnIndex" => 3,
                    ],
                    "cell" => [
                        "userEnteredFormat" => [
                            "numberFormat" => [
                            "type" => "DATE",
                            "pattern" => "dd-mm-yyyy"
                            ]
                        ]
                    ],
                    "fields" => "userEnteredFormat.numberFormat",
                ],
            ]),

            //Decimal Format
            new \Google\Service\Sheets\Request([
                "repeatCell" => [
                    "range" => [
                        "sheetId" => $sheet_id,
                        "startColumnIndex" => 2,
                        "endColumnIndex" => (sizeof($this->dataCols->all())),
                    ],
                    "cell" => [
                        "userEnteredFormat" => [
                            "numberFormat" => [
                            "type" => "NUMBER",
                            "pattern" => "#,##0.00"
                            ]
                        ]
                    ],
                    "fields" => "userEnteredFormat.numberFormat",
                ],
            ]),

`

I already figure it out based on column range. My real question is which part do i need to modified to apply cell format based on column name? Thank you.

The sample:

date

This is date columns.

ammount

This is Amount column.

i have alots of columns name that goes by default datatype.Currently, i set the format based on range, for examples like setIndexColumns =>0, endIndexColumns =>3, . How do i set the datatype format by columns name? for examples, if the columns name Doc. Date, the whole column or the data will be shown as date format. if the columns name Amount(MYR), the whole column or data will be shown as decimal format.

CodePudding user response:

I believe your goal is as follows.

  • You want to set the number format to the columns by searching the column title. The request body of the number format is shown in your script.
  • In your Spreadsheet, the header row is row 3. And, you want to set the number format from row 3 to the last row.
  • You want to achieve this using googleapis for PHP.
  • You have already been able to get and put values to Google Spreadsheet using Sheets API.

In this case, I thought that the following flow is required to be done.

  1. Retrieve the header row (In your situation, it's row 3.).
  2. Create the request body by searching the column using the header title.
  3. Request Sheets API using the created request body.

When this flow is reflected in the sample script, it becomes as follows.

Sample script:

$service = ###; // Please use your client.
$spreadsheet_id = "###"; // please set Spreadsheet ID.
$sheet_name = "Sheet1"; // Please set the sheet name.
$sheet_id = "###"; // Please set the sheet ID of the sheet name.

// Object for searching the header title. This is from your showing script.
$obj = [
    "Doc. Date" => ["numberFormat" => ["type" => "DATE", "pattern" => "dd-mm-yyyy"]],
    "Amount (MYR)" => ["numberFormat" => ["type" => "NUMBER", "pattern" => "#,##0.00"]]
];

// Retrieve the header title.
$res1 = $service->spreadsheets_values->get($spreadsheet_id, "'" . $sheet_name . "'!A3:3");
$header = $res1["values"][0];

// Create a request body for batchUpdate.
$requests = [];
foreach ($header as $i => $h) {
    if (array_key_exists($h, $obj)) {
        array_push($requests, 
            new \Google\Service\Sheets\Request([
                "repeatCell" => [
                    "range" => [
                        "sheetId" => $sheet_id,
                        "startColumnIndex" => $i,
                        "endColumnIndex" => $i   1,
                        "startRowIndex" => 3,
                    ],
                    "cell" => ["userEnteredFormat" => $obj[$h]],
                    "fields" => "userEnteredFormat.numberFormat",
                ],
            ]),
        );
    };
}

// Request Sheets API using the created request body.
if (count($requests) > 0) {
  $batchUpdateCellFormatRequest = new \Google\Service\Sheets\BatchUpdateSpreadsheetRequest(["requests" => $requests]);
  $service->spreadsheets->batchUpdate($spreadsheet_id, $batchUpdateCellFormatRequest);
};
  • When this script is run, the header title is retrieved from row 3, and create a request body using the header title and object. And, request Sheets API using the request body.

Note:

  • If your actual header titles are different from $obj, please modify them for your actual header title.

References:

  • Related