我想使用PHP API创建google doc excel表的副本。我可以看到java代码,但根本没有PHP代码:(
实际上我已经在谷歌驱动器上有一个excel表我想在网站上显示它,我已经成功了但是问题是每个人都可以看到相同的文件,如果一个用户正在更改文件并且同时用户可以看到更改,因为它已同步到谷歌驱动器。
所以我认为解决方案是,如果我可以创建工作表的副本,并通过PHP API向每个用户显示不同的2份副本。
===============
已编辑的代码
function copyFile($service, $originFileId, $copyTitle) {
$copiedFile = new Google_DriveFile();
$copiedFile->setTitle($copyTitle);
try {
$arr['convert'] = false;
$arr['visibility'] = 'default';
return $service->files->copy($originFileId, $copiedFile,$arr);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
return NULL;
}
function insertPermission($service, $fileId, $value, $type, $role) {
$newPermission = new Google_Permission();
$newPermission->setValue($value);
$newPermission->setType($type);
$newPermission->setRole($role);
try {
return $service->permissions->insert($fileId, $newPermission);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
return NULL;
}
function updateRevision($service, $fileId, $revisionId) {
try {
// First retrieve the revision from the API.
$revisions = $service->revisions;
$revision = $revisions->get($fileId, $revisionId);
echo '<pre>';print_r($revision);
$revision->setPinned(true);
return $revisions->update($fileId, $revisionId, $revision);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
return NULL;
}
function updateRevision1($service, $fileId, $revisionId) {
$patchedRevision = new Google_Revision();
$patchedRevision->setPublished(true);
$patchedRevision->setPublishAuto(true);
$patchedRevision->setPublishedOutsideDomain(true);
try {
return $service->revisions->patch($fileId, $revisionId, $patchedRevision);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
return NULL;
}
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('clientid');
$client->setClientSecret('secret');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$client->setRedirectUri('redirecturi');
$service = new Google_DriveService($client);
if(!isset($_GET['code']) ) {
$authUrl = $client->createAuthUrl();
header('location: '.$authUrl );
die;
}
else{
$authCode = $_GET['code'];
// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);
$new_file = copyFile($service,'fileid','Baby Schema');
echo 'file=<pre>';print_r($new_file);
$permission = insertPermission($service,$new_file['id'],'me','anyone','writer');
echo 'permission=<pre>';print_r($permission);
$revision = updateRevision1($service, $new_file['id'], '1');
echo 'pub=<pre>';print_r($revision);
}
代码工作绝对完美,但它没有做我想要的。
以上代码正在执行这些任务
1) I have one spreadsheet on bhuvneshgupta333@gmail.com
2) I am creating copy when bhuvnesh.gupta@witslog.com login to my abc.com website.
3) I am setting copy to be public on web.
4) Making copy to be published on web.
现在我遇到了问题。
1) In the spreadsheet there are 5 sheets out of them 2 are editable and rest 3 are protected so no one can see my formulas.
2) When copy is created by bhuvnesh.gupta@witslog.com then he becomes the owner of file while file is on bhuvneshgupta333@gmail.com while I want Owner to be bhuvneshgupta not bhuvnesh.gupta because I don't want to show formulas to any one not even bhuvnesh.gupta when file is opened on web.
3) I want to protect 3 sheets of spreadsheet to everyone.
我用这种方式在网上调用spreadhseet。
<iframe width="100%" height="600" src="https://docs.google.com/spreadsheets/d/fileid/edit?usp=sharing;&rm=minimal"></iframe>
请帮助我们。这是我项目的最后一点。
请帮帮我们。
提前致谢!
答案 0 :(得分:0)
google drive API参考确实包含用于文件复制的PhP代码。
/**
* Copy an existing file.
*
* @param Google_DriveService $service Drive API service instance.
* @param String $originFileId ID of the origin file to copy.
* @param String $copyTitle Title of the copy.
* @return DriveFile The copied file. NULL is returned if an API error occurred.
*/
function copyFile($service, $originFileId, $copyTitle) {
$copiedFile = new Google_DriveFile();
$copiedFile->setTitle($copyTitle);
try {
return $service->files->copy($originFileId, $copiedFile);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
return NULL;
}
就在这里:https://developers.google.com/drive/v2/reference/files/copy#try-it