Issues with uploading file to google drive via google apps script

57 views
Skip to first unread message

Calvin Ehlert

unread,
Feb 1, 2024, 4:18:49 PM2/1/24
to Google Apps Script Community
I'm in the process of writing a program in google apps script that will allow users to upload files onto google drive. However, when I run the program, google.script.run does not run the function to upload a file (secFunc(formObject)). Because the program is run asynchronous, I'm having difficulties debugging or troubleshooting my issue. I believe the issue is a typo or bad method within secFunc, but given that I was able to run this function with no issue a week ago, I'm struggling to understand what the issue is. 

Schobi HH

unread,
Mar 2, 2024, 11:02:26 AM3/2/24
to Google Apps Script Community
myFile should be myFile[0]. I am not sure if myFile  as part of the form object is streamed properly to the server. Better to do this in the client and pass it as seperate parameter.

Brian Pugh

unread,
Mar 2, 2024, 1:02:34 PM3/2/24
to google-apps-sc...@googlegroups.com
I got the script/html to run, but I have to choose which file I want to upload, then upload it.

Is that what you want to happen? Here is the slightly changed versions:

function uploadFiles(){
  const htmlDlg = HtmlService.createHtmlOutputFromFile('Index').setWidth(350).setHeight(425);
  SpreadsheetApp.getUi().showModelessDialog(htmlDlg, 'Email Attachments');
}

function serverFunc(formObject){
  const blob = formObject.myFile;
  const file = DriveApp.createFile(blob);
  return 'File uploaded successfully: ' + file.getName();
}

and:
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>

    <form id="myForm" onsubmit="submitFunc(this)">
           <div class="form-group">
            <label for="myFile">Upload File</label>
            <input name="myFile" class="myFile" type="file" id="myFile" />
           </div>
          <br>
          <button type="submit">Submit</button>
        </form>
        <br>
        <div id="output"></div>
  </body>
  <script>
    function submitFunc(formObject){
      google.script.run.withSuccessHandler(onSuccess).serverFunc(formObject)
    }
    function onSuccess() {
      var Output = 'Input Succesfully Translated';
      alert(Output)
      google.script.host.close()
    }
  </script>
</html>






















Brian Pugh, IT/Educational Technologies



Associated Hebrew Schools | Danilack Middle School

p: 416.494.7666, | e: bp...@ahschools.com

w: www.associatedhebrewschools.com

252 Finch Ave W., Toronto, ON M2R 1M9


facebook.png twitter.png instagram.png 


This email is confidential and is intended for the above-named recipient(s) only. If you are not the intended recipient, please delete this email from your system. Any unauthorized use or disclosure of this email is prohibited.




--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/23dce389-fc86-4237-893f-72681b863128n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages