asimlqt / php-google-spreadsheet-client

A PHP library for accessing and manipulating Google Spreadsheets
Other
544 stars 152 forks source link

Google spreadsheet, how to protect Cell? #154

Open shubaivan opened 8 years ago

shubaivan commented 8 years ago

I create excel file and proteced some field in this file. In Office excel all fine my some cell locked for edit, begin I upload my excel file in google drive with convert - true (Whether to convert this file to the corresponding Google Docs format. (Default: false)) But in google spread sheet I can edit locked cell, how to locked in google spread sheet some cell ?

create excel file

    $phpExcel = new \PHPExcel();
    $ews = $phpExcel->getSheet(0);
    $ews->setTitle(Reports::LIST_AOG);
    $ews->getProtection()->setSheet(true);
    $ews
        ->getStyle('E6:F36')
        ->getProtection()->setLocked(
            \PHPExcel_Style_Protection::PROTECTION_UNPROTECTED
        );

and now I can edit only E6:F36 cell in my file in computer, then upload in google drive

$insertArray = [
        'mimeType' => $fileUpload->getMimeType(),
        'uploadType' => 'media',
        'data' => file_get_contents($fileUpload),
        'convert' => true
    ];

    $service = new \Google_Service_Drive($client->getGoogleClient());
    $file = new \Google_Service_Drive_DriveFile();

    $file->setTitle($nameFile);
    $file->setMimeType($fileUpload->getMimeType());

    try {
        $createdFile = $service->files->insert($file, $insertArray);
        $spreadsheetId = $createdFile->getId();
    } catch (\Exception $e) {
        $view = $this->view((array)GoogleDriveController::ERROR_OCCURRED . $e->getMessage(), 400);
        return $this->handleView($view);
    }

I fing for google spreadsheet api bundle asimlqt/php-google-spreadsheet-client but not find how to protected

    $serviceRequest = new DefaultServiceRequest($arrayAccessTokenClient['access_token']);
    ServiceRequestFactory::setInstance($serviceRequest);

    $spreadsheetService = new SpreadsheetService();

    $spreadsheet = $spreadsheetService->getSpreadsheetById($spreadsheetId);
    $worksheetFeed = $spreadsheet->getWorksheets();
    $cellFeed = $worksheet->getCellFeed();

    $cellFeed->editCell(1, 1, 'developer');
    $cellFeed->editCell(1, 2, 'hors');
    $cellFeed->editCell(10, 2, 'sum');

or how to protected cell with asimlqt/php-google-spreadsheet-client ?

and In google spread sheet I can any edit any cell (((( Who knows hot to protected cell in google spreat sheet ?

UPDATE

I read Google Sheets API and try create request, this I have

    $arrayAccessTokenClient = json_decode($client->getGoogleClient()->getAccessToken(), true);

    $serviceRequest = new DefaultServiceRequest($arrayAccessTokenClient['access_token']);
    ServiceRequestFactory::setInstance($serviceRequest);

    $spreadsheetService = new SpreadsheetService();

    $spreadsheet = $spreadsheetService->getSpreadsheetById($spreadsheetId);
    $worksheetFeed = $spreadsheet->getWorksheets();
    $worksheet = $worksheetFeed->getByTitle(Reports::LIST_AOG);

        $addProtectedRange['addProtectedRange'] = [
        'protectedRange' => [
            'range' => [
                'sheetId' => $worksheet->getGid(),
                'startRowIndex' => 3,
                'endRowIndex' => 4,
                'startColumnIndex' => 0,
                'endColumnIndex' => 5,
            ],
            'description' => "Protecting total row",
            'warningOnly' => true
        ]
    ];
    $guzzle = new Client();
    $putTeam = $guzzle
        ->post('https://sheets.googleapis.com/v4/spreadsheets/'.$spreadsheetId.':batchUpdate?key='.$arrayAccessTokenClient['access_token'],
            [],
            json_encode($addProtectedRange)
        )
        ->send()
        ->getBody(true);
    $answer = json_decode($putTeam, true);

But have

Client error response
[status code] 401
[reason phrase] Unauthorized
[url]  https://sheets.googleapis.com/v4/spreadsheets/1M_NFvKMZ7Rzbj9ww86AJRMto1UesIy71840r2sxbD5Y:batchUpdate?key=myAccessToken

Early I have Google Api Clien with access token and I can change cell and update with google spread sheet and work fine but https://sheets.googleapis.com/v4/spreadsheets/'.$spreadsheetId.':batchUpdate?key='.$arrayAccessTokenClient['access_token'],

return 401 and I not understand why and how to correct. Help