如果 PHPSpreadSheet 中为空,则用文本填充单元格

Fill cell with text if empty in PHPSpreadSheet

我想为一个空单元格制作一个占位符之类的东西,所以我决定使用 conditional formatting,这里的例子说的是 OPERATOR_LESSTHAN,但没有太多关于其他条件的内容。我想用彩色文本填充单元格,类似于 'enter text here'。如果用户输入文本,单元格必须将其颜色更改为默认颜色。我尝试了以下方法,但它不起作用,单元格保持为空:

<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();

$conditional1 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
$conditional1->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS);
$conditional1->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_EQUAL);
$conditional1->addCondition('');
$conditional1->setText("Enter text here");
$conditional1->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);

$conditionalStyles = $spreadsheet->getActiveSheet()->getStyle('C3')->getConditionalStyles();
$conditionalStyles[] = $conditional1;
$spreadsheet->getActiveSheet()->getStyle('C3')->setConditionalStyles($conditionalStyles);

$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');

UPD:以下代码使单元格变灰,我可以通过 $spreadsheet->getActiveSheet()->getCell("C3")->setValue("text"); 设置单元格的内容,但如果用户向单元格输入内容,它不会改变颜色。

<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();

$conditional1 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
$conditional1->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_NOTCONTAINSTEXT);
$conditional1->getStyle()->getFont()->getColor()->setARGB('969696');

$conditionalStyles = $spreadsheet->getActiveSheet()->getStyle('C3')->getConditionalStyles();
$conditionalStyles[] = $conditional1;
$spreadsheet->getActiveSheet()->getStyle('C3')->setConditionalStyles($conditionalStyles);

$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');

我终于知道怎么做了。这是代码:

<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();

$cellToUse = 'C3';

$conditional1 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
$conditional1->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_CONTAINSTEXT);
$conditional1->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_CONTAINSTEXT);
$conditional1->setText('Add text here...');
$conditional1->getStyle()->getFont()->getColor()->setARGB('969696');

if ($spreadsheet->getActiveSheet()->getCell($cellToUse)->getValue() === null) {
    $spreadsheet->getActiveSheet()->getCell($cellToUse)->setValue('Add text here...');
}

$conditionalStyles = $spreadsheet->getActiveSheet()->getStyle($cellToUse)->getConditionalStyles();
$conditionalStyles[] = $conditional1;
$spreadsheet->getActiveSheet()->getStyle($cellToUse)->setConditionalStyles($conditionalStyles);


$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');

如果包含占位符文本,此代码将 C3 文本颜色设置为灰色,否则颜色保持默认