I want to align the cell value to the middle. My output looks like this:-
My expected output should be this:
I want every column to be in the center. I tried the following code:
$styleArray = [
'font' => [
'bold' => true,
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'startColor' => [
'argb' => '0070C0',
],
'endColor' => [
'argb' => '0070C0',
],
],
];
$spreadsheet->getDefaultStyle()->getFont()->setSize(10);
I tried all the other attributes like HORIZONTAL_CENTER, RIGHT, LEFT, JUSTIFY, etc. How can I do this properly?
You're setting the wrong (and one too few) key(s) for the alignment setting. What you're attempting to achieve is the vertical and horizontal alignment of the text.
'alignment' => [
'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
PhpSpreadsheet docs
Beyond the style array way you can also do it in the method chaining way:
$spreadsheet->getActiveSheet()->getStyle($cells)->getAlignment()->setHorizontal($align)
$spreadsheet->getActiveSheet()->getStyle($cells)->getAlignment()->setVertical($align);
$cells should be a single cell ('A1') or a range of cells ('A1:E4').
$align should be a constant of the \PhpOffice\PhpSpreadsheet\Style\Alignment class (or its string value) for the desired alignment.
for horizontal alignment:
Alignment::HORIZONTAL_GENERAL or 'general'
Alignment::HORIZONTAL_LEFT or 'left'
Alignment::HORIZONTAL_RIGHT or 'right'
Alignment::HORIZONTAL_CENTER or 'center'
Alignment::HORIZONTAL_CENTER_CONTINUOUS or 'centerContinuous'
Alignment::HORIZONTAL_JUSTIFY or 'justify'
Alignment::HORIZONTAL_FILL or 'fill'
Alignment::HORIZONTAL_DISTRIBUTED or 'distributed' (Excel2007 only)
for vertical alignment:
Alignment::VERTICAL_BOTTOM or 'bottom'
Alignment::VERTICAL_TOP or 'top'
Alignment::VERTICAL_CENTER or 'center'
Alignment::VERTICAL_JUSTIFY or 'justify'
Alignment::VERTICAL_DISTRIBUTED or 'distributed' (Excel2007 only)
The source of these is the source.
Related
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
];
I created a custom content element with TYPO3 ver. 10.4.21, but I have a problem.
Problem: showing up same Fields on every edit page of content elements.
I want to use own fields only on my custom content element (for my flipbox-content element). But if I select e.g. a regular text element, then I can see my own fields on the edit page by the text element too.
I wrote the codes in TCA/Overrides/tt_content.php:
###############################################
# Front side #
###############################################
// front side header
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
'tt_content',
[
'tx_pagesaddfields_frontsideheader' => [
'exclude' => 0,
'label' => 'Front side header',
'config' => [
'type' => 'text',
'renderType' => 'input',
'items' => [
[
0 => '',
1 => ''
]
],
],
],
]
);
// front side bodytext
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
'tt_content',
[
'tx_pagesaddfields_frontsidebodytext' => [
'label' => 'Front side bodytext',
'config' => [
'type' => 'text',
'cols' => 40,
'rows' => 15,
'enableRichtext' => true,
],
],
]
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addFieldsToPalette(
'tt_content',
'general',
'tx_pagesaddfields_frontsideheader, tx_pagesaddfields_frontsidebodytext',
'after:tx_container_parent'
);
###############################################
# Back side #
###############################################
// back side header
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
'tt_content',
[
'tx_pagesaddfields_backsideheader' => [
'exclude' => 0,
'label' => 'Back side header',
'config' => [
'type' => 'text',
'renderType' => 'input',
'items' => [
[
0 => '',
1 => ''
]
],
],
],
]
);
// back side bodytext
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
'tt_content',
[
'tx_pagesaddfields_backsidebodytext' => [
'label' => 'Back side bodytext',
'config' => [
'type' => 'text',
'cols' => 40,
'rows' => 15,
'enableRichtext' => true,
],
],
]
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addFieldsToPalette(
'tt_content',
'general',
'tx_pagesaddfields_backsideheader, tx_pagesaddfields_backsidebodytext',
'after:tx_pagesaddfields_frontsidebodytext'
);
I know because I wrote "addTCAcolumns('tt_content'). But I don't know how I can rewrite them to let show up my new fields only on my custom edit page.
Is it the right page to chage it?:
https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ExtensionArchitecture/ExtendingTca/Examples/Index.html
(by exanple1) and
https://docs.typo3.org/m/typo3/reference-tca/master/en-us/ColumnsConfig/Type/User/Index.html#columns-user?
I tried to do it, but it didn't work on my site. Unfortunately, I don't have enough experiences with PHP... If you know other website or if you can explain here, please write it down here.
I hope anyone can help me. Thank you.
It is best practice to add one file into TCA/Overrides/ per custom content element. The name is up to you, TYPO3 reads all .php files in this folder. content_element_<your_ctype>.php or tt_content_<your_ctype>.php are used as name quite often.
https://github.com/benjaminkott/bootstrap_package/blob/master/Configuration/TCA/Overrides/203_content_element_card_group.php
Here you can see how it is done in the bootstrap_package for the custom content element with the CType card_group
In line 42 $GLOBALS['TCA']['tt_content']['types']['card_group'] the configuration is limited to ['(C)type']['card_group'] and the 'showitem' => ' tells TYPO3 what and how to show fields for this content element.
From line 70 on you can see how to set a new field, in line 49 you can see how it was added.
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
When I'm using rendermode selectTree for my kategorie selection, then I've got an graphical bug. The bug independent from browser (tested in chrome and firefox). I found out, that it looks like the svg icon I'm using for this type.
My tca config for the field "eltern":
'eltern' => [
'exclude' => true,
'label' => 'Eltern',
'config' => [
'type' => 'select',
'renderType' => 'selectTree',
'foreign_table' => 'tx_adressen_domain_model_adresskategorie',
'foreign_table_where' => 'ORDER BY tx_adressen_domain_model_adresskategorie.kategoriename',
'size' => 20,
'treeConfig' => [
'parentField' => 'eltern',
'appearance' => [
'expandAll' => true,
'showHeader' => true,
],
],
'maxitems' => 1,
'minitems' => 0,
],
],
Resolved the problem with using a 16x16 Pixel PNG icon instead of a svg icon
TCA:
return [
...
'iconfile' => 'EXT:adressen/Resources/Public/Icons/Adresskategorie.png',
...
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/