Skip to content

Commit

Permalink
fixbug
Browse files Browse the repository at this point in the history
  • Loading branch information
ogenes committed Jan 19, 2024
1 parent e41f346 commit 58c4045
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 13 additions & 1 deletion example/FormulaExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ public function run(): string
['goodsName' => '半裙', 'price' => 1590, 'actualStock' => 1]
];

$data['sheet2'] = [
['goodsName' => '半裙', 'cat' => 1, 'price' => 1490, 'actualStock' => 2],
['goodsName' => '半裙', 'cat' => 2, 'price' => 1590, 'actualStock' => 1]
];

$config['sheet1'] = [
['bindKey' => 'goodsName', 'columnName' => '商品名称'],
['bindKey' => '={price}*{actualStock}', 'columnName' => '库存额'], //会自动转化为公式,所在行的 price * actualStock
['bindKey' => 'price', 'columnName' => '售价'],
['bindKey' => 'actualStock', 'columnName' => '实际库存'],
];
$config['sheet2'] = [
['bindKey' => 'goodsName', 'columnName' => '商品名称'],
['bindKey' => 'cat', 'columnName' => '分类'],
['bindKey' => '={price}*{actualStock}', 'columnName' => '库存额'], //会自动转化为公式,所在行的 price * actualStock
['bindKey' => 'price', 'columnName' => '售价'],
['bindKey' => 'actualStock', 'columnName' => '实际库存'],
];
$client = ExportClient::getInstance();
$client->setStyleWidth(50);
$client->setStyleUnit('');
Expand All @@ -41,7 +53,7 @@ public function run(): string


try {
$filepath = (new CellExample())->run();
$filepath = (new FormulaExample())->run();
print_r($filepath);
} catch (\Exception $e) {
print_r($e->getMessage());
Expand Down
8 changes: 4 additions & 4 deletions src/ExportClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ protected function formatHeader(Spreadsheet $excel): array
->setAutoSize(true)
->setWidth($this->width, $this->unit);
}
$this->columnIndexMap[$columnItem['bindKey']] = $columnIndex;
$this->columnIndexMap[$sheetName][$columnItem['bindKey']] = $columnIndex;
$columnIndex++;
}
$excel->createSheet();
Expand All @@ -212,7 +212,7 @@ protected function formatContent(Spreadsheet $excel, array $sheetIndexMap)
$config = $this->getConfig();
$data = $this->getData();
$sheetIndex = 0;
foreach ($data as $sheetData) {
foreach ($data as $sheetName => $sheetData) {
$excel->setActiveSheetIndex($sheetIndex);
$sheet = $excel->getActiveSheet();
$this->freezeHeader && $sheet->freezePane('A2');
Expand Down Expand Up @@ -241,10 +241,10 @@ protected function formatContent(Spreadsheet $excel, array $sheetIndexMap)
$text = str_replace(
array_map(static function ($key) {
return "{{$key}}";
}, array_keys($this->columnIndexMap)),
}, array_keys($this->columnIndexMap[$sheetName])),
array_map(static function ($val) use ($rowIndex) {
return "{$val}{$rowIndex}";
}, $this->columnIndexMap),
}, $this->columnIndexMap[$sheetName]),
$item['bindKey']
);
} else {
Expand Down

0 comments on commit 58c4045

Please sign in to comment.