Google sheets import

 google-sheets-tips-update.json

"google/apiclient""^2.0",

Route::get('excelImport''Admin\AdminController@excelImport')->name('excelImport');

{
  "type""service_account",
  "project_id""excel-sheet-demo",
  "private_key_id""f452ed9f9e2345fc3a6793737254e59efb7dad7ff",
  "private_key""-----BEGIN PRIVATE KEY-----END PRIVATE KEY-----\n",
  "client_email""demo-account-excel-sheet@excel-sheet.com",
  "client_id""105540755601386431894",
  "auth_uri""https://accounts.google.com/o/oauth2/auth",
  "token_uri""https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url""https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url""https://www.googleapis.com/.com"
}

    /**
     * update tips by google sheets.
     *
     * @param  GameTip $manage_tip
     * @return \Illuminate\Http\Response
    */
    public function excelImport(Request $request)
    {
        ini_set('max_execution_time'180);

        $client = new \Google_Client();
        $client->setApplicationName('My PHP App');
        $client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
        $client->setAccessType('offline');
        // $client->setAuthConfig(__DIR__ . './../google-sheets-tips-update.json');

        $client->setAuthConfig(__DIR__ . '/google-sheets-tips-update.json');

  
        $sheets = new \Google_Service_Sheets($client);

        try {
  
        $spreadsheeturl = $request->spreadsheetId;
        $uri_segments = explode('/', $spreadsheeturl);

        $spreadsheetId = $uri_segments[5];

        $response = $sheets->spreadsheets->get($spreadsheetId);


        $fileTitle = $response['sheets'][0]['properties']['title'];
 
        if(!empty($spreadsheetId) && $uri_segments[4] =='d'){
            $range = $fileTitle.'!A1:Z';
           
            // try {
            $rows = $sheets->spreadsheets_values->get($spreadsheetId, $range);
            // } catch (\Exception $e) {
            //     return redirect(route('updateresult'))->with('error', trans('messages.manage_tips.updateGoogleSheets.error'));
            // }
            
            $values = $rows->getValues();
 
            $limitRecord = 100;
            $increaseRecord = 100;
            $totalRecord = count($values);
            $totalRecordCount = count($values);
            $totalRecord = $totalRecord / $limitRecord;
            $totalRecord =  ceil($totalRecord);
            $updateArray = array();
            $startRecord = 1;

            $userId =  \Auth::guard('superadmin')->user()->id;

            $spreadsheet = new Spreadsheet;
            $spreadsheet->spreadsheetId = $spreadsheeturl;
            $spreadsheet->packageId = $request->package;
            $spreadsheet->userId = $userId;

            $spreadsheet->save();

            for ($i = 0; $i < $totalRecord; $i++) {
                if ($limitRecord > $totalRecordCount) {
                    $range = $fileTitle.'!A' . $startRecord . ':Z' . $totalRecordCount . '';
                } else {
                    $range = $fileTitle.'!A' . $startRecord . ':Z' . $limitRecord . '';
                }

                $rows = $sheets->spreadsheets_values->get($spreadsheetId, $range);
                $values = $rows->getValues();

                if ($values) {
                    if($startRecord == 1 ){
                        unset($values[0]);
                        unset($values[1]);
                    }

                    $updateArray = array();
                    foreach ($values as $key => $val) {
                        if (!empty($val)) {

                           $updateArray[$key]['competition'] = isset($val[1]) ? $val[1] : '';
                            
                        }
                    }
                }

                if(!empty($updateArray) && count($updateArray) > 0 )
                {
                    \DB::table('games_tips')->insert($updateArray);
                    $updateArray = array();
                }

                $startRecord = $startRecord + $increaseRecord;
                $limitRecord = $limitRecord + $increaseRecord;
            }

            return redirect(route('updateresult'))->with('success'trans('messages.manage_tips.updateGoogleSheets.success'));
        }
        } catch (\Exception $e) {
            return redirect(route('updateresult'))->with('error'trans('messages.manage_tips.updateGoogleSheets.error'));
        }

        return redirect(route('updateresult'))->with('error'trans('messages.manage_tips.updateGoogleSheets.error'));
    }

Comments

Popular posts from this blog

API

Encryption and Decryption By PHP

Seeder