如何从HTML文件输入中获取文件并将其作为附件发送到Google Apps脚本中的电子邮件中? [英] How to get file(s) from an HTML file input and send it as an attachment to an email in Google Apps Script?

查看:142
本文介绍了如何从HTML文件输入中获取文件并将其作为附件发送到Google Apps脚本中的电子邮件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此HTML代码使用Google Script的HTML类服务接受文件输入:

This HTML code takes file inputs using Google Script's class HTML service :

(在下面看到)

我想获取输入字段的值(文件),发送到我的.gs文件,并将其作为电子邮件的附件发送.

I would like to get the value(s)(the files) of the input field, send to the my .gs file and send that as an attachment to the email.

从输入字段获取值只会返回文件目录,这无济于事,因为Google Apps脚本无法从本地驱动器获取文件.

Getting the value from the input field simply returns the directory of the file, which is no help because Google Apps Script can't obtain files from local drive.

我已经对这个问题进行了长时间的研究,但找不到类似问题的人.

I have done a lengthy research with this problem and I can't find anyone with a similar issue.

Stack Code.gs

Stack Code.gs

function myFunction() {
    var html = HtmlService.createHtmlOutputFromFile('Stack HTML').setWidth(250).setHeight(250);
    SpreadsheetApp.getUi().showModalDialog(html,'Get File');
}

function processEmail(files){
  
  var subject = 'Subject';
  var message = 'Test';
  var recipient = 'test@gmail.com';
  GmailApp.sendEmail(recipient, subject, message, {attachments: files, htmlBody: message, name:'Stack Overflow Test'});  // Doesn't work

  
}

堆栈HTML.html

Stack HTML.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
  
     <form method="post" enctype="multipart/form-data">
     <div>
     <input type="file" id= "file" multiple = "true">
     </div>
     </form>
     <br><div style="text-align:center"> <input type="submit" name = "submitButton" onclick = "send()" id = "sendButton" value="Send"></div>
     
   <script>
   
   function done(e){
       google.script.host.close();
   }
   
   
   function send(){
      var files = document.getElementById("file").value;
      console.log(files);
      google.script.run.withSuccessHandler(done).processEmail(files);
   }
   
   </script>
  </body>
</html>



推荐答案

  • 您要使用HTML和Javascript将多个文件从本地PC上传到Google云端硬盘.
  • 您要发送上载的文件作为电子邮件的附件.
  • 您想使用Google Apps脚本实现这一目标.
  • 如果我的理解是正确的,那么该修改如何?请认为这只是几个答案之一.

    If my understanding is correct, how about this modification? Please think of this as just one of several answers.

    根据您的问题,这些文件将用作电子邮件的附件文件.因此,我认为以下流程可用于您的情况.

    From your question, the files are used as the attachment files of an email. So I thought that the following flow can be used for your situation.

    1. 从本地PC检索所有文件.
    2. 所有文件都汇总为一个对象.
      • 这时,文件将转换为base64字符串值.
    1. Retrieve all files from the local PC.
    2. All files are summarized as an object.
      • At this time, the files are converted to the base64 string value.

    修改后的脚本:

    请按如下所示在HTML端中修改send().

    function send() {
      const f = document.getElementById('file');
      Promise.all([...f.files].map((file, i) => {
        const fr = new FileReader();
        return new Promise(r => {
          fr.onload = e => {
            const data = e.target.result.split(",");
            r({fileName: file.name, mimeType: data[0].match(/:(\w.+);/)[1], data: data[1]});
          }
          fr.readAsDataURL(file);
        });
      }))
      .then(obj => {
        google.script.run.withSuccessHandler(done).processEmail(obj);
      });
    }
    

    请在GAS端进行如下修改processEmail().

    And please modify processEmail() in GAS side as follows.

    function processEmail(obj){
      var files = obj.map(function(e) {return Utilities.newBlob(Utilities.base64Decode(e.data), e.mimeType, e.fileName)});
      var subject = 'Subject';
      var message = 'Test';
      var recipient = 'test@gmail.com';
      GmailApp.sendEmail(recipient, subject, message, {attachments: files, htmlBody: message, name:'Stack Overflow Test'});
    }
    

    参考文献:

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