将文件从<input>发送到Google Drive,但不是直接发送(我需要先使用GAS进行一些数据处理)

问题描述 投票:0回答:1

我有一个巨大的表格,其中包含我需要进行数据处理的各种输入,然后直接在多个电子表格上注册。 获取值并将其发送到电子表格的第一部分我可以完成,但两个输入是文件输入。

然后我发现了这个:

https://gist.github.com/tanaikech/280b782ee0518aa083a4fe0d71384823

如果我按原样使用,这个概念就可以正常工作,换句话说,如果我获取输入的值并调用 saveFile(e) 将文件保存在谷歌驱动器上,它就可以工作。

但是我不需要这个,我需要那个而不是:

google.script.run.withSuccessHandler(e => console.log(e)).saveFile(obj);

我有类似的东西:

return obj;

我正在尝试使用异步函数,但我在该领域的知识确实很少,而且我很难理解它是如何工作的。当我们将它与 fileReader 联系起来时,这就更难了。这是我的实际代码:

 async function saveFileJS(f) {

      console.log("arquivo: " + f);

      const file = f.files[0];
      const fr = new FileReader();
      var retorno = ""; //variavél para receber o retorno do obj

      fr.onload = await function(e) {
        const obj = {
          filename: file.name,
          mimeType: file.type,
          bytes: [...new Int8Array(e.target.result)]
        };
        console.log("file: " + obj);
        console.log("nome do arquivo: " + obj.filename);
        //google.script.run.withSuccessHandler(e => console.log(e)).saveFile(obj,destino);
        
        retorno = obj; // determina o retorno como o obj
        
      };

      fr.readAsArrayBuffer(file);

      console.log("objeto = " + retorno);

      return retorno; //retorna obj 


  }

但它并没有像我预期的那样工作。 当我在另一个js函数中调用这个函数时,即使我使调用者函数异步,即使我这样做

file = await saveFileJS(f);

查看日志,它在:

之前返回:
console.log("arquivo: " + f);
console.log("objeto = " + retorno);

        console.log("file: " + obj);
        console.log("nome do arquivo: " + obj.filename);

这意味着

fr.onload
之间的所有内容都是异步的,即使有等待,并且可以正常工作,因为
google.script.run.withSuccessHandler
会等待它之前的所有内容。

将 return 放入函数(e)中也不起作用,因为只会将 obj 值返回到 fr.onload

关于整个代码流程...

首先调用 preparaVetores()

https://pastebin.com/z4gPf76U

此代码从多个字段获取值并将其组织在数组中,然后使用 google.script.run.finalizarFormulario 将其发送到服务器端 但在此之前,它对从输入接收到的两个文件进行数据处理,以实现它使用以下代码(我在函数调用之前使用await):

https://pastebin.com/mxJiJJ3e

说实话,我没有创建这段代码,所以我无法真正解释它是如何工作的,但我可以解释我试图更改的内容。

首先。我评论了

/google.script.run.withSuccessHandler(e => console.log(e)).saveFile(obj,destino);
因为我还不希望它发送到服务器端,而是添加了以下行:
retorno = obj; // determina o retorno como o obj
并在函数的末尾:
return retorno; //retorna obj
I did that on the hope that it would return the object in the previous function (
foto = 等待 saveFileJS(foto); )但是没有用(稍后我会解释什么是返回)

完成此操作后,应使用 script.run 并在将其发送到电子表格之前进行所有数据处理:

https://pastebin.com/gi6qjGeQ

大部分代码只是进行数据处理,似乎已经完成了它的工作,问题就在这里:

  Logger.log("gravando a foto " + foto);
  foto = saveFile(foto, pastaFotos);
  Logger.log(foto);
  Logger.log("gravando o currículo " + curriculo);
  curriculo = saveFile(curriculo, pastaCurriculos);
  Logger.log(curriculo);

saveFile 返回此错误:

TypeError: Cannot read properties of undefined (reading 'bytes')
    at saveFile(MAIN/main:1004:34)
    at finalizarFormulario(MAIN/main:790:10)

保存文件是:

https://pastebin.com/jrmFmTgU

同样,我没有创建此代码,但我可以解释我在这里更改的内容。

  file.moveTo(DriveApp.getFolderById(destino));
 
  let fileURL = file.getUrl();
 
  Logger.log(fileURL);
 
  return fileURL;

添加的代码将文件从根移动到特定代码(destino),移动文件夹后获取文件的url并返回url。因为对我来说重要的是文件 url,而不是 blob 本身。

在客户端,saveFileJS 中的 console.log() 按以下顺序运行:

第3行、第25行(...)第15行、第16行 (...) 代表 console.log() 在调用函数 preparaVetores() 中生成。

据我所知,这意味着 saveFileJS 是异步的。我只是不知道必须在 saveFileJS() 中更改什么才能使其正常工作。我尝试将

fr.onload = function(e) { 
更改为
fr.onload = await function(e) {
但没有任何效果。

谢谢你。

javascript google-apps-script google-drive-api google-sheets-api async.js
1个回答
0
投票

也许有一种方法可以使用 async/await 来完成此操作,但是使用 Promises 可以更轻松地完成此类操作。

function saveFileJS(f) {
   return new Promise((resolve, reject) => {
      console.log("arquivo: " + f);
      const file = f.files[0];
      const fr = new FileReader();

      fr.onload = function(e) {
        const obj = {
          filename: file.name,
          mimeType: file.type,
          bytes: [...new Int8Array(e.target.result)]
        };
        console.log("file: " + obj);
        console.log("nome do arquivo: " + obj.filename);
        //google.script.run.withSuccessHandler(e => console.log(e)).saveFile(obj,destino);
        resolve(obj); // 
      };

      fr.readAsArrayBuffer(file);
   });
  }
  
  async function testIt() {
    const file = await saveFileJS(someFile);
    console.log(file.mimeType);
  }

© www.soinside.com 2019 - 2024. All rights reserved.