指定文件夹以将文件保存到Google脚本中 [英] Specify folder to save files to in Google Script

查看:55
本文介绍了指定文件夹以将文件保存到Google脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过Google Apps脚本发布的简单表格,该表格允许某人匿名上传文件.该脚本运行良好,但是它将始终保存到我的Google云端硬盘的根目录中,而不是保存到特定文件夹中.我想用一个人的名字+姓氏(从表单中收集)在一个特定的文件夹中创建一个新文件夹.我知道我需要使用目标FolderID,只是不确定如何正确使用它.

I have a simple form published through Google Apps Scripts that lets someone upload a file anonymously. The script works well, but it will always save to the root of my Google Drive instead of to a specific folder. I'd like it to create a new folder with the person's first name + last name (collected from the form) within a specific folder. I know I need to use the destination FolderID, just not sure how to use it properly.

这是我现有的代码:

function uploadFiles(form) {

  try {

var dropbox = form.FirstName + form.LastName;
var folder, folders = DriveApp.getFoldersByName(dropbox);

if (folders.hasNext()) {
  folder = folders.next();
} else {
  folder = DriveApp.createFolder(dropbox);
}

var blob = form.myFile;    
var file = folder.createFile(blob);    
file.setDescription("Uploaded by " + form.Name);

return "Thank you, your file was uploaded successfully!";

推荐答案

我不确定我是否了解您的目标.但是,如果要在某个特定文件夹中创建文件夹"FirstnameLastname",则应首先选择此父"文件夹,然后将其用于创建新文件夹.

I'm not sure if I understood well what is your goal. But if you want to create folder 'FirstnameLastname' within some specific folder, you should first select this 'parent' folder and then use it for creating new one.

首先查看Google提供的参考资料.您使用类 DriveApp 和方法 .createFolder(). Google备忘单表示,DriveApp类中的此方法在用户云端硬盘的根目录中使用给定名称创建一个文件夹." .此方法返回 folder 作为返回类型(您的变量类型将是文件夹).

First look at the reference from Google. You use class DriveApp and methode .createFolder(). Google cheat sheet says, that this methode within the DriveApp class 'Creates a folder in the root of the user's Drive with the given name.'. This methode returns folder as return type (your variable type will be folder).

这就是为什么您的代码仅在根文件夹中创建文件夹的原因.

This is the reason why your code creates folders only in root folder.

再次查看备忘单,但现在来看一下文件夹类.您可以对此类使用相同的方法 .createFolder().结果如下:'在给定名称的当前文件夹中创建一个文件夹.'此方法再次返回文件夹.

Look in the cheatsheet again but now look at the folder class. You can use the same methode .createFolder() with this class. Result is following: 'Creates a folder in the current folder with the given name.' This methode returns again folder.

Google云端硬盘:

  1. 在Google云端硬盘存储库中手动创建(或打开)所需的文件夹.
  2. 查找文件夹的ID(打开文件夹时,您可以在URL的末尾找到该ID: drive.google.com/drive/folders/{这是您的ID} )

脚本:

  1. 获取所需的目标文件夹 var parentFolder = DriveApp.getFolderById("{在此处输入文件夹ID}");
  2. 添加您的代码
  3. 在父文件夹中创建新文件夹时,只需从之前创建的 parentFolder 中调用 .createFolder()方法即可: var folder =parentFolder.createFolder();
  1. Get your desired destination folder var parentFolder = DriveApp.getFolderById("{put your folder id here}");
  2. Add your code
  3. When you are creating new folder within parent folder, just call the .createFolder() methode from your parentFolder which you have created earlier: var folder = parentFolder.createFolder();

编辑的脚本:

    function uploadFiles(form) {
          try {
            var dropbox = form.FirstName + form.LastName;

            var parentFolder = DriveApp.getFolderById('parentfolderId'); //add this line... 

            var folder, folders = DriveApp.getFoldersByName(dropbox);

            if (folders.hasNext()) {
              folder = folders.next();
            } else {
              folder = parentFolder.createFolder(dropbox); //edit this line
            }

            var blob = form.myFile;
            var file = folder.createFile(blob); 

            file.setDescription("Uploaded by " + form.Name);

            return "Thank you, your file was uploaded successfully!";
          } 

这篇关于指定文件夹以将文件保存到Google脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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