使用 Google Apps 脚本覆盖图像文件 [英] Overwrite an Image File with Google Apps Script
问题描述
我可以使用 Google Apps 脚本覆盖图像文件吗?我试过了:
Can I overwrite an image file with Google Apps Script? I've tried:
file.setContent(newBlobImage);
file.replace(newBlobImage);
这些都不起作用..setContent()
将删除文件中的任何数据,看起来它可能只是将变量名称写为文本,或者类似的东西.我假设 .setContent()
和 .replace()
都用于文本文档,也许这就是它们不起作用的原因.
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.
我能够清除文件的唯一方法是使用 DocsList
,而我创建图像文件的唯一成功方法是使用 DriveApp
.所以我必须用 DocsList
删除文件,然后用 DriveApp
创建另一个文件.
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
.
嗯,我已经找到了如何删除文件而不将其发送到垃圾箱的方法,所以我以后不需要清理垃圾箱了.Apps Script 中的 Google Drive SDK
有一个 remove
方法,该方法没有将文件发送到垃圾箱,它只是消失了.
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);
};
因此,我结合了 DriveApp
服务和 DriveAPI
来删除文件而不将其发送到垃圾箱.DriveAPI
.remove 需要文件 ID,但我没有文件 ID,所以按名称查找文件,然后检索文件 ID,然后使用 ID 删除文件.所以,如果我找不到覆盖文件的方法,我至少可以删除旧文件而不会将其放入垃圾箱.
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.
我刚刚注意到 DriveAPI
服务有一个 Patch
和一个 Update
选项.
I just noticed that the DriveAPI
service has a Patch
and an Update
option.
.patch(resource, fileId, optionalArgs)
Google 文档补丁 更新文件元数据.
resource
参数可能是元数据.fileId
是不言自明的.我猜 optionalArgs
是遵循 HTTP 请求补丁语义的参数?我不知道.
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.
看起来补丁和更新都会更新数据.更新是一个 PUT
请求,它将
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.
根据文档.因此使用 Patch 请求更安全,因为任何缺少的参数都会被忽略.我还没有尝试过,但也许这就是答案.
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.
我在使用 Patch
时遇到错误,所以我会尝试 Update
:
I'm getting an error with Patch
, so I'll try Update
:
.update(resource, fileId, mediaData)
它有一个 blob 形式的 mediaData arg.我认为这就是我所需要的.但我不确定 resource
参数需要什么.所以我被困在那里.
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.
推荐答案
可以使用 Google Apps Script 和 DriveAPI 使用 update()
方法覆盖图像文件:
An image file can be overwritten with Google Apps Script and the DriveAPI using the update()
method:
.update(File resource, String fileId, Blob mediaData)
文件资源
在哪里:
var myFileName = 'fileName' + '.jpg';
var file = {
title: myFileName,
mimeType: 'image/jpeg'
};
我通过 DriveApp 服务获取文件 ID,而 Blob 是用户上传的内容.
I'm getting the file ID with the DriveApp service, and the Blob is what was uploaded by the user.
为了使用DriveAPI
,您需要通过Resources
、Advanced Google Services
菜单添加它.将 Drive API
设置为 ON.
In order to use DriveAPI
, you need to add it through the Resources
, Advanced Google Services
menu. Set the Drive API
to ON.
var allFilesByName,file,myFolder,myVar,theFileID,thisFile;
//Define var names without assigning a value
file = {
title: myFileName,
mimeType: 'image/jpeg'
};
myFolder = DriveApp.getFolderById('Folder ID');
allFilesByName = myFolder.getFilesByName(myFileName);
while (allFilesByName.hasNext()) {
thisFile = allFilesByName.next();
theFileID = thisFile.getId();
//Logger.log('theFileID: ' + theFileID);
myVar = Drive.Files.update(file, theFileID, uploadedBlob);
};
这篇关于使用 Google Apps 脚本覆盖图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!