This question already has answers here:
PHPExcel how to get column index from cell
(3 answers)
Closed 2 years ago.
I am using https://phpspreadsheet.readthedocs.io/en/latest/ phpspreadsheet to import files to database.I need to get the total number of columns in the uploaded excel before importing it to database.
I found getHighestDataColumn() which returns the highest coulmn as alphabets.
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($fileName);
$worksheet = $spreadsheet->setActiveSheetIndex(0);
$high=$spreadsheet->setActiveSheetIndex(0)->getHighestColumn();
dump($high);
die;
which gives output as I
Is there any way to get it as numbers ..??
If you only have A-Z you can convert the chars to ascii and get their int value using ord.
// Get ascii offset for alphabet
$start = ord("A");
$current = "I";
// Calculate char position from offset A
echo ord($current) - $start;
Found the solution as
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($fileName);
$worksheet = $spreadsheet->setActiveSheetIndex(0);
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
dump($highestColumnIndex);
die;
This will output the index of highestColumn ..
Related
This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 2 years ago.
I am getting issue when increasing number.
For example:
$r = 000123 //Integer
Now i want
$r = 000124
I have try with multiple way.. but i didn't get the result. because of starting with 000.
You can do it this way
$num = "00000123";
// get number without zero prefix
preg_match('/(?!0+)\d+/', $num, $match);
// increase by 1
$match[0]++;
// add zero prefix
$newNum = sprintf('%08d', $match[0]);
var_dump($newNum);
What if you try:
$r = 000123
$r++
This question already has answers here:
MySQL custom primary key generator
(3 answers)
Closed 3 years ago.
I have a pattern of integer (0000012019). where 2019 is current year and 000001 need to increase with every new row inserted to db like 000002, 000003, 000004, 000005 and so on.
how can i achieve this?
First of all it's a string you have.
You can split the string with substr() and do the math on the first part and then convert back to string with str_pad by padding with 0's from left.
$str = '0000012019';
$first = substr($str, 0, 6);
$year = substr($str, -4);
$first = str_pad($first+1, 6, 0, STR_PAD_LEFT);
$new = $first . $year;
echo $new; // 0000022019
This question already has answers here:
PHP How do I round down to two decimal places? [duplicate]
(16 answers)
Closed 4 years ago.
Is there any way to do a regex that cuts off a number at a certain point without rounding (simply drops the digits?) say after 4 digits.... It will not be handling negative numbers, EVER. I could have number inputs such as 0.03123 or 1.31, or 10000.98, etc .... What I have written so far as my solution is rounding and not what I'm seeking....
$number = 10000.51999999;
$precision = 4;
echo "<br>";
// grab number before decimal by rounding down the whole number down...
$numberBeforeDecimal = floor($number);
echo "<br>";
// grab the decimal and set the correct precision needed
$n = $number;
intval($n); // 12
$theDecimalPart = explode('.', number_format($n, ($precision)))[1]; // 3430
echo $theDecimalPart; // this is outputting 5200
$theNewValue = $numberBeforeDecimal.".".$theDecimalPart;
explode() the number to get integer and decimal part separated out in an array
Use substr() function to get relevant precision from the decimal part.
Finally, concatenate them back.
Try the following (Rextester DEMO):
$number = 10000.51999999;
$precision = 4;
// separate out the integer and decimal part
$number_str_arr = explode('.', $number);
// concatenate them back
$theNewValue = $number_str_arr[0] . '.' . substr($number_str_arr[1], 0, $precision);
echo $theNewValue; // displays 10000.5199
This question already has answers here:
a method of selecting random characters from given string
(15 answers)
Closed 6 years ago.
I just start to learn PHP, but I can't to make a easy problem. Can you help me and after say why my problem doesn't work? Here is the problem:
Create a new variable $name and store your name in it.
Then print a random character from your name. Use your knowledge of strlen(string), rand(min, max), and substr(string, start, length) to do this.
HINT: Remember that substr() treats characters in a string as a zero-indexed array (first letter is at position zero). This means that the last character in the string will be at position length - 1.
I try but without result.
<html>
<p>
<?php
$name = "George";
$fl = substr($name, 0, strlen($name));
$sl = substr($name, 0, 1);
print rand($fl,$sl);
?>
</p>
</html>
I think problem is at rand...I can't randomise items with id or something?I can print the $sl or $fl ..
It can be done like this:
<?php
$name = "George";
$nameLength = strlen($name);
$randomNumber = rand(0, $nameLength - 1);
$randomLetter = substr($name, $randomNumber, 1);
echo $randomLetter;
?>
Source: https://stackoverflow.com/a/23915981/5798798
I'm loading an Excel file that has cells with time data, e.g. 08:00:00. But when I try to read those cells with getValue(), it returns some floating point numbers instead of the actual time (in case of 08:00:00, it returns 0.3333333). Here's my code:
$objPHPExcel = PHPExcel_IOFactory::load($filename);
$objWorksheet = $objPHPExcel->getActiveSheet();
echo $objWorksheet->getCellByColumnAndRow(3, 5)->getValue();
How do I bypass this weird conversion?
PHPExcel 1.7.6 and Excel 2003 Worksheet (.xls)
You need to apply cell format for this:
$cell = $objWorksheet->getCellByColumnAndRow(3, 5);
$cell_value = PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'hh:mm:ss');
echo $cell_value;