7-6-2
設定最適欄位寬度
您沒有觀看影片的權限
請先登入,登入後,確認您的權限後,即可觀看影片。
- 先在標題列,做字數判斷,並將每欄的字數寫入 session 方便在內容部份繼續判斷
foreach ($head as $column => $value) {
$objActSheet->setCellValueByColumnAndRow($column, $row, $value); //直欄從0開始,橫列從1開始
$objActSheet->getStyleByColumnAndRow($column, $row)->applyFromArray($head_style);
$len = strlen($value);
if (!isset($_SESSION['length'][$column])) {
$_SESSION['length'][$column] = $len;
$objActSheet->getColumnDimensionByColumn($column)->setWidth($len);
}
}
- 接著在內容部份也判斷之,若是字數比原有紀錄多,那就重設session值,並重新套用寬度
// 抓出內容部份
if ($type == 'signup') {
$signup = Tad_signup_data::get_all($action['id']);
foreach ($signup as $signup_data) {
/*--略--*/
$row++;
foreach ($iteam as $column => $value) {
$objActSheet->setCellValueByColumnAndRow($column, $row, $value); //直欄從0開始,橫列從1開始
$objActSheet->getStyleByColumnAndRow($column, $row)->applyFromArray($content_style);
$len = strlen($value);
if (!isset($_SESSION['length'][$column]) || $len > $_SESSION['length'][$column]) {
$_SESSION['length'][$column] = $len;
$objActSheet->getColumnDimensionByColumn($column)->setWidth($len);
}
}
}
}
unset($_SESSION['length']);
- 最後記得清掉 session的 length 紀錄,以免影響到別的檔案(session是全域跨檔案的)影片中未示範,記得加上
unset($_SESSION['length']);
link to https://github.com/tadlearn/tad_signup/commit/b145edc8d4330aa703ab92174649f5a9113b7e65 \