使用 Apps 脚本在 Google Drive 中创建一个 zip 文件 [英] Creating a zip file inside Google Drive with Apps Script

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

问题描述

我在 Google 云端硬盘文件夹中有一个文件夹,其中包含一些文件.我想制作一个 Google Apps 脚本,它将压缩该文件夹中的所有文件并在同一文件夹中创建 zip 文件.

I have a folder in Google Drive folder containing few files. I want to make a Google Apps Script that will zip all files in that folder and create the zip file inside same folder.

我找到了一个具有 Utilities.zip() 功能的视频,但没有 API 参考.我该如何使用它?提前致谢.

I found a video that has Utilities.zip() function, but there is no API reference for that. How do I use it? Thanks in advance.

推荐答案

其实比这更简单.文件已经是 Blob(任何具有 getBlob() 的东西都可以传递给任何需要 Blob 的函数).所以代码看起来像这样:

Actually it's even easier than that. Files are already Blobs (anything that has getBlob() can be passed in to any function that expects Blobs). So the code looks like this:

var folder = DocsList.getFolder('path/to/folder');
folder.createFile(Utilities.zip(folder.getFiles(), 'newFiles.zip'));

此外,如果您在文件夹中有多个同名文件,它将不起作用...Google 云端硬盘文件夹支持,但 Zip 文件不支持.

Additionally, it won't work if you have multiple files with the same name in the Folder... Google Drive folders support that, but Zip files do not.

要使用多个具有相同名称的文件:

To make this work with multiple files that have the same name:

var folder = DocsList.getFolder('path/to/folder');
var names = {};
folder.createFile(Utilities.zip(folder.getFiles().map(function(f){
  var n = f.getName();
  while (names[n]) { n = '_' + n }
  names[n] = true;
  return f.getBlob().setName(n);
}), 'newFiles.zip'));

这篇关于使用 Apps 脚本在 Google Drive 中创建一个 zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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