猿问

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

我正在尝试使用谷歌表格中的数据动态填充下拉菜单中可供选择的选项。数据位于 A 列(目前为 A2:A4,但这可能会发生变化),并将包括可用员工的姓名。


因此,如果:


   A

1 name 

2 jack 

3 Bob

4 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;

  }


长风秋雁
浏览 144回答 2
2回答

白衣非少年

您需要使用重力形式提供的少量filters来实现achieve。只需要四个过滤器。gform_pre_render_gform_pre_validation_gform_pre_submission_filter_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}工作选择字段预览

qq_花开花谢_0

感谢您提供了这个很好的例子。最初,它对我不起作用,直到我意识到“名字”是我想要的。是列的标题(名称)并且是硬编码的 - 我修改了它。我还缺少在列表之间浏览的功能,因此我为此添加了一个变量。最后,如果您在 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 ) {&nbsp; &nbsp; //the select feild id you want the names to load&nbsp; &nbsp; $field_ID = 'FIELD_ID_HERE';&nbsp; &nbsp; //your g sheet ID&nbsp; &nbsp; $gSheet_form_ID = 'SHEET_ID_HERE';&nbsp; &nbsp; // which column to scan - what is the heading name&nbsp; &nbsp; $column_name = 'COLUMN_HEADING_NAME_HERE';&nbsp; &nbsp; $placeholder = 'YOUR_PLACEHOLDER_HERE';&nbsp; &nbsp; $list_number = '1';&nbsp; &nbsp; //get data&nbsp; &nbsp; $url = 'https://spreadsheets.google.com/feeds/list/'.$gSheet_form_ID.'/'.$list_number.'/public/values?alt=json';&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $file = file_get_contents($url);&nbsp; &nbsp; $json = json_decode($file);&nbsp; &nbsp; $rows = $json->{'feed'}->{'entry'};&nbsp; &nbsp; //get all the same from sheet&nbsp; &nbsp; $names = array(); //store names in this array&nbsp; &nbsp; foreach($rows as $row) {&nbsp; &nbsp; &nbsp; &nbsp; $name = $row->{'gsx$'.$column_name}->{'$t'};&nbsp; &nbsp; &nbsp; &nbsp; array_push($names, $name); //push data&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; //Go through each form fields&nbsp; &nbsp; foreach ( $form['fields'] as $field ) {&nbsp; &nbsp; &nbsp; &nbsp; //check if field type is a select dropdown and id is correct&nbsp; &nbsp; &nbsp; &nbsp; if ( $field->type == 'select' && $field->id == $field_ID) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //add name and value to the option&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($names as $single_name){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $choices[] = array('text' => $single_name, 'value' => $single_name );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Add a place holder&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $field->$placeholder;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Add the new names to the form choices&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $field->choices = $choices;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Print out the contents of the array (troubleshooting only)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //echo '<pre>'; print_r($choices); echo '</pre>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $form; //return form}还有什么可以改进的地方?用于通过 Google Sheets API 检索数据的 URL 使用的是 API v3,该 API 将于 2021 年 6 月 8 日弃用。如果有人对如何改进 APIv4 的代码有想法,请告诉我们!如果您要检查“较短”的列中的单元格,与另一个相比,您最终会在数组中得到空值。应该对代码中的空值进行检查(添加起来应该很简单)。
随时随地看视频慕课网APP
我要回答