[英]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.我相信你的目标如下。
In this case, I thought that the following flow is required to be done.在这种情况下,我认为需要完成以下流程。
When this flow is reflected in the sample script, it becomes as follows.当这个流程反映在示例脚本中时,它变成如下。
$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);
};
$obj
, please modify them for your actual header title.$obj
不同,请修改为您实际的 header 标题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.