Changing Cell Colour With Google Sheets API (PHP) - php

I'm trying to change a ranges colour via the Google Sheets API in PHP.
I have done around an hour of researchig. The code below is as far as I've got.
$requests = [
// Change the spreadsheet's title.
new Google_Service_Sheets_Request([
'updateSpreadsheetProperties' => [
'properties' => [
'title' => "The Title"
],
'fields' => 'title'
],
'UpdateCellsRequest' => [
'properties' => [
'range' => "Sheet1!A1",
'backgroundColor' => "#000"
],
'fields' => ''
]
])
];
// Add additional requests (operations) ...
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
'requests' => $requests
]);
$response = $GoogleSheetsAPIHandler->sheets->spreadsheets->batchUpdate("SHEETID", $batchUpdateRequest);
print_r($response);
If I take out this:
'UpdateCellsRequest' => [
'properties' => [
'range' => "Sheet1!A1",
'backgroundColor' => "#000"
],
'fields' => ''
]
Then the code works to update the sheets title. However, I can't seem to update a ranges colour.
Any advice would be greatly appreciated!

I believe your goal and situation as follows.
You want to change the background color of cells using googleapis for php.
You have already been able to get and put values for Google Spreadsheet using Sheets API.
Modification points:
When you want to use the batchUpdate method of Sheets API, please put each request to each element of the array of requests.
I think that the request body of UpdateCellsRequest in your script is not correct.
From your question of I'm trying to change a ranges colour via the Google Sheets API in PHP., when you want to change the background color of several cells with one color, I think that RepeatCellRequest might be suitable.
In this answer, I would like to propose a modified script for changing the several cells using one color. When your script is modified, it becomes as follows.
Modified script:
Before you use this, please set the sheet ID.
$requests = [
new Google_Service_Sheets_Request([
'updateSpreadsheetProperties' => [
'properties' => [
'title' => "The Title"
],
'fields' => 'title'
]
]),
new Google_Service_Sheets_Request([
'repeatCell' => [
'cell' => [
'userEnteredFormat' => [
'backgroundColor' => [
'red' => 1,
'green' => 0,
'blue' => 0
]
]
],
'range' => [
'sheetId' => $sheetId, // <--- Please set the sheet ID.
'startRowIndex' => 0,
'endRowIndex' => 3,
'startColumnIndex' => 0,
'endColumnIndex' => 2
],
'fields' => 'userEnteredFormat'
]
])
];
When above request body is used for the batchUpdate method of Sheets API, the title of Spreadsheet is changed and the background color of the cells "A1:B3" changed to the red color.
Wne you want to use UpdateCellsRequest, you can use the following request body. At the following request body, the background colors of cells "A1:B1" are changed to red and green colors, respectively. When UpdateCellsRequest is used, each cell can be updated. About the detail information of UpdateCellsRequest, please check the official document. Ref
$requests = [
new Google_Service_Sheets_Request([
'updateCells' => [
'rows' => array([
'values' => array(
['userEnteredFormat' => [
'backgroundColor' => [
'red' => 1,
'green' => 0,
'blue' => 0
]
]],
['userEnteredFormat' => [
'backgroundColor' => [
'red' => 0,
'green' => 1,
'blue' => 0
]
]]
)
]),
'range' => [
'sheetId' => $sheetId, // <--- Please set the sheet ID.
'startRowIndex' => 0,
'startColumnIndex' => 0,
],
'fields' => 'userEnteredFormat'
]
])
];
References:
UpdateCellsRequest
RepeatCellRequest

Related

Getting Empty Rows in Google Analytics Data API

Having difficulty getting empty rows from the Analytics Data API (GA4) BETA. I'm pulling simple traffic by date array but any dates that have zero traffic are not returned - I would prefer it to give me the date and zero.
I believe there should be a KEEPEMPTYROWS parameter but this doesn't seem to work - it took me so long to get the ORDERBY working that I thought I'd check here first to see if anyone else has got this working lol. I know I can fill in the missing dates using PHP code but I'd prefer to get it from the query. Here's the PHP for my JSON query:
$response = $client->runReport([
'property' => 'properties/' . $_SESSION["gaid"],
'dateRanges' => [
new DateRange([
'start_date' => $mstartdate,
'end_date' => $menddate,
]),
],
'dimensions' => [new Dimension(
[
'name' => 'date',
]
),
],
'metrics' => [new Metric(
[
'name' => 'activeUsers',
'name' => 'sessions',
]
)
],
'orderBys' => [
new OrderBy([
'dimension' => new OrderBy\DimensionOrderBy([
'dimension_name' => 'date',
'order_type' => OrderBy\DimensionOrderBy\OrderType::ALPHANUMERIC
]),
'desc' => false,
])],
'keepEmptyRows'=>"1"
]);
Thanks in advance :)

Add TYPO3 palette to all elements?

I have made a custom palette in tt_content.php and want to add it to all content elements on the appearance tab like this:
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'tt_content',
'--palette--;My Palette;my_palette',
'',
'before:sectionIndex'
);
This works for everything except Grid Elements (gridelements_pi1). How do I make the new palette show up on Grid Elements as well?
The comment from #MathiasBrodala lead me to finding the answer is in the order of extensions.
In this case I needed to add gridelements under suggests in my ext_emconf.php which ensures it will be loaded before my site package.
$EM_CONF[$_EXTKEY] = [
'title' => 'My Package',
'description' => 'TYPO3 Sitepackage',
'category' => 'templates',
'version' => '1.0.0',
'state' => 'stable',
'constraints' => [
'depends' => [
'typo3' => '8.7.0-9.5.99',
'fluid_styled_content' => '8.7.0-9.5.99'
],
'suggests' => [
'gridelements' => '9.3.0-0.0.0',
],
'conflicts' => [
],
],
'uploadfolder' => 0,
'createDirs' => '',
'clearCacheOnLoad' => 1
];

PHPOffice\Excel Styling Worksheet without a Spreadsheet Object

I'm having trouble styling cells in a worksheet object that doesn't yet belong to a spreadsheet object. Is this possible? It doesn't appear to be possible using the getStyle() method since this method calls functions in the parent spreadsheet. Maybe there is another method?
Worksheet class:
class MyWorksheet extends \PHPOffice\PHPSpreadsheet\Worksheet\Worksheet {
public function something() {
$this->setCellValue('A1', 'Something');
$this->getStyle('A1')->ApplyFromArray([
'font' => ['bold' => true]
]);
}
}
When something() is executed it results in a setActiveSheetIndex() on null exception.
Formatting cells
A cell can be formatted with font, border, fill, ... style information. For example, one can set the foreground colour of a cell to red, aligned to the right, and the border to black and thick border style.
Some examples:
$spreadsheet->getActiveSheet()->getStyle('B3:B7')->getFill()
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getStartColor()->setARGB('FFFF0000');
On the WorkSheet (your case)
$worksheet->getParent()->getDefaultStyle()->applyFromArray([
'font' => [
'name' => $pValue->getFont()->getName(),
'size' => $pValue->getFont()->getSize(),
],
]);
OR
Directly on the Spreadsheet
$styleArray = [
'font' => [
'bold' => true,
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
],
'borders' => [
'top' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
],
],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
'rotation' => 90,
'startColor' => [
'argb' => 'FFA0A0A0',
],
'endColor' => [
'argb' => 'FFFFFFFF',
],
],
];
$spreadsheet->getActiveSheet()->getStyle('A3')->applyFromArray($styleArray);
https://phpspreadsheet.readthedocs.io/en/develop/topics/recipes/

How can I send custom payload paramter with the NotificationPusher component to apple devices?

I am struggling with the usage of the NotificationPusher component and the possibility to send custom parameters within the payload to apple products.
I've tried the following, since I've found this annotation within the docs on github.
$message = new Message("Hello there", [
'message' => [
'sound' => 'default'
],
'custom' => [
'lat' => 123,
'lon' => 321,
'radius' => 32,
'date' => date('Y-m-d H:i:s'),
'action' => 'update'
]
]);
This syntax sadly didn't led to the expected result. The apple devices wouldn't receive these parameters.
I've also tried this, but this also failed.
$message = new Message("Hello there", [
'message' => [
'sound' => 'default',
'custom_lat' => 123,
'custom_lon' => 321,
'custom_radius' => 32,
'custom_date' => date('Y-m-d H:i:s'),
'custom_action' => 'update'
]
]);
What is the exact syntax so send custom parameters within the payload to apple devices with a push message?
I've dug trough the source code on github and have found out, that the 'custom' key of the array wasn't extracted by the ASPN Adapter.
But I've found a piece of code that extracted the complete 'message' array, so my guess was to add the 'custom' array within the 'message' part, what then also was the solution for my problem.
$message = new Message("Hello there", [
'message' => [
'sound' => 'default',
'custom' => [
'lat' => 123,
'lon' => 321,
'radius' => 32,
'date' => date('Y-m-d H:i:s'),
'action' => 'update'
]
]
]);
I faced the same need. Started debugging and found that KhorneHoly is right: You need to send the payload under custom key. The array format is a little bit different, though:
$message = new Message('This is the message', [
'custom'=> [
'payloadKey' => 'payloadContent'
]
]);
I'm using "sly/notification-pusher": "^2.3"
Hope it helps =)

How to insert YouTube video with the Telegram API?

I am using this code, but can't get it to work with the Telegram API. It works fine with an img tag, but not with an `iframe. Does anyone have a working example?
["tag"=>"iframe",
"attrs"=>["src"=>"*youtube link*"]
];
When I test out the created page, iframe is missing.
return [
'tag' => 'figure',
'children' => [
[
'tag' => 'iframe',
'attrs' => [
'src' => '/embed/youtube?url=SHORTURL',
]
],
[
'tag' => 'figcaption',
'children' => ['Video caption'],
]
]
];

Categories