Authenticating and using a Google Spreadsheet as a database for a web app

别等时光非礼了梦想. 提交于 2019-12-04 20:23:20

Here's a link to a library for accessing Google Spreadsheets:

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

Note: Ive not actually used this lib

You will need to use PHP and the Zend GData Library.

When you post your form to your PHP script, you'll need to collect all of the variables into an associative array that you then pass to the insertRow method of Zend_Gdata_Spreadsheets.

It's important to note that your spreadsheet must contain column headers for my example to work. E.g. First Name / Last Name and it's important to note that when you target these column headers in a script, they will need to be all lowercase and stripped of spaces because this is how the spreadsheet expects them.

Here is a basic example of the PHP script:

<?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));
?>

Let me know if this works for you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!