Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple example to open an existing Google Spreadsheet and look up the Gender of the names in the names column
<?php
/**
* Simple example to open an existing Google Spreadsheet and look up the Gender of the names in the names column
*/
require 'vendor/autoload.php';
use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;
$accessToken = 'ACCESSTOKEN';
$serviceRequest = new DefaultServiceRequest($accessToken);
ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
$spreadsheet = $spreadsheetFeed->getByTitle('Examples');
$worksheetFeed = $spreadsheet->getWorksheets();
$worksheet = $worksheetFeed->getByTitle('Gender');
$listFeed = $worksheet->getListFeed();
foreach ($listFeed->getEntries() as $entry) {
$values = $entry->getValues();
if ($values['gender'] == '') {
$gender_json = file_get_contents('https://api.genderize.io/?name=' . $values['name']);
$gender_array = json_decode($gender_json, true);
$values['gender'] = $gender_array['gender'];
$values['probability'] = $gender_array['probability'] * 100 . '%';
}
$entry->update($values);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment