I'm trying to use PHPExcel to produce spreadsheets with charts based on "templates" that non-coders have created and formatted, but with data for the charts injected by my code. I've created a "template" spreadsheet including a chart which is looking at the appropriate data range, so I want to read it in, set cell values (which are the data range for the chart), then write it out again.
In the output that PHPExcel produces, the injection of the data has worked, but the chart is missing. It's Excel2007 (not guessing, i'm getting it to identify) and I'm doing a setIncludeCharts(true) on both the reader and the writer.
Any ideas? (other than getting PHPExcel to create the chart. I can't really do that because I need non programmers to be able to create the templates that my code just puts data into based on them providing the cell reference where the data insert should start).
Many Thanks
Code (that works other than charts disappearing in output):
// Identify the filetype (it returns 'Excel2007')
$filetype = PHPExcel_IOFactory::identify("template.xlsx");
// Create the reader and load the file
$objReader = PHPExcel_IOFactory::createReader($filetype);
$objReader->setIncludeCharts(true);
$objPHPExcel = $objReader->load("template.xlsx");
// Inject the data
$objPHPExcel->setActiveSheetIndex(1)->setCellValue('A3', 'Category 1');
$objPHPExcel->setActiveSheetIndex(1)->setCellValue('B3', '10');
$objPHPExcel->setActiveSheetIndex(1)->setCellValue('A4', 'Category 2');
$objPHPExcel->setActiveSheetIndex(1)->setCellValue('B4', '20');
$objPHPExcel->setActiveSheetIndex(1)->setCellValue('A5', 'Category 3');
$objPHPExcel->setActiveSheetIndex(1)->setCellValue('B5', '30');
// Output headers
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . time() . '.xlsx"');
header('Cache-Control: max-age=0');
// Prepare PCLZIP
define( 'PCLZIP_TEMPORARY_DIR', '/tmp/' );
PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);
// Create writer and spit file out to browser
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $filetype);
$objWriter->setIncludeCharts(true);
$objWriter->save('php://output');
Related
i have been working on this for a while now and cant seem to wrap my head around it.
I am using wordpress build in projects to create "people" for a website.
I then have made a function that runs on project publish, where i need it to create a .vcf file to use as VCards for these peoples pages.
I have managed to make it create the vcf file and input some hardcoded data but i need it to get info from custom fields meta data i have made using Advanced Custom fields
I need the file to output something like this:
BEGIN:VCARD
VERSION:2.1
N:Nørbygaard;Tobias
FN:Tobias Nørbygaard
ADR;WORK;PREF:;;;Sunds;;7451;Danmark
ORG:Offbeat Media
TITLE:Webudvikler
TEL;WORK;VOICE:+45 27 50 74 57
TEL;CELL;VOICE:
URL;TYPE=WORK:www.offbeatmedia.dk
EMAIL;PREF;INTERNET:tobias#offbeatmedia.dk
END:VCARD
This is the code i have so far
function dothisfunction() {
// Do stuff
$ext = ".vcf";
$filename = "tobias".$ext;
$path = wp_upload_dir();
$file = fopen($path['path'].$filename,"w+");
echo fwrite($file, "This is where the data needs to be input");
fclose($file);
chmod($file,0777);
}
add_action( 'publish_project', 'dothisfunction' );
using the esc_html__ function was tiring to write text domain all the time: D so I created a function as below.
The problem is I can get the following word but it doesn't save to .pot file. If I make a direct definition of esc_html__ in the function, it works but when I use variables for dynamics, what is the reason?
// Working
function lsph_lang(){
return esc_html__( "Hello", 'lsph' );
}
lsph_lang();
// Not working.
function lsph_lang( $str ){
return esc_html__( $str, 'lsph' );
}
lsph_lang( "hello" );
Just giving you my 2cents about this translation stuff, either you are patient and keep writing the full form like this:
esc_html__( "Hello", 'lsph' )
Or you "hack it" by making that your "lsph_lang" internally generates a .php temp file which contains ALL the possible variant of your translations stuff. Doing that will ensure that tools that read the translations stuff will work as expected and you can stop writing the full form
I have multiple posts that has multiple custom fields on it. Now i need to add these fields from a excel/csv file. These files contain lots of info about this post. The problem i run into is that i don't see a way to import this content automatically. I don't seem to be able to use wp all import for this because it create a new item for each record and thats not what i want. All records need to be added as a custom field/repeater field.
If anyone has an idea on how to do this it would be much appreciated.
I tried to import this data using WP all import without succes.
function add_some_fields($file_url) {
$data= array_map('str_getcsv', file($file_url));
foreach ($data as $item) {
$post_id = $item[0]; // I hope you have post_id in that CSV file somewhere
$field_name = $item[1] ; // ACF field name IF YOU HAVE IT IN CSV if not set manually, or make an array with names
$col1 = $item[2]; //Value
add_row( $field_name, $col1, $post_id ); //add row (https://www.advancedcustomfields.com/resources/add_row/)
}
}
Call from where you want and how you want, usually I use condition in footer checking if custom GET parameter passed and if Yes then run function.
Guys I'm writting a wordpress site to run a knowldge base for our Service desk. As one person will be updating it i needed to have a field for who wrote the kb artical. I'm tring to add a custom field into my wordpress theme to display writtenby using Advance custom Fields. Now I'm using echo Knowledge Base plugin for knowldge base.
I've got as far add ing code below will display the text below the last up date value that plugin creates. However i cannot get it to put value from the custom field on the page after this. The plugin creates the page using php below the ive added the two lines as below.
$wb = get_post_meta($post->ID, 'Writtenby', true);
echo ' Last Update Writtenby:'.$wb.' ';
// LAST UPDATED ON
public static function last_updated_on( $args ) {
echo '' . esc_html( $args['config']['last_udpated_on_text'] ) . ' ' . EPKB_Utilities::get_formatted_datetime_string( $args['article']->post_modified, 'F d, Y' ).'';
$wb = get_post_meta($post->ID, 'Writtenby', true);
echo ' Last Update Written by:'.$wb.' ';
}
Advanced Custom Fields plugin use a little bit different system to store postmeta. Try to use get_field() instead get_post_meta()
If you have the ID
$customField = get_post_meta($my_id, "_mcf_customField", true);
However if you want to get the ID from the object:
$customField = get_post_meta($post_id->ID, "_mcf_customField", true);
After much more work looks like at the point the page was being created it had no reference to partical page not even a current one. They where writting to an array all artical numbers and info.
So by telling get_field which artical to get the writtenby field from it now displayed data and not blank
$wb= get_field('Writtenby', $args['article']->ID);
I am building a wordpress site and I have many files I need to upload and these files need the content to be searchable for users. Right now they are in pdf format. (format does not matter as long as these files can not be altered)
example: If the user types in a keyword or phrase the file content will be searched and return the file to the user that the keyword matched.
I searched for solutions and came up empty, maybe someone can point me in the right direction.
Its preety simple, you just have to install pdftotext (here is a good tutorial) and then you can search for any text. So if you a have a list of all your documents in an array you can loop and search for a certain string.
I have not tested this code, but i guess it should work fine.
<?php
$files = array('relative-link-to-file1', 'relative-link-to-file2', 'file3');
$text_to_search = 'test';
$found = array();
foreach($files as $file){
$content = shell_exec('/usr/local/bin/pdftotext '.$file.' -');
$found[]['position'] = strpos($content, $text_to_search );
$found[]['file'] = $file;
}
// To echo all the found instances.
foreach($found as $file){
echo 'The text has been found at position '.$file['position'].' within the file '.$file['file'];
}
?>