如何从 Google Sheets 数据动态填充 Gravity Forms select(下拉)菜单项

How to dynamically populate Gravity Forms select (dropdown) menu items from Google Sheets data

我正在尝试使用 google 工作表中的数据在下拉菜单中动态填充可供选择的选项。数据位于 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;
  }

你需要使用一些filters重力形式给achieve这个。只需要四个过滤器。

  1. gform_pre_render_
  2. gform_pre_validation_
  3. gform_pre_submission_filter_
  4. gform_admin_pre_render_

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

为了 push 在工作表中找到的所有新名称,我们可以使用 array_push 方法,然后遍历 array 以获取所有已存储的名称。

如果你想在 select field 中添加一个占位符,最后只是 return $form

在下面的代码中只需添加您自己的 $form_id、select $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
}

工作Select现场预览

谢谢你提供的这个很好的例子。最初,它对我不起作用,直到我意识到 'name' 是列的标题(名称)并且是 hard-coded - 我修改了它。我还缺少在列表之间浏览的功能,因此我为此添加了一个变量。

最后,如果您在 WordPress 上 运行,我建议使用 'Code Snippets' 插件而不是直接将代码添加到 functions.php - 它更干净 + 不会停止工作如果您的主题是 modified/changed.

见下文:

$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,将于 6 月 8 日弃用, 2021。如果有人对如何改进 APIv4 的代码有任何想法,请告诉我们!

  2. 如果您要检查的列中的单元格与另一列 'shorter' 相比,您最终会在数组中得到空值。应该检查代码中的空值(应该很容易添加)。