亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何動態填充重力形式從 Google Sheets 數據中選擇(下拉)菜單項

如何動態填充重力形式從 Google Sheets 數據中選擇(下拉)菜單項

PHP
長風秋雁 2023-12-15 16:26:21
我正在嘗試使用谷歌表格中的數據動態填充下拉菜單中可供選擇的選項。數據位于 A 列(目前為 A2:A4,但這可能會發生變化),并將包括可用員工的姓名。因此,如果:   A1 name 2 jack 3 Bob4 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;  }
查看完整描述

2 回答

?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

您需要使用重力形式提供的少量filters來實現achieve。只需要四個過濾器。

  1. gform_pre_render_

  2. gform_pre_validation_

  3. gform_pre_submission_filter_

  4. 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

}

工作選擇字段預覽


https://img1.sycdn.imooc.com/657c0dda0001d64a06530249.jpg

查看完整回答
反對 回復 2023-12-15
?
qq_花開花謝_0

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

}

還有什么可以改進的地方?

  1. 用于通過 Google Sheets API 檢索數據的 URL 使用的是 API v3,該 API 將于 2021 年 6 月 8 日棄用。如果有人對如何改進 APIv4 的代碼有想法,請告訴我們!

  2. 如果您要檢查“較短”的列中的單元格,與另一個相比,您最終會在數組中得到空值。應該對代碼中的空值進行檢查(添加起來應該很簡單)。


查看完整回答
反對 回復 2023-12-15
  • 2 回答
  • 0 關注
  • 234 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號