简体   繁体   English

如何按列名动态应用单元格格式?

[英]How can i apply cell format dynamically by columns name?

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:例子:

日期

This is date columns.这是日期列。

数量

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, .我有很多默认数据类型的列名称。目前,我根据范围设置格式,例如 setIndexColumns =>0, endIndexColumns =>3, 。 How do i set the datatype format by columns name?如何按列名设置数据类型格式? for examples, if the columns name Doc.例如,如果列名为 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.如果列名称为 Amount(MYR),则整个列或数据将显示为十进制格式。

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.在您的电子表格中,header 行是第 3 行。并且,您想要设置从第 3 行到最后一行的数字格式。
  • You want to achieve this using googleapis for PHP.您想使用 PHP 的 googleapis 来实现此目的。
  • You have already been able to get and put values to Google Spreadsheet using Sheets API.您已经能够使用表格 API 获取值并将值放入 Google 电子表格。

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.).检索 header 行(在您的情况下,它是第 3 行)。
  2. Create the request body by searching the column using the header title.通过使用 header 标题搜索列来创建请求正文。
  3. Request Sheets API using the created request body.使用创建的请求正文请求表 API。

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.运行此脚本时,从第 3 行检索 header 标题,并使用 header 标题和 object 创建请求正文。然后,使用请求正文请求 Sheets API。

Note:笔记:

  • If your actual header titles are different from $obj , please modify them for your actual header title.如果您实际的 header 标题与$obj不同,请修改为您实际的 header 标题。

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM