2 回答

TA貢獻1155條經驗 獲得超0個贊
您需要使用重力形式提供的少量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
}
工作選擇字段預覽

TA貢獻1835條經驗 獲得超7個贊
感謝您提供了這個很好的例子。最初,它對我不起作用,直到我意識到“名字”是我想要的。是列的標題(名稱)并且是硬編碼的 - 我修改了它。我還缺少在列表之間瀏覽的功能,因此我為此添加了一個變量。
最后,如果您在 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 ) {
//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,該 API 將于 2021 年 6 月 8 日棄用。如果有人對如何改進 APIv4 的代碼有想法,請告訴我們!
如果您要檢查“較短”的列中的單元格,與另一個相比,您最終會在數組中得到空值。應該對代碼中的空值進行檢查(添加起來應該很簡單)。
- 2 回答
- 0 關注
- 234 瀏覽
添加回答
舉報