Google drive api 将文档创建到Google Drive并更改其纸张大小

Google drive api 将文档创建到Google Drive并更改其纸张大小,google-drive-api,google-docs,google-api-php-client,Google Drive Api,Google Docs,Google Api Php Client,我使用PHP库上传和更新HTML文件,并将其转换为原生Google文档 include_once ROOT.'/google-api-php-client/vendor/autoload.php'; $client = new Google_Client(); $credentialsFile = ROOT.'/google-api-php-client/service_account.json'; if (!file_exists($credentialsFile)) { thro

我使用PHP库上传和更新HTML文件,并将其转换为原生Google文档

include_once ROOT.'/google-api-php-client/vendor/autoload.php';

$client = new Google_Client();

$credentialsFile = ROOT.'/google-api-php-client/service_account.json';
if (!file_exists($credentialsFile)) {
    throw new RuntimeException('Service account credentials Not Found!');
}

$client->setAuthConfig($credentialsFile);
$client->setScopes(Google_Service_Drive::DRIVE);

$service = new Google_Service_Drive($client);

/**
   * Проверка существования папки для документов
   */

$optParams = array(
  'q' => "name='Документы'"
);
$results = $service->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
exit('Нет папки для документов');
} else {
  foreach ($results->getFiles() as $file) {
$papka_doc = $file->getId();
  }
 }
/**
   * Обращение к шаблону
   */

$optParams = array(
  'q' => "name='{$tpl_name}' "
);
$results = $service->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
exit('Нет шаблона');
} else {
  foreach ($results->getFiles() as $file) {
$sh_id = $file->getId();
  }
 }
/**
   * Получение контента из шаблона
   */

$content = $service->files->export($sh_id, 'text/html', array(
'alt' => 'media' ));
$dd =$content->getBody();

$chto = array_keys ($replace);
$chto= $this->translit($chto);


$nachto =array_values ($replace);
$nachto= $this->translit($nachto);


$dd = str_replace($chto, $nachto, $dd);



/**
   * Создание основы для документа
   */
$fileId = $sh_id;
$doc_name = $tpl_name.'_'.date('dmY-Hi');
$fileMetadata = new Google_Service_Drive_DriveFile(array(
  'name' => $doc_name,
  'parents' => array($papka_doc)));
$file = $service->files->copy($fileId, $fileMetadata, array(
  'fields' => 'id'));
$new_file_id = $file->id;

/**
   * Вставка контента в новый документ
   */
$fileMetadata = array(
    'data' => $dd);
$filenew = new Google_Service_Drive_DriveFile();
$file = $service->files->update($new_file_id, $filenew, $fileMetadata);
它将创建一个纸张大小设置为字母的文档。对于我的帐户,默认值为A4,我希望新创建的文档采用这种格式。 有人知道如何设置上传文档的纸张大小吗


注意:我看到了线程,但可能出现了一个方法?

Google drive包含文件,它基本上是一个关于文件信息的元数据列表。其中有些字段可以更新,有些字段不能更新。在我上面链接的页面上,写关于状态的字段

没有关于纸张大小的信息。这可能是因为驱动器不关心打印文件时本地机器设置的纸张大小


回答:不,你不能在谷歌硬盘上设置文件的纸张大小,你可以用应用程序脚本来设置。这有点粗糙,但应该能用。您需要:-

  • 创建使用文档服务(请参阅和)的应用程序脚本函数,以操作给定文档ID的页面参数
  • 发布该脚本,以便将其作为端点调用
  • 按当前操作导入文件后,G Drive会将新创建文件的ID返回到
    $file
    中。使用此ID(
    $file->getId()
    )从步骤2调用端点

  • 我已经搜索了文档,但不幸的是,我没有找到任何相关示例。如中所述,它还不受支持。但是,您可以为此提交一个文件。