使用 Google 表格作为数据库?

发布于 2024-10-17 17:34:07 字数 883 浏览 0 评论 0原文

我想为一个网站创建一个非常简单的 PHP 页面,它将显示一个时间表/日历之类的数据,其中每个时段要么是空闲的,要么是有一些预约的。

因为所有数据实际上只是一张表,例如 {month, day, hour, talk_name, talk_description},所以我想为什么不使用 Google Sheets 电子表格作为数据库。好的,主要原因是我只是在阅读有关如何在 PHP 中使用 MySQL 的书籍,所以我绝对达不到以下水平:

  • 创建一个漂亮的管理界面来管理事件
  • 使整个事情安全(我的意思是所有我关于安全的想法是使用 .htaccess 作为管理文件夹,并使该网站在其他地方只读)。

另一方面,每个人都可以使用 Google 电子表格来编辑表格,这样安全方面和 UI 方面都将得到解决。

我的问题是你会建议我如何去做? Google 表格可以以 XML 和 CSV 格式发布。我可以只使用 fgetcsv 来获取数据吗?你能给我一些如何解析 csv 的简单例子吗?它是否高效(好吧,每天的浏览量将少于 50 次),如果我会做这样的事情(抱歉抽象语法)?

$source_csv = fgetcsv(...);

get_talk_name(x,y,z) {
  for all rows in $source_csv {
    if (month == x && day == y && hour == z) return talk_name
  }
}

get_talk_desc(x,y,z) {
  for all rows in $source_csv {
    if (month == x && day == y && hour == z) return talk_name
  }
}

I would like to create a very simple PHP page for a site, which would show a timetable / calendar like data, where each slot would be either free or would have some appointment in it.

Because all the data is actually just one table, something like {month, day, hour, talk_name, talk_description}, I thought why not use a Google Sheets Spreadsheet as the database. OK, the main reason is that I'm just reading books about how to use MySQL in PHP, so I'm definitely not on a level to:

  • create a nice admin interface for managing the events
  • make the whole thing safe (I mean all my idea about safety is to use .htaccess for an admin folder and make the site read-only elsewhere).

On the other hand everyone could use Google spreadsheets for editing the table, so this way both the security aspects and the UI aspects would be solved.

My question is that how would you recommend me to do that? Google Sheets can both publish in XML and CSV formats. Can I just use fgetcsv to get the data? Can you give me some simple examples how to parse the csv, and if it would be efficient (ok, it will be less than 50 views a day), if I would do something like this (sorry for the abstract syntax)?

$source_csv = fgetcsv(...);

get_talk_name(x,y,z) {
  for all rows in $source_csv {
    if (month == x && day == y && hour == z) return talk_name
  }
}

get_talk_desc(x,y,z) {
  for all rows in $source_csv {
    if (month == x && day == y && hour == z) return talk_name
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扎心 2024-10-24 17:34:07

因此,虽然它可能不明智或不具有可扩展性,但是的,您可以做到。尝试以下代码:

<?php
$url = "https://spreadsheets.google.com/pub?hl=en&hl=en&key=0AupgXsRU8E9UdC1DY0toUUJLV0M0THM4cGJTSkNSUnc&output=csv";
$row=0;

if (($handle = fopen($url, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}

基本上,将电子表格发布为公开,通过手动编辑 URL(即 output=csv)将输出更改为 csv(从默认 HTML),获取它,然后迭代它逐行使用 fgetcsv

如果电子表格如下所示:

在此处输入图像描述

这将为相关 csv 输出以下内容:

array(2) {
  [0]=>
  string(4) "Name"
  [1]=>
  string(5) "Value"
}
array(2) {
  [0]=>
  string(3) "Foo"
  [1]=>
  string(5) "13123"
}
array(2) {
  [0]=>
  string(3) "Bar"
  [1]=>
  string(3) "331"
}

So, while it might not be wise or scalable, yes, you can do it. Try this code:

<?php
$url = "https://spreadsheets.google.com/pub?hl=en&hl=en&key=0AupgXsRU8E9UdC1DY0toUUJLV0M0THM4cGJTSkNSUnc&output=csv";
$row=0;

if (($handle = fopen($url, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}

Basically, publish the spreadsheet as public, change output to csv (from the default HTML) by manually editing the URL (ie, output=csv), fetch it, and then iterate over it line by line using fgetcsv.

If the spreadsheet looks like this:

enter image description here

This will output the following for the csv in question:

array(2) {
  [0]=>
  string(4) "Name"
  [1]=>
  string(5) "Value"
}
array(2) {
  [0]=>
  string(3) "Foo"
  [1]=>
  string(5) "13123"
}
array(2) {
  [0]=>
  string(3) "Bar"
  [1]=>
  string(3) "331"
}
剩一世无双 2024-10-24 17:34:07

你可以尝试这个只是为了好玩。但如果您只需要一个公共日历,请使用 Google 日历之类的工具。 Google Calendar API 可让您通过程序更新日历。您还可以使用 Google Embeddable Calendar Helper 在您的网站中嵌入日历。

不过,不如从头开始编程那么有趣......;-)

You could try this just for fun. But if you just need a communal calendar, use something like Google Calendar. The Google Calendar API enables you to update your calendar from a program. And you can embed a calendar in your website using the Google Embeddable Calendar Helper.

Not as much fun as programming it from scratch though ... ;-)

無心 2024-10-24 17:34:07

查看此页面,了解使用 GDocs 电子表格作为工具的非常简单的方法CRUD DB。 使用遵循此模式,但您需要首先从 github 下载 Zend 类和 GDocs/PHP 文件...

<?php            // Zend library include path
set_include_path(get_include_path() . PATH_SEPARATOR . "$_SERVER[DOCUMENT_ROOT]/ZendGdata-1.8.1/library");
include_once("Google_Spreadsheet.php");
    $u = "[email protected]";
    $p = "password";
    $ss = new Google_Spreadsheet($u,$p);
    $ss->useSpreadsheet("My Spreadsheet");
                // if not setting worksheet, "Sheet1" is assumed
                // $ss->useWorksheet("worksheetName");
$row = array 
    ( "name" => "John Doe", "email" => "[email protected]", "comments" => "Hello world" );
if ($ss->addRow($row)) echo "Form data successfully stored using Google Spreadsheet";
    else echo "Error, unable to store spreadsheet data";
?>

Check out this page for a pretty straightforward approach to using a GDocs spreadsheet as a CRUD DB. Usage follows this pattern, but you need to download a Zend class, and a GDocs/PHP file from github first...

<?php            // Zend library include path
set_include_path(get_include_path() . PATH_SEPARATOR . "$_SERVER[DOCUMENT_ROOT]/ZendGdata-1.8.1/library");
include_once("Google_Spreadsheet.php");
    $u = "[email protected]";
    $p = "password";
    $ss = new Google_Spreadsheet($u,$p);
    $ss->useSpreadsheet("My Spreadsheet");
                // if not setting worksheet, "Sheet1" is assumed
                // $ss->useWorksheet("worksheetName");
$row = array 
    ( "name" => "John Doe", "email" => "[email protected]", "comments" => "Hello world" );
if ($ss->addRow($row)) echo "Form data successfully stored using Google Spreadsheet";
    else echo "Error, unable to store spreadsheet data";
?>
Saygoodbye 2024-10-24 17:34:07

我没有足够的代表在上面添加评论,但将工作表导出到 CSV 的新方法确实有效。

您从工作表(公开)共享的网址:

https://docs.google.com/spreadsheets/d/9999999999999/edit?usp=sharing

更改结尾 /edit?usp=sharing /export?format=csv

例如:

https://docs.google.com/spreadsheets/d/999999999999999/export?format=csv

来源:https://productforums.google.com/d/msg/docs/An-nZtjaupU/llWy4eYFywcJ

I don`t have enough rep to add a comment above, but the new method of exporting a sheet to CSV does work.

Your url as (publicly) shared from sheets:

https://docs.google.com/spreadsheets/d/9999999999999/edit?usp=sharing

Change the end /edit?usp=sharing to /export?format=csv

Like:

https://docs.google.com/spreadsheets/d/99999999999999/export?format=csv

Source: https://productforums.google.com/d/msg/docs/An-nZtjaupU/llWy4eYFywcJ

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文