Google Apps脚本 - 如何替换文件?

时间:2013-11-12 10:25:30

标签: file pdf google-apps-script google-drive-api

我正在尝试使用脚本替换Google云端硬盘文件夹中的PDF文件。由于GAS没有提供添加修订版本(版本)的方法,我正在尝试替换文件的内容,但我得到的只是一个空白的PDF。

我无法使用DriveApp.File类,因为我们的管理员已禁用新API,因此我必须使用DocsList.File。

  • 输入:
    • OldFile.pdf(8页)
    • NewFile.pdf(20页)
  • 预期输出
    • OldFile.pdf,内容与NewFile.pdf相同
  • 实际输出:
    • OldFile.pdf,包含20个空页。

过程:

var old = DocsList.getFileById("####");
var new = DocsList.getFileById("####");
old.replace(new.getContentAsString());

请问任何想法? 非常感谢。

PS。:我也尝试先调用old.clear(),但我会说问题出在getContentAsString方法上。

2 个答案:

答案 0 :(得分:4)

Advanced Drive Service可用于替换Google云端硬盘中现有PDF文件的内容。这还包括如何使用团队云计算中的文件的示例。

function overwriteFile(blobOfNewContent,currentFileID) {
  var currentFile;

  currentFile = DriveApp.getFileById(currentFileID);    

  if (currentFile) {//If there is a truthy value for the current file
    Drive.Files.update({
      title: currentFile.getName(), mimeType: currentFile.getMimeType()
    }, currentFile.getId(), blobOfNewContent);
  }
}

参考

与团队驱动器一起使用的示例:

Drive.Files.update({ title: currentFile.getName(), mimeType: 
  currentFile.getMimeType() }, currentFile.getId(), blobOfNewContent, 
  {supportsTeamDrives: true});

答案 1 :(得分:1)

尝试将其作为 blob 数据类型。