问题描述
我正在尝试使用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
。只需四个过滤器。
-
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
}
正在选择字段预览
,谢谢你的这个很好的例子。最初,它对我不起作用,直到我意识到“名称”是列的标题(名称)并且是硬编码的 - 我修改了它。我还缺少在列表之间浏览的功能,因此我为此添加了一个变量。
最后,如果您的一方在 WordPress 上运行,我建议使用“代码片段”插件,而不是直接将代码添加到 functions.php - 它更干净+ 如果您的主题被修改/更改不会停止工作。
见下文:
$location_form_id = '21';
add_filter( 'gform_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
}
还有什么可以改进的?
-
用于通过 Google Sheets API 检索数据的 URL 使用的是 API v3,该版本将于 2021 年 6 月 8 日弃用。如果有人对如何改进 APIv4 的代码有任何想法,请告诉我们!
-
如果您要检查的列中的单元格比另一列“短”,则数组中最终会出现空值。应该检查代码中的空值(添加应该很简单)。