使用Google Apps脚本覆盖图像文件 [英] Overwrite an Image File with Google Apps Script

查看:110
本文介绍了使用Google Apps脚本覆盖图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用Google Apps Script覆盖图片文件吗?我试过了:

  file.setContent(newBlobImage); 
file.replace(newBlobImage);

这些都不起作用。 .setContent()会删除文件中的任何数据,看起来也许它只是将变量名写入文本或类似的东西。我假设 .setContent() .replace()用于文本文档,也许这就是为什么他们不工作。



如果是文本文件或电子表格,我可以清除它,然后添加新内容。



我可以垃圾文件,然后创建一个新文件,但如果还有其他方法,我宁愿不要。



如果我编写一个具有相同名称的文件,它不会覆盖现有文件,它会创建另一个具有相同名称的文件。



我已经能够垃圾文件是与 DocsList 和我创建一个图像文件的唯一成功与 DriveApp 。所以我必须用 DocsList 来清理文件,然后用 DriveApp 创建另一个文件。



好吧,我已经想出了如何删除文件而不将它发送到垃圾箱,所以我以后不需要清理垃圾箱。 Apps Script中的 Google Drive SDK 有一个 remove 方法,它不会将文件发送到垃圾箱,它只是

  var myFolder = DriveApp.getFolderById('3Bg2dKau456ySkhNBWB98W5sSTM'); 

thisFile = myFolder.getFilesByName(myFileName); $(b)b
while(thisFile.hasNext()){
var eachFile = thisFile.next();
var idToDLET = eachFile.getId();
Logger.log('idToDLET:'+ idToDLET);

var rtrnFromDLET = Drive.Files.remove(idToDLET);
};

所以,我将 DriveApp 服务和 DriveAPI 删除文件而不将它发送到垃圾箱。 DriveAPI .remove需要文件ID,但我没有文件ID,因此文件被按名称查找,然后检索文件ID,然后ID用于删除文件。所以,如果我找不到覆盖文件的方法,我至少可以删除旧文件而不会将它扔到垃圾箱中。



我刚刚注意到 DriveAPI 服务有一个修补程序和一个更新选项。

  .patch(resource,fileId,optionalArgs)

Google文档修补程序更新文件元数据。



资源 arg可能是元数据。 fileId 不言自明。我在猜测 optionalArgs 是遵循HTTP Request Patch语义的参数吗?我不知道。



看起来Patch和Update都会更新数据。更新是一个 PUT 请求,它将清除以前设置的数据,如果你不' t提供可选参数。

根据文档。所以使用补丁请求会更安全,因为任何缺少的参数都会被忽略。我还没有尝试过,但也许这是答案。



我遇到 Patch ,所以我会尝试 Update

  .update(resource ,fileId,mediaData)

以blob形式存在mediaData的arg。我认为这是我需要的。但我不确定资源参数需要什么。所以我被困在那里。

解决方案

我计算出来了。我可以用Google Apps Script和DriveAPI覆盖图片文件。我正在使用:

  .update(文件资源,String fileId,Blob mediaData)

其中文件资源为:

  var myFileName ='文件名'+'.jpg'; 

var file = {
title:myFileName,
mimeType:'image / jpeg'
};

我使用DriveApp服务获取文件ID,而Blob是由用户。

为了使用 DriveAPI ,您需要通过资源添加它高级Google服务菜单。将驱动器API 设置为

  var file = {
title:myFileName,
mimeType:'image / jpeg'
};

var myFolder = DriveApp.getFolderById('文件夹ID');

var allFilesByName = myFolder.getFilesByName(myFileName);

while(allFilesByName.hasNext()){
var thisFile = allFilesByName.next();
var theFileID = thisFile.getId();
Logger.log('theFileID:'+ theFileID);

var myVar = Drive.Files.update(file,theFileID,uploadedBlob);
};


Can I overwrite an image file with Google Apps Script? I've tried:

file.setContent(newBlobImage);
file.replace(newBlobImage);

Neither of those work. .setContent() will delete whatever data was in the file, and it looks like maybe it just writes the variable name as text, or something like that. I'm assuming that both .setContent() and .replace() are meant for text documents, and maybe that's why they don't work.

If it were a text file, or a spreadsheet, I might be able to clear it, then append new content.

I can trash the file, then create a new one, but I'd rather not if there is some other way.

If I write a file with the same name, it won't overwrite the existing file, it creates a another file with the same name.

The only way I've been able to trash the file is with DocsList and the only success I've had with creating an image file is with DriveApp. So I have to trash the file with DocsList, then create another file with DriveApp.

Well, I've figured out how to delete the file without sending it to the trash, so I won't need to clean out the trash later. The Google Drive SDK inside of Apps Script has a remove method that didn't send the file to trash, it's just gone.

var myFolder = DriveApp.getFolderById('3Bg2dKau456ySkhNBWB98W5sSTM');

thisFile = myFolder.getFilesByName(myFileName);

while (thisFile.hasNext()) {
  var eachFile = thisFile.next();
  var idToDLET = eachFile.getId();
  Logger.log('idToDLET: ' + idToDLET);

  var rtrnFromDLET = Drive.Files.remove(idToDLET);
};

So, I'm combining the DriveApp service and the DriveAPI to delete the file without sending it to the trash. The DriveAPI .remove needs the file ID, but I don't have the file ID, so the file gets looked up by name, then the file ID is retrieved, then the ID is used to delete the file. So, if I can't find a way to overwrite the file, I can at least delete the old file without it going to the trash.

I just noticed that the DriveAPI service has a Patch and an Update option.

.patch(resource, fileId, optionalArgs)

Google Documentation Patch Updates file metadata.

The resource arg is probably the metadata. The fileId is self explanatory. I'm guessing that the optionalArgs are parameters that follow the HTTP Request Patch semantics? I don't know.

It looks like both Patch and Update will update data. Update is a PUT request that will

clears previously set data if you don't supply optional parameters.

According to the documentation. So it's safer to use a Patch request because any parameters that are missing are simply ignored. I haven't tried it yet, but maybe this is the answer.

I'm getting an error with Patch, so I'll try Update:

.update(resource, fileId, mediaData)

That has a arg for mediaData in the form of a blob. And I think that is what I need. But I'm not sure what the resource parameter needs. So I'm stuck there.

解决方案

I figured it out.I can overwrite an image file with Google Apps Script and the DriveAPI. I'm using:

.update(File resource, String fileId, Blob mediaData)

Where file resource is:

var myFileName = 'fileName' + '.jpg';

var file = {
  title: myFileName,
  mimeType: 'image/jpeg'
};

I'm getting the file ID with the DriveApp service, and the Blob is what was uploaded by the user.

In order to use DriveAPI, you need to add it through the Resources, Advanced Google Services menu. Set the Drive API to ON.

var file = {
  title: myFileName,
  mimeType: 'image/jpeg'
};

var myFolder = DriveApp.getFolderById('Folder ID');

var allFilesByName = myFolder.getFilesByName(myFileName);

while (allFilesByName.hasNext()) {
  var thisFile = allFilesByName.next();
  var theFileID = thisFile.getId();
  Logger.log('theFileID: ' + theFileID);

  var myVar = Drive.Files.update(file, theFileID, uploadedBlob);
};

这篇关于使用Google Apps脚本覆盖图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆