【问题标题】:Search a google spread sheet using JQuery使用 JQuery 搜索谷歌电子表格
【发布时间】:2020-06-07 16:18:26
【问题描述】:

我想在我的 google 电子表格中搜索包含字符串的特定列。 我使用 PHP 从工作表中读取数据并将它们存储到一个数组中。 打印数组会显示列值,但是当使用 (json_encode) 时,我只得到数组中的最后一个值,这很奇怪。

这是我的代码:

<?php
require 'vendor/autoload.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Publications</title>
<meta charset="utf-8">
</head>
<body>
    <?php
    $client = new \Google_Client();
    $client->setApplicationName('Google Sheets and PHP');
    $client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
    $client->setAccessType('offline');
    $credentials_file = __DIR__ . "/TestCase-641dce71a5df.json";
    $spreadsheetId = '1wP-NqVcGK9CBSPQRQ_oognVuKF07wsuiHjOAP0rQ3xI';
    $range = 'Responses!F2:F';
    $client->setAuthConfig($credentials_file);
    $service = new Google_Service_Sheets($client);
    $response = $service->spreadsheets_values->get($spreadsheetId, $range);
    $values = $response->getValues();
    if (empty($values)) {
        print "No data found.\n";
    } else {
        foreach ($values as $row) {
            $values_array = array($row[0]);
            echo "Normal:",json_encode($values_array), "\n";
        }
    }
    ?>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#myInput").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    var complexArray = <?php echo json_encode($values_array); ?>;
    console.log(complexArray);
    $("complexArray").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});
</script>
<h2>Filterable Table</h2>
<p>Type something in the input field to search</p>  
<input id="myInput" type="text" placeholder="Search..">
<br><br>
</body>
</html>

【问题讨论】:

    标签: php jquery arrays google-sheets google-sheets-api


    【解决方案1】:

    您正在将数组重写为此处的最后一个值:

    $values_array = array($row[0]);
    

    试试

    var complexArray = <?php echo json_encode($values); ?>;
    

    【讨论】:

      猜你喜欢
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-30
      • 2012-05-02
      • 1970-01-01
      相关资源
      最近更新 更多