php - 有什么方法可以从共享的 Google Sheet 文档中获取数据吗?

标签 php excel google-sheets google-api google-api-php-client

我需要创建一个网页来显示 Google 表格中的当前价格。有说明书https://developers.google.com/api-client-library/php/auth/web-app#protectauthcode但它需要授权。

有没有办法在用户每次打开我的 php 页面时导入数据,即使他没有 Google 帐户?或者我需要创建服务器到服务器的通信并每次通过 cron 任务同步到 json 文件或我的数据库?

最佳答案

我成功了,这是我的解决方案:

<?php
$apiKey="yourAPIkey";
include_once 'vendor/autoload.php';
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey($apiKey);
$service = new Google_Service_Sheets($client);
$spreadsheetId = 'SheetID';
$range = 'A1:B';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if (count($values) == 0) {
  print "No data found.\n";
} else {
  print "Name, Major:\n";
  foreach ($values as $row) {
    printf("<p>%s, %s</p>", $row[0], $row[1]);
  }
}  
?>

Install library

Get Simple API access key

关于php - 有什么方法可以从共享的 Google Sheet 文档中获取数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41381591/

相关文章:

php - 我怎样才能让 PHP 魔法常量 __FILE__ 与 Eclipse 和 PDT 一起工作

php - 尝试修复此 INSERT UPDATE on DUPLICATE 查询

python - 如何在Python中使用Openpyxl对多行Excel行进行平均?

vba - 将字符串转换为名为范围的列

python - Excel 'LET' 函数无法使用 openpyxl

google-sheets - 突出显示工作表中的第二个实例重复项

php - jQuery 插件无法在加载了 ajax 的页面上运行

php - Wordpress:编辑子主题

javascript - 使用脚本编辑器将谷歌表格中的表情符号 unicode 插入电子邮件

google-sheets - Google Sheet API v4