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, . 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.
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.