为了账号安全,请及时绑定邮箱和手机立即绑定

如何动态填充重力形式从 Google Sheets 数据中选择(下拉)菜单项

如何动态填充重力形式从 Google Sheets 数据中选择(下拉)菜单项

PHP
长风秋雁 2023-12-15 16:26:21
我正在尝试使用谷歌表格中的数据动态填充下拉菜单中可供选择的选项。数据位于 A 列(目前为 A2:A4,但这可能会发生变化),并将包括可用员工的姓名。因此,如果:   A1 name 2 jack 3 Bob4 John我需要这 3 个名称能够动态地在重力形式的下拉菜单中进行选择。我还需要灵活性,以便在员工空闲时间发生变化时允许有更多或更少的名字。我一直在尝试使用重力形式文档将一些东西组合在一起,并从我在 github 上找到的片段中提取一些零碎的东西。这是我到目前为止所拥有的,但它给了我一个严重错误:$location_form_id = [FORM ID HERE];add_filter( 'gform_pre_render_'.$location_form_id, 'populate_posts' );add_filter( 'gform_pre_validation_'.$location_form_id, 'populate_posts' );add_filter( 'gform_pre_submission_'.$location_form_id, 'populate_posts' );add_filter( 'gform_pre_submission_filter_'.$location_form_id, 'populate_posts' );add_filter( 'gform_admin_pre_render_'.$location_form_id, 'populate_posts' );  function populate_posts($form){            foreach($form['fields'] as &$field){                  if($field->id != [FIELD ID HERE] ) {           continue;                        // Hook into Google Spreadsheets //            $url = 'http://spreadsheets.google.com/feeds/list/[SPREADSHEET ID HERE]/od6/public/values?alt=json';            $file = file_get_contents($url);                        $json = json_decode($file);            $rows = $json->{'feed'}->{'entry'};                        $names = array();                        foreach($rows as $row) {                $name = $row->{'gsx$name'}->{'$t'};                    $names[] = $name;            }            foreach($names as $single_name){                $choices[] = array('text' => $single_name, 'value' => $single_name );            }                     $field['choices'] = $choices;                }           return $form;  }
查看完整描述

2 回答

?
白衣非少年

TA贡献1155条经验 获得超0个赞

您需要使用重力形式提供的少量filters来实现achieve。只需要四个过滤器。

  1. gform_pre_render_

  2. gform_pre_validation_

  3. gform_pre_submission_filter_

  4. gform_admin_pre_render_

您需要循环遍历表单 ID 的所有 fields XX 并检查您选择的字段是否是实际的 字段。dropdown 字段表示 select

对于push在工作表中找到的所有新内容,我们可以使用array_push方法,然后循环遍历该array获取所有存储的名称。

如果您愿意,您还可以在选择中添加占位符 field,最后仅返回 $form

在下面的代码中只需添加您自己的$form_id,选择$feild_id 和$gSheet_form_ID

将此代码添加到您的活动主题functions.php 文件中。(代码已测试且有效)

$location_form_id = '62';

add_filter( 'gform_pre_render_'.$location_form_id, 'populate_posts' );

add_filter( 'gform_pre_validation_'.$location_form_id, 'populate_posts' );

add_filter( 'gform_pre_submission_filter_'.$location_form_id, 'populate_posts' );

add_filter( 'gform_admin_pre_render_'.$location_form_id, 'populate_posts' );

function populate_posts( $form ) {


    //the select feild id you want the names to load

    $field_ID = '2';

    //your g sheet ID

    $gSheet_form_ID = 'your_public_google_sheet_id';


    //get data

    $url = 'https://spreadsheets.google.com/feeds/list/'.$gSheet_form_ID.'/public/values?alt=json';

    $file = file_get_contents($url);

    $json = json_decode($file);

    $rows = $json->{'feed'}->{'entry'};


    //get all the same from sheet

    $names = array(); //store names in this array

    foreach($rows as $row) {

        $name = $row->{'gsx$name'}->{'$t'};

        array_push($names, $name); //push data

    }

    

    //Go through each form fields

    foreach ( $form['fields'] as $field ) {

        //check if field type is a select dropdown and id is 2

        if ( $field->type == 'select' && $field->id == $field_ID) {

            //add name and value to the option

            foreach($names as $single_name){

                $choices[] = array('text' => $single_name, 'value' => $single_name );

            }

            //Add a place holder

            $field->placeholder = 'Select a Name';

            //Add the new names to the form choices

            $field->choices = $choices;

        }

    }

    return $form; //return form

}

工作选择字段预览


https://img1.sycdn.imooc.com/657c0dda0001d64a06530249.jpg

查看完整回答
反对 回复 2023-12-15
?
qq_花开花谢_0

TA贡献1835条经验 获得超6个赞

感谢您提供了这个很好的例子。最初,它对我不起作用,直到我意识到“名字”是我想要的。是列的标题(名称)并且是硬编码的 - 我修改了它。我还缺少在列表之间浏览的功能,因此我为此添加了一个变量。


最后,如果您在 WordPress 上运行,我建议使用“代码片段”插件而不是直接将代码添加到functions.php中 - 它更干净+如果您的主题被修改/更改也不会停止工作。


见下文:


$location_form_id = '21';

add_filter( 'gform_pre_render_'.$location_form_id, 'populate_posts' );

add_filter( 'gform_pre_validation_'.$location_form_id, 'populate_posts' );

add_filter( 'gform_pre_submission_filter_'.$location_form_id, 'populate_posts' );

add_filter( 'gform_admin_pre_render_'.$location_form_id, 'populate_posts' );



function populate_posts( $form ) {


    //the select feild id you want the names to load

    $field_ID = 'FIELD_ID_HERE';

    //your g sheet ID

    $gSheet_form_ID = 'SHEET_ID_HERE';

    // which column to scan - what is the heading name

    $column_name = 'COLUMN_HEADING_NAME_HERE';

    $placeholder = 'YOUR_PLACEHOLDER_HERE';

    $list_number = '1';



    //get data

    $url = 'https://spreadsheets.google.com/feeds/list/'.$gSheet_form_ID.'/'.$list_number.'/public/values?alt=json';

    

    $file = file_get_contents($url);

    $json = json_decode($file);

    $rows = $json->{'feed'}->{'entry'};



    //get all the same from sheet

    $names = array(); //store names in this array

    foreach($rows as $row) {

        $name = $row->{'gsx$'.$column_name}->{'$t'};

        array_push($names, $name); //push data

    }

    

    //Go through each form fields

    foreach ( $form['fields'] as $field ) {

        //check if field type is a select dropdown and id is correct

        if ( $field->type == 'select' && $field->id == $field_ID) {

            //add name and value to the option

            foreach($names as $single_name){

                $choices[] = array('text' => $single_name, 'value' => $single_name );

            }

            //Add a place holder

            $field->$placeholder;

            //Add the new names to the form choices

            $field->choices = $choices;

            // Print out the contents of the array (troubleshooting only)

            //echo '<pre>'; print_r($choices); echo '</pre>';

        }

    }

    return $form; //return form

}

还有什么可以改进的地方?

  1. 用于通过 Google Sheets API 检索数据的 URL 使用的是 API v3,该 API 将于 2021 年 6 月 8 日弃用。如果有人对如何改进 APIv4 的代码有想法,请告诉我们!

  2. 如果您要检查“较短”的列中的单元格,与另一个相比,您最终会在数组中得到空值。应该对代码中的空值进行检查(添加起来应该很简单)。


查看完整回答
反对 回复 2023-12-15
  • 2 回答
  • 0 关注
  • 62 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信