使用 Google 表格作为数据库?
我想为一个网站创建一个非常简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因此,虽然它可能不明智或不具有可扩展性,但是的,您可以做到。尝试以下代码:
基本上,将电子表格发布为公开,通过手动编辑 URL(即
output=csv
)将输出更改为 csv(从默认 HTML),获取它,然后迭代它逐行使用fgetcsv
。如果电子表格如下所示:
这将为相关 csv 输出以下内容:
So, while it might not be wise or scalable, yes, you can do it. Try this code:
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 usingfgetcsv
.If the spreadsheet looks like this:
This will output the following for the csv in question:
你可以尝试这个只是为了好玩。但如果您只需要一个公共日历,请使用 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 ... ;-)
查看此页面,了解使用 GDocs 电子表格作为工具的非常简单的方法CRUD DB。 使用遵循此模式,但您需要首先从 github 下载 Zend 类和 GDocs/PHP 文件...
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...
我没有足够的代表在上面添加评论,但将工作表导出到 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