• Resolved violetasites

    (@violetasites)


    Hello, I am looking the way that the excel have the name of one of my fields.s.
    Example: I ask to my clients for the order code in the form, I need that code to be the name of the excel.
    I have reviewed all the comments and I cannot find nothing.
    Please, can you help me?
    Thank you.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Doeke Norg

    (@doekenorg)

    Hi @violetasites
    So this is for the single entry attached to the notification?

    If so then u can use something like this. I would add the class to it’s own file, and then include it at the top of your functions.php.
    Then you can use the class like this:

    // replace the name of the excel of form 1, with the value of field 1.3
    new ReplaceExcelEntryFilename(1, '1.3');

    Hope this helps you out. This is the class:

    <?php
    /**
     * Class that renames the excel file for a form to the value of a field.
     */
    class ReplaceExcelEntryFilename
    {
        private $filename;
    
        private $field_id;
    
        private $form_id;
    
        public function __construct($form_id, $field_id)
        {
            add_filter('gform_notification', [$this, 'retrieveFilename'], 0, 3);
            add_filter(sprintf('gfexcel_renderer_filename_%d', $form_id), [$this, 'setFilename']);
    
            $this->field_id = $field_id;
            $this->form_id = (int) $form_id;
        }
    
        public function retrieveFilename($notification, $form, $entry)
        {
            if ((int) $form['id'] === $this->form_id) {
                $this->filename = $entry[$this->field_id];
            }
    
            return $notification;
        }
    
        public function setFileName($filename)
        {
            if ($this->filename) {
                $filename = $this->filename;
            }
    
            return $filename;
        }
    }
    • This reply was modified 3 years, 9 months ago by Doeke Norg. Reason: Added code
    Thread Starter violetasites

    (@violetasites)

    Hy, thanks.
    It doesn’t work for me. I don’t know if I’m doing it wrong. I need to rename the Excel file whith the name of the value of one of the fields to fill in.

    Where i need to put these?
    replace the name of the excel of form 1, with the value of field 1.3
    new ReplaceExcelEntryFilename(1, ‘1.3’);

    If the id of the value of my form is 21, I need to put thes:

    // replace the name of the excel of form 1, with the value of field 1.3
    new ReplaceExcelEntryFilename(1, ’21’);

    Thank you very much.

    Plugin Author Doeke Norg

    (@doekenorg)

    You can just append that to the end of your functions.php.

    Can you confirm for me you need this for the single entry attachment? That is the only place this will work.

    So you should:

    1. Copy the class to a ReplaceExcelEntryFilename.php in the theme folder.
    2. require_once 'ReplaceExcelEntryFilename.php'; at the top of functions.php (after <?php)
    3. Add new ReplaceExcelEntryFilename(1, ’21’); to the bottom of your functions.php

    That will then work for the form with the id of 1 and it will take the value provided in the entry of field 21. You need to change those values according to your form and field id.

    Thread Starter violetasites

    (@violetasites)

    Hello,

    Yes, I need it for the single entry attachment:

    Ok I have created a new file called ReplaceExcelEntryFilename.php and uploaded it to my theme folder.

    I have pasted this at the beginning of my functions.php
    require_once ‘ReplaceExcelEntryFilename.php’;

    I have pasted this at the end of my functions.php

    new ReplaceExcelEntryFilename (3, ’21’);

    3 is the ID number of my form and 21 the field.

    Still not working. The name of the excel is still gfexcel-3-nameoftheform-20210215.

    Thanks.

    Plugin Author Doeke Norg

    (@doekenorg)

    Strange,

    Can you try just 21, instead of in quotes? So new ReplaceExcelEntryFilename (3, 21);

    If that doesn’t work, can you send me a json export of your form to [email protected]?

    I’ll take a look.

    Thread Starter violetasites

    (@violetasites)

    Ok, i have send to you.
    Thanks

    Plugin Author Doeke Norg

    (@doekenorg)

    Fixed behind the scenes. For everyone else reading this:

    This solution is correct.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Excel with the name of one of my fields’ is closed to new replies.