Merge pull request #16345 from FHenry/13_fix_column_excelfiles

FIX : Import Excel Failed and sample import file excel missing 1st column
This commit is contained in:
Laurent Destailleur 2021-02-19 17:56:58 +01:00 committed by GitHub
commit db34481cf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 17 deletions

View File

@ -173,7 +173,7 @@ class ImportXlsx extends ModeleImports
$this->workbook->getActiveSheet()->getStyle('1')->getFont()->setBold(true); $this->workbook->getActiveSheet()->getStyle('1')->getFont()->setBold(true);
$this->workbook->getActiveSheet()->getStyle('1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT); $this->workbook->getActiveSheet()->getStyle('1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);
$col = 0; $col = 1;
foreach ($headerlinefields as $field) { foreach ($headerlinefields as $field) {
$this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, 1, $outputlangs->transnoentities($field)); $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, 1, $outputlangs->transnoentities($field));
// set autowidth // set autowidth
@ -195,7 +195,7 @@ class ImportXlsx extends ModeleImports
public function write_record_example($outputlangs, $contentlinevalues) public function write_record_example($outputlangs, $contentlinevalues)
{ {
// phpcs:enable // phpcs:enable
$col = 0; $col = 1;
$row = 2; $row = 2;
foreach ($contentlinevalues as $cell) { foreach ($contentlinevalues as $cell) {
$this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, $row, $cell); $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, $row, $cell);
@ -289,7 +289,7 @@ class ImportXlsx extends ModeleImports
$xlsx = new Xlsx(); $xlsx = new Xlsx();
$info = $xlsx->listWorksheetinfo($this->file); $info = $xlsx->listWorksheetinfo($this->file);
$countcolumns = $info[0]['totalColumns']; $countcolumns = $info[0]['totalColumns'];
for ($col = 0; $col < $countcolumns; $col++) { for ($col = 1; $col <= $countcolumns; $col++) {
$this->headers[$col] = $this->workbook->getActiveSheet()->getCellByColumnAndRow($col, 1)->getValue(); $this->headers[$col] = $this->workbook->getActiveSheet()->getCellByColumnAndRow($col, 1)->getValue();
} }
return 0; return 0;
@ -314,7 +314,7 @@ class ImportXlsx extends ModeleImports
$xlsx = new Xlsx(); $xlsx = new Xlsx();
$info = $xlsx->listWorksheetinfo($this->file); $info = $xlsx->listWorksheetinfo($this->file);
$countcolumns = $info[0]['totalColumns']; $countcolumns = $info[0]['totalColumns'];
for ($col = 0; $col < $countcolumns; $col++) { for ($col = 1; $col <= $countcolumns; $col++) {
$val = $this->workbook->getActiveSheet()->getCellByColumnAndRow($col, $this->record)->getValue(); $val = $this->workbook->getActiveSheet()->getCellByColumnAndRow($col, $this->record)->getValue();
$array[$col]['val'] = $val; $array[$col]['val'] = $val;
$array[$col]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we consider it null $array[$col]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we consider it null
@ -372,7 +372,7 @@ class ImportXlsx extends ModeleImports
//var_dump($sort_array_match_file_to_database); //var_dump($sort_array_match_file_to_database);
if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[0]['val']))) { if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[1]['val']))) {
//print 'W'; //print 'W';
$this->warnings[$warning]['lib'] = $langs->trans('EmptyLine'); $this->warnings[$warning]['lib'] = $langs->trans('EmptyLine');
$this->warnings[$warning]['type'] = 'EMPTY'; $this->warnings[$warning]['type'] = 'EMPTY';
@ -420,7 +420,7 @@ class ImportXlsx extends ModeleImports
if ($key <= $maxfields) { if ($key <= $maxfields) {
// Set $newval with value to insert and set $listvalues with sql request part for insert // Set $newval with value to insert and set $listvalues with sql request part for insert
$newval = ''; $newval = '';
if ($arrayrecord[($key - 1)]['type'] > 0) $newval = $arrayrecord[($key - 1)]['val']; // If type of field into input file is not empty string (so defined into input file), we get value if ($arrayrecord[($key)]['type'] > 0) $newval = $arrayrecord[($key)]['val']; // If type of field into input file is not empty string (so defined into input file), we get value
// Make some tests on $newval // Make some tests on $newval
@ -572,21 +572,21 @@ class ImportXlsx extends ModeleImports
$newval = $this->thirpartyobject->code_client; $newval = $this->thirpartyobject->code_client;
//print 'code_client='.$newval; //print 'code_client='.$newval;
} }
if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null" if (empty($newval)) $arrayrecord[($key)]['type'] = -1; // If we get empty value, we will use "null"
} elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getsuppliercodeifauto') { } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getsuppliercodeifauto') {
if (strtolower($newval) == 'auto') { if (strtolower($newval) == 'auto') {
$newval = $this->thirpartyobject->get_codefournisseur(0, 1); $newval = $this->thirpartyobject->get_codefournisseur(0, 1);
$newval = $this->thirpartyobject->code_fournisseur; $newval = $this->thirpartyobject->code_fournisseur;
//print 'code_fournisseur='.$newval; //print 'code_fournisseur='.$newval;
} }
if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null" if (empty($newval)) $arrayrecord[($key)]['type'] = -1; // If we get empty value, we will use "null"
} elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getcustomeraccountancycodeifauto') { } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getcustomeraccountancycodeifauto') {
if (strtolower($newval) == 'auto') { if (strtolower($newval) == 'auto') {
$this->thirpartyobject->get_codecompta('customer'); $this->thirpartyobject->get_codecompta('customer');
$newval = $this->thirpartyobject->code_compta; $newval = $this->thirpartyobject->code_compta;
//print 'code_compta='.$newval; //print 'code_compta='.$newval;
} }
if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null" if (empty($newval)) $arrayrecord[($key)]['type'] = -1; // If we get empty value, we will use "null"
} elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getsupplieraccountancycodeifauto') { } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getsupplieraccountancycodeifauto') {
if (strtolower($newval) == 'auto') { if (strtolower($newval) == 'auto') {
$this->thirpartyobject->get_codecompta('supplier'); $this->thirpartyobject->get_codecompta('supplier');
@ -594,7 +594,7 @@ class ImportXlsx extends ModeleImports
if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null" if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
//print 'code_compta_fournisseur='.$newval; //print 'code_compta_fournisseur='.$newval;
} }
if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null" if (empty($newval)) $arrayrecord[($key)]['type'] = -1; // If we get empty value, we will use "null"
} elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getrefifauto') { } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getrefifauto') {
$defaultref = ''; $defaultref = '';
// TODO provide the $modTask (module of generation of ref) as parameter of import_insert function // TODO provide the $modTask (module of generation of ref) as parameter of import_insert function
@ -624,7 +624,7 @@ class ImportXlsx extends ModeleImports
$errorforthistable++; $errorforthistable++;
$error++; $error++;
} else { } else {
$newval = $arrayrecord[($key - 1)]['val']; //We get new value computed. $newval = $arrayrecord[($key)]['val']; //We get new value computed.
} }
} elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'numeric') { } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'numeric') {
$newval = price2num($newval); $newval = price2num($newval);
@ -695,8 +695,8 @@ class ImportXlsx extends ModeleImports
$listfields[] = $fieldname; $listfields[] = $fieldname;
// Note: arrayrecord (and 'type') is filled with ->import_read_record called by import.php page before calling import_insert // Note: arrayrecord (and 'type') is filled with ->import_read_record called by import.php page before calling import_insert
if (empty($newval) && $arrayrecord[($key - 1)]['type'] < 0) $listvalues[] = ($newval == '0' ? $newval : "null"); if (empty($newval) && $arrayrecord[($key)]['type'] < 0) $listvalues[] = ($newval == '0' ? $newval : "null");
elseif (empty($newval) && $arrayrecord[($key - 1)]['type'] == 0) $listvalues[] = "''"; elseif (empty($newval) && $arrayrecord[($key)]['type'] == 0) $listvalues[] = "''";
else $listvalues[] = "'" . $this->db->escape($newval) . "'"; else $listvalues[] = "'" . $this->db->escape($newval) . "'";
} }
$i++; $i++;

View File

@ -550,7 +550,7 @@ class modProduct extends DolibarrModules
// field order as per structure of table llx_product // field order as per structure of table llx_product
$import_sample = array( $import_sample = array(
'p.ref' => "PREF123456", 'p.ref' => "ref:PREF123456",
'p.datec' => dol_print_date(dol_now(), '%Y-%m-%d'), 'p.datec' => dol_print_date(dol_now(), '%Y-%m-%d'),
'p.label' => "Product name in default language", 'p.label' => "Product name in default language",
'p.description' => "Product description in default language", 'p.description' => "Product description in default language",
@ -690,7 +690,7 @@ class modProduct extends DolibarrModules
); );
$this->import_examplevalues_array[$r] = array( $this->import_examplevalues_array[$r] = array(
'sp.fk_product' => "PRODUCT_REF or id:123456", 'sp.fk_product' => "ref:PRODUCT_REF or id:123456",
'sp.fk_soc' => "My Supplier", 'sp.fk_soc' => "My Supplier",
'sp.ref_fourn' => "XYZ-F123456", 'sp.ref_fourn' => "XYZ-F123456",
'sp.quantity' => "5", 'sp.quantity' => "5",
@ -752,13 +752,13 @@ class modProduct extends DolibarrModules
$this->import_convertvalue_array[$r] = array( $this->import_convertvalue_array[$r] = array(
'pr.fk_product'=>array('rule'=>'fetchidfromref', 'classfile'=>'/product/class/product.class.php', 'class'=>'Product', 'method'=>'fetch', 'element'=>'Product') 'pr.fk_product'=>array('rule'=>'fetchidfromref', 'classfile'=>'/product/class/product.class.php', 'class'=>'Product', 'method'=>'fetch', 'element'=>'Product')
); );
$this->import_examplevalues_array[$r] = array('pr.fk_product'=>"PRODUCT_REF or id:123456", $this->import_examplevalues_array[$r] = array('pr.fk_product'=>"ref:PRODUCT_REF or id:123456",
'pr.price_base_type'=>"HT (for excl tax) or TTC (for inc tax)", 'pr.price_level'=>"1", 'pr.price_base_type'=>"HT (for excl tax) or TTC (for inc tax)", 'pr.price_level'=>"1",
'pr.price'=>"100", 'pr.price_ttc'=>"110", 'pr.price'=>"100", 'pr.price_ttc'=>"110",
'pr.price_min'=>"100", 'pr.price_min_ttc'=>"110", 'pr.price_min'=>"100", 'pr.price_min_ttc'=>"110",
'pr.tva_tx'=>'20', 'pr.tva_tx'=>'20',
'pr.recuperableonly'=>'0', 'pr.recuperableonly'=>'0',
'pr.date_price'=>'2013-04-10'); 'pr.date_price'=>'2020-12-31');
} }
if (!empty($conf->global->MAIN_MULTILANGS)) if (!empty($conf->global->MAIN_MULTILANGS))