验证并使用Google电子表格作为Web应用程序的数据库

时间:2011-06-07 18:55:52

标签: javascript google-sheets google-spreadsheet-api

围绕这个主题似乎有很多问题,但似乎没有人回答我的问题。我有一个简单的网站,注册表单,当用户输入他们的电子邮件时,我想将其作为我已设置的Google电子表格中的新行推送。我不希望用户对此电子表格进行身份验证甚至了解。我如何进行身份验证,以便开始使用Google API?代码/伪代码将不胜感激!以下是一些不回答我问题的例子:

Using Google Spreadsheet as DB for apps

Google spreadsheet as db for web applications

2 个答案:

答案 0 :(得分:2)

以下是访问Google Spreadsheets的库的链接:

https://github.com/EastCloud/node-spreadsheets

注意:我实际上并没有使用过这个lib

答案 1 :(得分:0)

您需要使用PHP和Zend GData Library。

当您将表单发布到PHP脚本时,您需要将所有变量收集到一个关联数组中,然后将其传递给Zend_Gdata_Spreadsheets的insertRow方法。

请务必注意,您的电子表格必须包含列标题才能使我的示例正常工作。例如。名字/姓氏,重要的是要注意,当您在脚本中定位这些列标题时,它们将需要全部小写并删除空格,因为这是电子表格期望它们的方式。

以下是PHP脚本的基本示例:

<?php
        $errors = array(); // use this to create an associative array to json encode and send back any errors
        $rowData = array(); // this will be the associative array that gets passed to the insertRow method

        $firstName = $_POST['firstName'];

        if(isset($firstName)){
            $rowData['firstname'] = $firstName; // note the key 'firstname' must equal the column header in your spreadsheet which you are inserting the value into. the column header name key should be all lowercase and not contain spaces regardless of if you have it in the spreadsheet
        }else{
            $errors['firstname'] = '1';
        }

        $lastName = $_POST['lastName'];

        if(isset($lastName)){
            $rowData['lastname'] = $lastName; // note the key 'lastname' must equal the column header in your spreadsheet which you are inserting the value into. the column header name key should be all lowercase and not contain spaces regardless of if you have it in the spreadsheet
        }else{
            $errors['lastname'] = '1';
        }


        set_include_path($_SERVER['DOCUMENT_ROOT'] . '/library/');

        $spreadsheetKey = 'your-spreadsheet-key';
        $worksheetId = 'your-worksheet-id'; // if you only have one worksheet this will be 'od6'

        require_once 'Zend/Loader/Autoloader.php';
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->setFallbackAutoloader(true);

        $user = "your-user-name-at-gmail-dot-com";
        $pass = "your-password";

        $service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
        $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
        $spreadsheetService = new Zend_Gdata_Spreadsheets($client);

        $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
        $query->setSpreadsheetKey($spreadsheetKey);
        $feed = $spreadsheetService->getWorksheetFeed($query); 

        global $spreadsheetService,$spreadsheetKey,$worksheetId,$rowData;
        $insertedListEntry=$spreadsheetService->insertRow($rowData,$spreadsheetKey,$worksheetId);
        $returnObject['success'] = 'true';
        echo(json_encode($returnObject));
?>

请告诉我这是否适合您。