Authenticating and using a Google Spreadsheet as a

2019-04-15 19:59发布

There seem to be many questions in around this topic but none seem to answer my question. I have a simple website with a signup form, and when users enter their email I want to push this as a new row in a Google Spreadsheet I have already setup. I don't want the user to authenticate or even know about this spreadsheet. How do I authenticate so I can start using the Google API? Code/pseudocode would be greatly appreciated! Here are some examples that do not answer my question:

Using Google Spreadsheet as DB for apps

Google spreadsheet as db for web applications

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-04-15 20:04

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

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

Note: Ive not actually used this lib

查看更多
欢心
3楼-- · 2019-04-15 20:22

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.

查看更多
登录 后发表回答