How to repeat the cellmerging in XLSX with openTBS? - xlsx

Let me try to describe the problem in words. I have a headline in my array which should be centered in two merged cells. When opentbs is looping throug my array, only the first headline is in a merged cell, for all upcoming headlines the cellmerging gets lost.
The Array is similar to this:
$data = [
'name' => 'district one',
'streets' => [
[
'name' => 'first street',
'description' => 'first text',
],
[
'name' => 'second street',
'description' => 'second text',
]
],
'name' => 'district two',
'streets' => [
[
'name' => 'third street',
'description' => 'third text',
],
[
'name' => 'fourth street',
'description' => 'fourth text',
]
],
]
The cell structure i want to reach is like this:
Cellstructure
and this is the code of template:
[data;block=begin;sub1=streets]
[data.name;block=tbs.row]
[data_sub1;block=begin]
[data_sub1.name] [data_sub1.description]
[data_sub1;block=end]
[data;block=end]
here is the templatecode and the result in one img:
templatecode an result
How to repeat the cellmerging with openTBS?

Related

Cakephp4 save with associations with more than one level

I'm developing with CakePHP 4 and try to save a nested entity.
The new data are looking like that:
$data = [
'device_status_id' => '3',
'name' => 'myDevice',
'modules' => [
[
'name' => 'Autodetect',
'module_state_id' => '1',
'module_class_id' => '12',
'module_type_id' => '4',
'ports' => [
[
'physical_port' => '1',
'port_unit_id' => '1',
'port_identity' => '201901',
'name' => 'Analog-1',
],
[
'physical_port' => '2',
'port_unit_id' => '1',
'port_identity' => '201902',
'name' => 'Analog-2',
],
],
],
]
];
The structure is looking like that: $device -> hasMany($modules) -> hasMany($ports)
The table associations with hasMany are also given and working:
e.g.
$this->hasMany('Ports', [
'foreignKey' => 'module_id',
'dependent' => true,
'cascadeCallbacks' => true,
]);
The same for device as well...
In my controller I'm doing the following:
$device = $this->Devices->newEmptyEntity();
$device->setDirty('modules', true);
$device->setDirty('ports', true); // right?
// adding more info to $device
if ($this->request->is('post')) {
$device = $this->Devices->patchEntity($device, $this->request->getData(), [
'validate' => false,
'associated' => [
'DeviceTypes',
'DeviceStatuses',
'Modules',
'Modules.ModuleStates',
'Modules.ModuleClasses',
'Modules.ModuleTypes',
'Modules.Ports',
]
]);
$this->Devices->save($device)
Now the save fails, as the validator is complaining about a missing port->id.
So what is wrong here? The modules are created in the DB, but not the associated ports:
[
'device' => [
'modules' => [
(int) 0 => [
'ports' => [
(int) 0 => [
'id' => [
'_required' => 'This field is required',
],
....
Many thanks for your hints how I can force the ports to be added to the DB.

Custom widget in Elementor with the same controls

I am creating a custom Elementor widget with two image controls, unfortunately, I can only get one of the two to work in the content tab. I thought if I add two sections it would work but seems not, looked at Elementor documentation at https://developers.elementor.com/docs/editor-controls/control-media/ but cant find anything there.
$this->start_controls_section(
'section_image_one',
[
'label' => esc_html__( 'Image One' , $this->domain ),
]
);
$this->add_control(
'image',
[
'label' => esc_html__( 'Choose Image', $this->domain ),
'type' => Controls_Manager::MEDIA,
'dynamic' => [
'active' => true,
],
'default' => [
'url' => Utils::get_placeholder_image_src(),
],
]
);
$this->end_controls_section();
$this->start_controls_section(
'section_image_two',
[
'label' => esc_html__( 'Image Two' , $this->domain ),
]
);
$this->add_control(
'image',
[
'label' => esc_html__( 'Choose Image', $this->domain ),
'type' => Controls_Manager::MEDIA,
'dynamic' => [
'active' => true,
],
'default' => [
'url' => Utils::get_placeholder_image_src(),
],
]
);
$this->end_controls_section();
It looks like it is a rather simple solution
.....
$this->add_control(
'image',
.....
As mentioned on https://developers.elementor.com/docs/editor-controls/regular-control/, the "image" mentioned is a control name and needs to be unique.

Filter array by property and other array

I have a planning, with multiple items.
My object is like this :
planning: [
{ id: '1', equipe: [ '1', '2' ] },
{ id: '2', equipe: [ '1' ] },
{ id: '3', equipe: [ '2', '3' ] }
...
]
I want to filter this array to hide or show planning items.
My shown array is :
checked: [ '1', '4' ]
So, the array planning could be filtered by equipe array, so it should be :
planning: [
{ id: '1', equipe: [ '1', '2' ] },
{ id: '2', equipe: [ '1' ] }
]
I've tried to filter array but I don't see how indexOf could work with array and not string.
I've also tried includes function but this doens't work:
planning.filter(item => checked.includes(item.equipe))
Thask for help !
you need iterate over each value from item.equipe against checked, you could use some to check if at least one matches checked array:
planning.filter(item => item.equipe.some(val => checked.includes(val)))
const planning = [
{ id: '1', equipe: [ '1', '2' ] },
{ id: '2', equipe: [ '1' ] },
{ id: '3', equipe: [ '2', '3'] }
]
const checked = [ '1', '4' ]
const filtered = planning.filter(item => item.equipe.some(val => checked.includes(val)))
console.log(filtered)
I would implement the logic in this manner.
//Original Data
let planning = [
{ id: '1', equipe: ['1', '2'] },
{ id: '2', equipe: ['1'] },
{ id: '3', equipe: ['2', '3'] }
]
/**
* Converting checked into Set so it will maintain all the unique values.
* I can check which elements are present in O(1) using checked.has(value)
*/
let checked = new Set(['1', '4'])
/**
* Using standard filter() function.
* Using some() to check if atleast 1 element from equipe is present in Set.
* If yes then checked.has(element) evaluates to true and some() receives atleast 1 element is valid and the element passes the filter.
*/
let filtered = planning.filter(plan => {
return plan.equipe.some(element => checked.has(element));
})
console.log(filtered)

Perl dynamic multidimensional array [duplicate]

This question already has answers here:
Push to array reference
(4 answers)
Closed 8 years ago.
In my Perl Programm, I used for testing the following static array definition:
my %data = (
56 => [
{ 'Titel' => 'Test 1',
'Subtitel' => 'Untertest 1',
'Beginn' => '00:05',
'Ende' => '00:50'
},
{ 'Titel' => 'Test 2',
'Subtitel' => 'Untertest 2',
'Beginn' => '00:50',
'Ende' => '01:40'
}
],
58 => [
{ 'Titel' => 'Test 3',
'Subtitel' => 'Untertest 3',
'Beginn' => '00:10',
'Ende' => '01:50'
}
],
51 => [
{ 'Titel' => 'Test 4',
'Subtitel' => 'Untertest 4',
'Beginn' => '00:05',
'Ende' => '00:20'
},
{ 'Titel' => 'Test 5',
'Subtitel' => 'Untertest 5',
'Beginn' => '00:20',
'Ende' => '00:40'
},
{ 'Titel' => 'Test 6',
'Subtitel' => 'Untertest 6',
'Beginn' => '00:40',
'Ende' => '01:05'
}
],
);
Now I like to change it, to get data from a database. My select returns 5 values: an id (like 56, 58 or 51 in my example) and the values for each Titel, Subtitel, Beginn and Ende.
How can I build the same array construct like in my static example?
Thanks in advance! Best regards
Daniel
Assuming you want it at the end, you need to push your hashref into the arrayref stored at $data{$id}:
push #{ $data{$id} }, {
Titel => $titel,
Subtitel => $subtitel,
Beginn => $beginn,
Ende => $ende,
};
Could be something like this. Sorry, it's been a while since I've done perl.
#!/usr/bin/perl
use Data::Dumper;
sub dataset2struc {
my $result = {};
foreach my $row (#_) {
my $id = $row->{'id'};
my $recref = $result->{$id} || ();
my #rec = #{$recref};
delete $row->{'id'};
push(\#rec, $row);
$result->{$id} = \#rec;
}
return $result;
}
my #dataset = ({"id" => 1, "a" => "b"}, {"id" => 2, "a" => "c"}, {"id" => 2, "a" => "d"});
print Dumper(dataset2struc(#dataset));

perl group results in hash using fetchrow_hashref

I want to group my results by countryid with the data shown below.
my #test = ();
my $st = qq[
SELECT id,countryID,abbrev
FROM myTable
];
my $q = $data->prepare($st);
$q->execute() or query_error($st);
while ( my $result = $q->fetchrow_hashref ) {
push #test, $result;
}
Using fetchrow_hashref I have no problem displayling the results
use Data::Dumper;
print STDERR Dumper(\#test);
returns
$VAR1 = [
{ 'id' => '1',
'countryID' => '1',
'title' => 'Title 1',
'abbrev' => 't1'
},
{ 'id' => '2',
'countryID' => '2',
'title' => 'Title 2',
'abbrev' => 't2'
},
{ 'id' => '3',
'countryID' => '3',
'title' => 'Title 3',
'abbrev' => 't3'
},
{ 'id' => '4',
'countryID' => '1',
'title' => 'Title 4',
'abbrev' => 't4'
}
];
I want to group it by countries as shown below.
$VAR1 = [
'countries' => {
'1' => [
{ 'id' => '1',
'title' => 'Title 1',
'abbrev' => 't1'
},
{ 'id' => '4',
'title' => 'Title 4',
'abbrev' => 't4'
}
],
'2' => [
{ 'id' => '2',
'title' => 'Title 2',
'abbrev' => 't2'
}
],
'3' => [
{ 'id' => '3',
'title' => 'Title 3',
'abbrev' => 't3'
}
]
}
];
How can I get this working in the while loop?
Ignoring the errors in your sample data structure, you are basically looking to convert form array of hashes to hash of hash of array of hashes. Once you have your initial data structure setup, you can do the following to create your new nested data structure:
for my $href ( #test ) {
my $id = $href->{countryID};
delete $href->{countryID};
push #{ $test2->{countries}{$id} }, $href;
}
Iterate each element of your array #test which basically is an array of hash references. Create a variable $id which will capture the countryID value from the hash. We delete it from the hash reference and then assign that hash reference to our new nested data structure which has countries as the first level key and the $id as the second level key.
We use push syntax to create our array of such references.
Note: As stated by thb in the comments, this does destroys your original data structure. If you'd like to retain the original structure, modify the code to the following:
for my $href ( #test ) {
my $copy = { %$href };
my $id = $copy->{countryID};
delete $copy->{countryID};
push #{ $test2->{countries}{$id} }, $copy;
}
You'll need to fix your syntax above a little (for example => instead of = >), but once you have done that, something like this should work nicely.
for (#$VAR1_orig) {
my %a = %$_;
my $countryID = $a{countryID};
delete $a{countryID};
push #{$VAR1->{countries}{$countryID}}, \%a;
}
(I have tried it on my computer, incidentally. It works.)
The above assumes that %$VAR1 is initially empty, then populates it according to #$VAR1_orig, after which you can do with $VAR1 whatever you like. (I assume that you know what %$ and #$ mean in Perl, but this is not a beginner's topic, as you may know. See man 1 perlref.)
Something like this, the input/output data structures might not be exactly what you have or want, you can patch that up.
use strict;
use Data::Dumper;
$a = [
{ 'id' => '1',
'countryID' => '1',
'title' => 'Title 1',
'abbrev' => 't1'
},
{ 'id' => '2',
'countryID' => '2',
'title' => 'Title 2',
'abbrev' => 't2'
},
{ 'id' => '3',
'countryID' => '3',
'title' => 'Title 3',
'abbrev' => 't3'
},
{ 'id' => '4',
'countryID' => '1',
'title' => 'Title 4',
'abbrev' => 't4'
}
];
my $b = {};
for my $item (#$a) {
if ( exists( $b->{ $item->{'countryID'} } ) ) {
push( #{ $b->{ $item->{'countryID'} } }, $item );
} else {
$b->{ $item->{'countryID'} } = [$item];
}
}
print Dumper($b);
The above prints:
$VAR1 = {
'1' => [
{ 'abbrev' => 't1',
'title' => 'Title 1',
'id' => '1',
'countryID' => '1'
},
{ 'abbrev' => 't4',
'title' => 'Title 4',
'id' => '4',
'countryID' => '1'
}
],
'3' => [
{ 'abbrev' => 't3',
'title' => 'Title 3',
'id' => '3',
'countryID' => '3'
}
],
'2' => [
{ 'abbrev' => 't2',
'title' => 'Title 2',
'id' => '2',
'countryID' => '2'
}
]
};

Resources