I need to implement a filter that will update a form entry after it’s been submitted. To do this, I need the filter to go pull information from a Google sheet I have set up, searching for the correct row using the Entry ID (which will already be somewhere in column A).
I wrote something to handle this, but I can’t get it to work. I’m getting the following error:
FATAL ERROR syntax error, unexpected single-quoted string “/public_html/wp-content/plugin…” on line number 10
Can anyone take a look and tell me where I may have messed this up? This is my first time working with Google APIs so some things may not yet be obvious to me that are obvious to others. I set up the API key, the OAuth consent screen, the Service Account, and I downloaded the JSON file and then uploaded it to a folder I created in the root. It appears that I don’t have the path right for the autoload.php file, but I don’t know how to fix it. When I search, I see a lot of autoload files. The one I included in the code looked to be the right one, but I’m not sure.
// Use Gravity Forms hook to capture the form submission.
add_action('gform_after_submission', 'update_gravity_form_entry_with_sheet_data', 10, 2);
function update_gravity_form_entry_with_sheet_data($entry, $form) {
// Specify your Google Sheet ID and range (e.g., "Sheet1!A1:B10")
$spreadsheetId = '1kB66ktI2YJviz4AD7YtGHe6kmgObQxHD_ZGzLRhGHYw';
$range = 'Sheet1!A1:D1000000'; // Adjust the range based on your data
// Include the Google API Client
require_once 'public_html/wp-content/plugins/gc-google-sheets/vendor
/
autoload.php'; // Make sure this points to where your composer autoload file is.
// Set up Google Client
$client = new Google_Client();
$client->setApplicationName('grantstream-gsheets-api');
$client->setScopes([Google_Service_Sheets::SPREADSHEETS_READONLY]);
$client->setAuthConfig('/cred/grantstream-gsheets-api-4f1ea78fd367.json');
$client->setAccessType('offline');
$service = new Google_Service_Sheets($client);
// Fetch data from Google Sheets
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
// Ensure there's data in the Google Sheet
if (empty($values)) {
error_log("No data found in Google Sheet.");
return;
}
// Get the identifier from the Gravity Form entry
$identifier_field_id = 275; // Adjust this to the correct field ID
$identifier_value = rtrim($entry[$identifier_field_id]); // Trim spaces
// Search for the matching row in Google Sheets
$matched_row = null;
foreach ($values as $row) {
if (!empty($row[0]) && $row[0] == $identifier_value) { // Assuming the identifier is in column A (index 0)
$matched_row = $row;
break;
}
}
// If no matching row is found, exit
if (!$matched_row) {
error_log("No matching row found in Google Sheet for identifier: " . $identifier_value);
return;
}
// Extract relevant data from the matched row
$google_data_column1 = $matched_row[1] ?? ''; // Assuming the value is in column B (index 1)
$google_data_column2 = $matched_row[2] ?? ''; // Assuming the value is in column C (index 2)
$google_data_column3 = $matched_row[3] ?? ''; // Assuming the value is in column C (index 3)
// Update the Gravity Forms entry with data from Google Sheet
$entry_id = $entry['id']; // Get the current entry ID
// Update the entry using Gravity Forms' GFAPI
GFAPI::update_entry_field($entry_id, 451.3, $google_data_column1); // Field ID 1
GFAPI::update_entry_field($entry_id, 451.6, $google_data_column2); // Field ID 2
GFAPI::update_entry_field($entry_id, 452, $google_data_column3); // Field ID 3
}