我创建excel文件并保护此文件中的某些字段。在Office excel中,我的一些单元格被锁定以进行编辑,开始我在google驱动器中使用convert上传我的excel文件 - true(是否将此文件转换为相应的Google Docs格式。(默认值:false))但在google电子表格中我可以编辑锁定的单元格,如何锁定谷歌电子表格中的一些单元格?
创建excel文件
$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
);
现在我只能在我的计算机文件中编辑E6:F36单元格,然后在谷歌驱动器中上传
$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);
}
我想去谷歌电子表格api包asimlqt/php-google-spreadsheet-client
,但找不到如何保护
$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');
或如何使用asimlqt/php-google-spreadsheet-client
保护单元格?
并且在google电子表格中我可以编辑任何单元格((((谁知道google spreat sheet中受保护的单元格很热?
更新
我看了Google Sheets API并尝试创建请求,我有
$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);
但是
Client error response
[status code] 401
[reason phrase] Unauthorized
[url] https://sheets.googleapis.com/v4/spreadsheets/1M_NFvKMZ7Rzbj9ww86AJRMto1UesIy71840r2sxbD5Y:batchUpdate?key=myAccessToken
早期我有谷歌Api Clien的访问令牌,我可以更改单元格并使用谷歌电子表格更新并正常工作但https://sheets.googleapis.com/v4/spreadsheets/'.$spreadsheetId.':batchUpdate?key='.$arrayAccessTokenClient['access_token'],
返回401,我不明白为什么以及如何纠正。帮助
答案 0 :(得分:0)
我认为谷歌电子表格允许在他们成为Google文档时编辑受保护的MSExcel表格。 MSExcel的规则并不总是适用于Google表格。从Adding Named or Protected Ranges阅读:
以下spreadsheets.batchUpdate请求包含两个请求 对象。第一个给出范围A1:E3的名称"计数"。第二 为A4:E4范围提供警告级保护。 此级别 保护仍然允许编辑范围内的单元格,但是 在进行更改之前提示警告。
此thread也分享相同的观点。
但是,如果您要保护Google电子表格文件,请检查this guide。
答案 1 :(得分:0)
我看了documentation和
$addProtectedRange['requests'] = [
'addProtectedRange' => [
'protectedRange' => [
'range' => [
'sheetId' => $worksheetGid,
'startRowIndex' => $startRowIndex,
'endRowIndex' => $endRowIndex,
'startColumnIndex' => $startColumnIndex,
'endColumnIndex' => $endColumnIndex,
],
'description' => "Protecting total row",
'warningOnly' => false,
'editors' => [
'users' => [
"shuba.ivan.vikt@gmail.com"
]
]
],
]
];
此file_get_contents
变体,因为Guzzle没有从谷歌返回json响应,如下所示:
{
"protectedRangeId": number,
"range": {
object(GridRange)
},
"namedRangeId": string,
"description": string,
"warningOnly": boolean,
"requestingUserCanEdit": boolean,
"unprotectedRanges": [
{
object(GridRange)
}
],
"editors": {
object(Editors)
},
}
我为Guzzle reasponse创建了新的question
这是更新受保护编辑单元格
的变体 $addProtectedRangeJson = json_encode($addProtectedRange);
$url = 'https://sheets.googleapis.com/v4/spreadsheets/' . $spreadsheetId . ':batchUpdate';
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/json\r\n" .
"Authorization: Bearer $arrayAccessTokenClient\r\n",
'content' => $addProtectedRangeJson
)
);
$context = stream_context_create($opts);
$response = file_get_contents($url, FALSE, $context);
$answer = json_decode($response, TRUE);
$addProtectedRangeUpdate['requests'] = [
'updateProtectedRange' => [
'protectedRange' => [
'protectedRangeId' => $protectedRangeId,
'warningOnly' => false,
'editors' => [
'users' => [
"shuba.ivan.vikt@gmail.com"
]
]
],
'fields' => "namedRangeId,warningOnly,editors"
]
];
$addProtectedRangeUpdateJson = json_encode($addProtectedRangeUpdate);
$optsUpdate = array('http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/json\r\n" .
"Authorization: Bearer $arrayAccessTokenClient\r\n",
'content' => $addProtectedRangeUpdateJson
)
);
$contextUpdate = stream_context_create($optsUpdate);
$responseUpdate = file_get_contents($url, FALSE, $contextUpdate);
$answerUpdate = json_decode($responseUpdate, TRUE);
这是Guzzle,但Guzzle没有谷歌json回应这是不好我等待也许谁知道它是什么问题,因为标准的PHP函数file_get_contents
工作得很好,我有json谷歌响应。但是如果没有响应,这个变体工作正常,电子邮件中的细胞保护
$guzzle = new Client();
$postCell = $guzzle
->post('https://sheets.googleapis.com/v4/spreadsheets/' . $spreadsheetId . ':batchUpdate',
[],
$addProtectedRangeJson
)
->addHeader('Authorization', 'Bearer ' . $arrayAccessTokenClient)
->addHeader('Content-type', 'application/json')
;
$postCell
->send()
->getBody(true)
;
$contents = (string) $postCell->getBody();// get body that I post -> my request, not response
$contents = $postCell->getBody()->getContents();// not find getContents, ask me did you mean to call getContentMd5
$answer = json_decode($postCell->getResponse()->getBody(), true);