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
Related
I want to extract the table from this website https://www.rankingthebrands.com/The-Brand-Rankings.aspx?rankingID=37&year=214
Checking the source of that website, I noticed that somehow the table tag is missing. I assume that this table is a summary of multiple div classes. Is there any easy approach to convert this table to excel/csv? I badly have coding skills/experience...
Appreciate any help
There are a few way to do that. One of which (in python) is (pretty self-explanatory, I believe):
import lxml.html as lh
import csv
import requests
url = 'https://www.rankingthebrands.com/The-Brand-Rankings.aspx?rankingID=37&year=214'
req = requests.get(url)
doc = lh.fromstring(req.text)
headers = ['Position', 'Name', 'Brand Value', 'Last']
with open('brands.csv', 'a', newline='') as fp:
#note the 'a' in there - for 'append`
file = csv.writer(fp)
file.writerow(headers)
#with the headers out of the way, the heavier xpath lifting begins:
for row in doc.xpath('//div[#class="top100row"]'):
pos = row.xpath('./div[#class="pos"]//text()')[0]
name = row.xpath('.//div[#class="name"]//text()')[0]
brand_val = row.xpath('.//div[#class="weighted"]//text()')[0]
last = row.xpath('.//div[#class="lastyear"]//text()')[0]
file.writerow([pos,name,brand_val,last])
The resulting file should be at least close to what you're looking for.
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
I have the following ALV report generated from the RFKSLD00 program:
I need to generate a report based on the above report like this one (as part of my work):
Any ideas how to do this? I am not asking for a solution but some steps on how to achieve this.
Each line of the original report is one line of your report, you need just to adjust the sums for local currency, i.e. multiply all values by local currency rate. The yellow totals lines shouldn't confuse you, they are generated by grid, not by report.
The only thing that is missing in original report is debit and credit of balance carryforward, I suppose in the original you have already reconciliated value. To get separate values for it you need inspecting the code.
The initial step would be to declare final structure and table based on it:
TYPES: BEGIN OF ty_report,
rec_acc TYPE skont,
vendor TYPE lifnr,
...
jan_deb TYPE wrbtr,
jan_cred TYPE wrbtr,
febr_deb TYPE wrbtr,
febr_cred TYPE wrbtr,
...
acc_bal_deb TYPE wrbtr,
acc_bal_cred TYPE wrbtr,
END OF ty_report,
tt_report TYPE TABLE OF ty_report.
DATA: lt_report TYPE tt_report.
Then you only need looping original report internal table and fill your final structure, not missing currency conversion:
select single * from tcurr
into #DATA(tcurr)
where fcurr = 'EUR'
and tcurr = 'AUD'. "<- your local currency
DATA(lv_ukurs) = tcurr-ukurs.
LOOP AT orig_table INTO DATA(orig_line).
APPEND INITIAL LINE INTO lt_report ASSIGNING FIELD-SYMBOL(<fs_rep>).
MOVE-CORRESPONDING orig_line TO <fs_rep>.
CASE orig_line-monat. "<- your period
WHEN '01'.
<fs_rep>-jan_deb = orig_line-debit.
<fs_rep>-jan_cred = orig_line-credit.
WHEN '02'.
<fs_rep>-febr_deb = orig_line-debit.
<fs_rep>-febr_cred = orig_line-credit.
...
ENDCASE.
DO 30 TIMES.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_rep> TO FIELD-SYMBOL(<field>).
CHECK sy-subrc = 0.
DESCRIBE FIELD <field> TYPE DATA(TYP).
CHECK TYP = 'P'.
CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
EXPORTING
DATE = sy-datum
FOREIGN_CURRENCY = 'EUR'
LOCAL_CURRENCY = 'AUD'
FOREIGN_AMOUNT = <field>
TYPE_OF_RATE = 'M'
IMPORTING
EXCHANGE_RATE = lv_ukurs
LOCAL_AMOUNT = <field>.
ENDDO.
ENDLOOP.
I recommend to name all components of your final structure ty_report the same as in original as much as possible. Thus you can maximally utilize MOVE-CORRESPONDING and avoid manual coding.
This is just quick shot and I may be missing some details and errors.
Using the built-in "Import Data..." functionality we can import a properly formatted text file (like CSV and/or tab-delimited) as an image. It is rather straight forward to write a script to do so. However, my scripting approach is not efficient - which requires me to loop through each raw (use the "StreamReadTextLine" function) so it takes a while to get a 512x512 image imported.
Is there a better way or an "undocumented" script function that I can tap in?
DigitalMicrograph offers an import functionality via the File/Import Data... menu entry, which will give you this dialog:
The functionality evoked by this dialog can also be accessed by script commands, with the command
BasicImage ImageImportTextData( String img_name, ScriptObject stream, Number data_type_enum, ScriptObject img_size, Boolean lines_are_rows, Boolean size_by_counting )
As with the dialog, one has to pre-specify a few things.
The data type of the image.
This is a number. You can find out which number belongs to which image data type by, f.e., creating an image outputting its data type:
image img := Realimage( "", 4, 100 )
Result("\n" + img.ImageGetDataType() )
The file stream object
This object describes where the data is stored. The F1 help-documention explains how one creates a file-stream from an existing file, but essentially you need to specify a path to the file, then open the file for reading (which gives you a handle), and then using the fileHandle to create the stream object.
string path = "C:\\test.txt"
number fRef = OpenFileForReading( path )
object fStream = NewStreamFromFileReference( fRef, 1 )
The image size object
This is a specific script object you need to allocate. It wraps image size information. In case of auto-detecting the size from the text, you don't need to specify the actual size, but you still need the object.
object imgSizeObj = Alloc("ImageData_ImageDataSize")
imgSizeObj.SetNumDimensions(2) // Not needed for counting!
imgSizeObj.SetDimensionSize(0,10) // Not used for counting
imgSizeObj.SetDimensionSize(1,10) // Not used for counting
Boolean checks
Like with the checkboxes in the UI, you spefic two conditions:
Lines are Rows
Get Size By Counting
Note, that the "counting" flag is only used if "Lines are Rows" is also true. Same as with the dialog.
The following script improrts a text file with couting:
image ImportTextByCounting( string path, number DataType )
{
number fRef = OpenFileForReading( path )
object fStream = NewStreamFromFileReference( fRef, 1 )
number bLinesAreRows = 1
number bSizeByCount = 1
bSizeByCount *= bLinesAreRows // Only valid together!
object imgSizeObj = Alloc("ImageData_ImageDataSize")
image img := ImageImportTextData( "Imag Name ", fStream, DataType, imgSizeObj, bLinesAreRows, bSizeByCount )
return img
}
string path = "C:\\test.txt"
number kREAL4_DATA = 2
image img := ImportTextByCounting( path, kREAL4_DATA )
img.ShowImage()
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 = '_-* "-"';
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)