Corrupted or Blank file is getting by email through Web app - Google script

前端 未结 1 554
隐瞒了意图╮
隐瞒了意图╮ 2021-01-27 07:26

Question

Below google script running fine but the file uploaded send via email is corrupted or blank while getting through email.. Attached filename, c

相关标签:
1条回答
  • 2021-01-27 08:06

    To solve your issue, in the handleFormSubmit function, I took an array buffer and transformed it into a string containing the file data and pass it to your processForm function, in order for that logic to be handled in the frontend instead of the backend, google.script.run is a little picky with the values you can pass as arguments. Therefore, your handleFormSubmit function will look like this now:

     const handleFormSubmit = async (formObject) => {
          // Get all the file data
          let file = formObject.myFile.files[0];
          // Get binary content, we have to wait because it returns a promise 
          let fileBuffer = await file.arrayBuffer();
          // Get the file content as binary and then pass it to string 
          const data = (new Uint8Array(fileBuffer)).toString();
          // Pass the file meta data and the content 
          google.script.run.withSuccessHandler(updateUrl).processForm(file.name, file.type, data);
    }
    

    As for the backend function processForm , you will need to transform the data string into a binary data array again, that's why I use JSON.parse("[" + data + "]"). Now, your processForm will look like this:

    function processForm(name, type, data) {
      var fileToSend = {
        fileName: name,
        // Convert the string to a Binary data array
        content: JSON.parse("[" + data + "]"), 
        mimeType: type
      };
      GmailApp.sendEmail('email@domain', '6 Attachment example', '6 Please see the attached file.', {
        attachments: [fileToSend],
        name: '6 Automatic Emailer Script'
      });
      return "this file " + name + " has just been sent to your email";
    }
    
    0 讨论(0)
提交回复
热议问题