setAuthConfig($credentials_file); } elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) { // use the application default credentials $client->useApplicationDefaultCredentials(); } else { echo missingServiceAccountDetailsWarning(); return; } $client->setApplicationName("Generate Sheet"); $client->setScopes(['https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.appfolder', 'https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/drive.metadata.readonly', 'https://www.googleapis.com/auth/spreadsheets']); $service = new Google_Service_Sheets($client); function saveSheetValue($service, $range, $raw_data) { try{ $spreadsheetId = '13WK5r38f4qiDujFZmKkmmR0gbElqJ24eO208e826QVo'; $data = [ [ 'range'=> $range, 'values'=> $raw_data ] ]; //executing the request $body = new Google_Service_Sheets_BatchUpdateValuesRequest([ 'valueInputOption' => 'USER_ENTERED', 'data' => $data ]); $result = $service->spreadsheets_values->batchUpdate($spreadsheetId, $body); return $result; } catch(Exception $e) { // TODO(developer) - handle error appropriately header('Content-Type: application/json; charset=UTF-8'); error_log($e->getMessage()); return FALSE; } } $range1 = 'hypothesis!A2:B5'; $range2 = 'variable!A2:B2'; $range3 = 'experiment design!A2:B5'; $range4 = 'data!A2:C41'; $result1 = saveSheetValue($service, $range1, $raw_data1); $result2 = saveSheetValue($service, $range2, $raw_data2); $result3 = saveSheetValue($service, $range3, $raw_data3); $result4 = saveSheetValue($service, $range4, $raw_data4); if($result1 != FALSE && $result2 != FALSE && $result3 != FALSE && $result4 != FALSE) { header( 'Content-Type: application/json; charset=UTF-8' ); echo json_encode( array( "totalUpdatedCells" => $result1['totalUpdatedCells'] + $result2['totalUpdatedCells'] + $result3['totalUpdatedCells'] + $result1['totalUpdatedCells'] ), JSON_UNESCAPED_UNICODE ); } else { header('HTTP/1.1 500 Internal Server Error'); } ?>