如何动态填充Google表格数据中的重力表单选择下拉菜单项

问题描述

我正在尝试使用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;
  }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)