且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Google Apps脚本将文件夹上载/命名到Google云端硬盘上的目标文件夹

更新时间:2023-02-11 09:33:25

我对您的代码做了一些小的更改:

I made some little changes to your code:

在您的"sendFileToDrive"中我将文件夹名称(在本例中为"Uploads")传递给了您的"uploadFileToDrive".在您的Apps脚本中.

in your "sendFileToDrive" I passed the folder name (in this case "Uploads") to your "uploadFileToDrive" in your Apps Script.

function sendFileToDrive(file) {
    var reader = new FileReader();
    reader.onload = function (e) {
        var content = reader.result;
        console.log('Sending ' + file.name);
        // set the file's name where you want to set your files 
        var folderName = "UPLOADS"; // my desired destination folder
        google.script.run.withSuccessHandler(updateProgressbar).uploadFileToDrive(content, file.name, folderName);
    }
    reader.readAsDataURL(file);
}

要在提交表单后重置表单中的值,可以通过以下方式进行操作(请记住,这是Vanilla JS,因此Internet上有多种方法可以实现此目的.)

To reset the values in your form after submitting it you can do it in the following way (Keep in mind this is vanilla JS, so there are several ways on the Internet to achieve this).

在您的"updateProgressbar"中写出这行代码document.getElementById("myForm").reset();

Write this line of code document.getElementById("myForm").reset(); in your "updateProgressbar"

function updateProgressbar( idUpdate ){
   console.log('Received: ' + idUpdate);
   numUploads.done++;
   var porc = Math.ceil((numUploads.done / numUploads.total)*100);
   $("#progressbar").progressbar({value: porc });
   $(".progress-label").text(numUploads.done +'/'+ numUploads.total);
   if( numUploads.done == numUploads.total ){
      //uploadsFinished();
      numUploads.done = 0;
      // Reset the form's fields values
      document.getElementById("myForm").reset();

   };
}

应用脚本文件

在您的Apps脚本代码中,我进行了这些更改,从刚刚上传的文件创建文件夹,然后将其放入上传"文件夹中.文件夹.您遇到的问题是,您试图在if-else中创建一个新文件夹,但是在您遇到的情况下,您将不会进入代码部分来创建文件夹.

Apps Script file

In your Apps Script code, I made these changes to create the folder from the file you just uploaded and then putting it in your "Uploads" folder. The problem you were having is you were trying to create a new folder in your if-else, but with the condition you had, you were not going to enter to the part of the code to create the folder.

function doGet() {
  return HtmlService.createHtmlOutputFromFile('form')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function uploadFileToDrive(base64Data, fileName, folderName) {
  try{
    var splitBase = base64Data.split(','),
        type = splitBase[0].split(';')[0].replace('data:','');

    var byteCharacters = Utilities.base64Decode(splitBase[1]);
    var ss = Utilities.newBlob(byteCharacters, type);
    ss.setName(fileName);
    
    // Get your Uploads folder
    var folders = DriveApp.getFoldersByName(folderName);
    
    // Regex for splitting the file's extention like .pdf
    var patt1 = /\.[0-9a-z]+$/i;
    
    if (folders.hasNext()) {
      var folder = folders.next();
      // Create a folder with the name of the file you just uploaded in your root path
      // DriveApp.createFolder(fileName.split(patt1)[0]);
      // Create the file in your Uploads folder and a folder
      var file = folder.createFile(ss);
      folder.createFolder(fileName.split(patt1)[0]);
      return file.getName(); 
    } else return null;
    
  } catch(e){
    return 'Error: ' + e.toString();
  }
}

编辑

请注意,类文件夹有一个名为createFolder(name)的特殊方法,该方法将允许您在要处理的当前文件夹中创建一个文件夹,就像我在更新的代码"中所述的那样.查看更新的文档"链接以获取更多信息.

EDIT

Notice the class folder has a special method called createFolder(name), which will allow you to create a folder in the currently folder you are handling as I put it in my Updated code. Check the Updated Docs links for more info.

我使用这些文档来帮助您找到问题的解决方案:

I used these docs to help you find the solution to your problem:

网络应用

类文件夹-createFolder(name)