使用 PHP API 创建 Google Doc 电子表格的副本


create copy of google doc spreadsheet using php api

我想使用 PHP API 创建谷歌文档的 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>

请帮助我伙计们。这是我项目的最后一点。

请帮助我伙计们。

提前感谢!

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