PHPExcel Spreadsheet cell contains ### bydefault - phpexcel

Enter image description here
I've added a calculation in that cell (e:g D6 = (C6/B6)) and as I need that value in percentage, So I used the following:
$objPHPExcel->getActiveSheet()->getStyle('D6')->getNumberFormat()->setFormatCode('#.##');
$objPHPExcel->getActiveSheet()->getStyle('D6')->getNumberFormat()->applyFromArray( array('code' => PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00) );
On clicking a button, I'm downloading the excel sheet and by default I'm getting the '###' symbols.
On Expanding that cell I've my values.
I have also given setAutoSize(true).
Any help?

Related

All worksheets in spreadsheet appear to be a reference to sheet 0

I am writing a price import script which reads from an Excel spreadsheet.
The spreadsheet is generated using Office 365 Excel however I am using LibreOffice Calc on Ubuntu 18.04 to view it during development - no issues here.
I'm using phpoffice/phpspreadsheet at version 1.10.1:
"name": "phpoffice/phpspreadsheet",
"version": "1.10.1",
"source": {
"type": "git",
"url": "https://github.com/PHPOffice/PhpSpreadsheet.git",
"reference": "1648dc9ebef6ebe0c5a172e16cf66732918416e0"
},
I am trying to convert the data of each worksheet within the spreadsheet to an array.
There are 3 worksheets, each representing 'Zones' - Zone 1, Zone 2 and Zone 3.
I appear to be getting the same data for Zone 2 and Zone 3 as Zone 1 - the worksheet title is correctly returned however the data is not changing between worksheets.
/**
* #param Spreadsheet $spreadsheet
*
* #return array
*/
private function parseZones(Spreadsheet $spreadsheet): array
{
$zones = [];
foreach ([0, 1, 2] as $sheetIndex) {
$sheet = $spreadsheet->getSheet($sheetIndex);
// this is correctly reporting 'Zone 1', 'Zone 2' and 'Zone 3' - sheet title is accurate
$sheetName = $sheet->getTitle();
// sheet 0 is accurate
$sheetData = $sheet->toArray();
// on sheet index 1 and 2 - $sheetData is identical to that of sheet index 0
// the XLSX file in OpenOffice / Excel has distinctly different row data - 50% less rows in both cases
// feels like a memory cache issue / some mis-referencing?
}
// retrieving rows using this approach yields the same result:
foreach ($spreadsheet->getAllSheets() as $sheet) {
// this is correctly reporting 'Zone 1', 'Zone 2' and 'Zone 3' - sheet title is accurate
$sheetName = $sheet->getTitle();
// on sheet index 1 and 2 - $sheetData is identical to that of sheet index 0
$sheetData = $sheet->toArray();
}
return $zones;
}
Any ideas?
Thanks
I'm a numpty - completely failed to see / check the row filtering in the spreadsheet.
It's returning the correct data.
None issue, sorry!
I've since started to investigate how to read a worksheet whilst obeying the filters embedded in the spreadsheet, and it appears Worksheet::toArray() does not automatically take filters in to account - nor does iterating columns and rows manually, see:
https://phpspreadsheet.readthedocs.io/en/latest/topics/autofilters/
You must manually test a row's visibility settings, as per the docs.
Hope this helps!
Try just change current active sheet before reads.
$spreadsheet->setActiveSheetIndex($sheetIndex);
$sheet = $spreadsheet->getActiveSheet();
$dataArray = $sheet
->rangeToArray(
'A4:O07', // The worksheet range that we want to retrieve
NULL, // Value that should be returned for empty cells
TRUE, // Should formulas be calculated (the equivalent of getCalculatedValue() for each cell)
TRUE, // Should values be formatted (the equivalent of getFormattedValue() for each cell)
TRUE // Should the array be indexed by cell row and cell column
);
PhpSpreadsheet

Fillable textboxes in PDF are not unique - all are duplicates of each other in the pdf, how to fix?

I am creating a report to send out to people and have them respond in the pdf with input in Adobe to save and send back to me.
I have managed to make text fields with hyperreff; however, the output pdf's textboxes duplicate eachother - so if I put "def" in one textbox - all the other textboxes in the document also put "def".
I am using:
\begin{Form} \TextField[width = 10cm, height = 3cm, multiline=true]{ } \end{Form}
which is used later in the document for user input:
\begin{Form} \TextField[width = 10cm, height = 3cm, multiline=true]{ } \end{Form}
You need to add a unique name to each field otherwise they all get the same name and then, in Acrobat, the same value when you edit one of them. See the name property below.
\begin{Form} \TextField[name = foo, width = 10cm, height = 3cm, multiline=true]{ } \end{Form}

Python-docx - Center a cell content in an existing table after adding values

I have a .docx template with an empty table, where I am adding values:
def manipulate_table():
table = doc.tables[0]
table.cell(0, 0).text = 'A'
table.cell(0, 1).text = 'B'
table.cell(0, 2).text = 'C'
table.cell(0, 3).text = 'D'
After adding these values, the the table attribute "Centered" is gone, which is standard behaviour.
How can I loop through my table and center all values again? I've already Googled, but found nothing helpful. E.g.: does not work:
for cell in ....????:
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
tcVAlign = OxmlElement('w:vAlign')
tcVAlign.set(qn('w:val'), "center")
tcPr.append(tcVAlign)
I appreciate all your help.
The .text property on a cell completely replaces the text in the cell, including the paragraph(s) that were there before.
The "centered" attribute is on each paragraph, not on the cell. So you need to do something like:
from docx.enum.text import WD_ALIGN_PARAGRAPH
cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
to each of the "new" paragraphs (assigning to .text will leave you with exactly one in each cell).

phpexcel format value on import

I'm working on laravel project that takes in excel file to grab certain columns from it
$sheetData = PHPExcel_IOFactory::load($file)->getActiveSheet();
$hi_row = $sheetData->getHighestRow();
$hi_col = $sheetData->getHighestColumn();
$sheetData = $sheetData->toArray(null,false,true, true);
there are some files that have different formatting on cells where in my toArray() function if i do null, false, true, true) dates comes in fine but on some excel files currency value comes up with ? in it, if i set third true condition to false, than currency comes out fine but on some excel files data gets turn into random integer. so is there any way to set all cells to general on import? or if i choose to leave it at toArray(null, false,true, true) can i set the currency column to general or something so i won't get those ?s

PHPExcel get cell currency format other than $ and EUR

PHPExcel uses $ and EUR only:
const FORMAT_CURRENCY_USD_SIMPLE = '"$"#,##0.00_-';
const FORMAT_CURRENCY_USD = '$#,##0_-';
const FORMAT_CURRENCY_EUR_SIMPLE = '[$EUR ]#,##0.00_-';
What I need is to get other currencies (I use "zł" for polish zloty). All currencies excep $ and EUR are marked as 'General'.
Follow-up question to SWilk's answer: How do I use custom format. I put new constants in NumberFormat.php:
const FORMAT_CURRENCY_PLN_1 = '_-* #,##0.00\ [$zł-415]_-';
const FORMAT_CURRENCY_PLN_2 = '\-* #,##0.00\ [$zł-415]_-';
const FORMAT_CURRENCY_PLN_3 = '_-* "-&quot';
const FORMAT_CURRENCY_PLN_4 = '??\ [$zł-415]_-;_-#_-';
is it ok? What else I have to do to read formats using
$objPHPExcel->getCellXfByIndex($cell->getXfIndex())->getNumberFormat()->getFormatCode();
Default constants are defined for Dollars and Euros only, but PHPExcel doesn't limit you to those constant constant values only. They're simply strings; and you can set any valid MS Excel format code as the numberFormat mask simply by setting it to the appropriate string value. It generally isn't a good idea to add your own new constants to numberFormat.php though, as you'll need to remember to add them whenever you upgrade to a new version of PHPExcel... it's better to define additional constants in your own code.
You can apply a currency format to cells using:
$objPHPExcel->getActiveSheet()
->getStyle('E4:E13')
->getNumberFormat()
->setFormatCode(
'_-* #,##0.00\ [$zł-415]_-'
);
Or, if you define a new constant of your own using
define('FORMAT_CURRENCY_PLN_1', '_-* #,##0.00\ [$zł-415]_-');
then you can apply it to your cells using
$objPHPExcel->getActiveSheet()
->getStyle('E4:E13')
->getNumberFormat()
->setFormatCode(
FORMAT_CURRENCY_PLN_1
);
I have saved a simple excel file with one cell formatted as currency cell denominated in PLN. Unzipped the .xlsx and checked the format.
It is:
_-* #,##0.00\ [$zł-415]_-;\-* #,##0.00\ [$zł-415]_-;_-* "-"??\ [$zł-415]_-;_-#_-
Try using this format or writing your own based on this one.
for UK Pound (Sterling) phpExcel
$numformat= '£#,##0_-';
$activeSheet->getStyle($cell)->getNumberFormat()->setFormatCode($numformat);
$activeSheet->setCellValue($cell,$val4);
I found out the correct value by unzipping an xlsx with a cell formatted as currency £ (or a cell with a £ entered into to it: seemed to be the same formatting)

Resources