我正在尝试使用 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;
}
最佳答案
您需要使用重力形式提供的一些过滤器
来实现
这一点。只需要四个过滤器。
gform_pre_render_
gform_pre_validation_
gform_pre_submission_filter_
gform_admin_pre_render_
您需要循环遍历表单 ID XX
的所有字段
,并检查您选择的字段是否是实际的下拉列表
字段表示选择
字段。
要push
在工作表中找到的所有新名称,我们可以使用array_push
方法,然后循环遍历该array
以获取所有名称已存储。
如果您想在选择的字段
中添加占位符,最后只需返回$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
}
工作选择字段预览
关于php - 如何动态填充重力形式从 Google Sheets 数据中选择(下拉)菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63758064/