使用Google Apps脚本将文件上传到Google云端硬盘

时间:2020-08-23 08:14:09

标签: google-apps-script

我找到了一段视频,该视频说明了如何将文件上传到Google云端硬盘,这是视频链接 https://www.youtube.com/watch?v=l9atSDs7-oI 这是使用的脚本

    val viewTreeObserver: ViewTreeObserver = container.viewTreeObserver
    if (viewTreeObserver.isAlive) {
        viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                container.viewTreeObserver.removeOnGlobalLayoutListener(this)
                val viewHeight: Int = container.height
                val viewHeight1: Int = container.measuredHeight
            }
        })
     }

我在代码中更改了文件夹ID。当我尝试使用它时,遇到错误function uploadFiles(url) { var response = UrlFetchApp.fetch(url) var fileName = getFilenameFromURL(url) var folder = DriveApp.getFolderById('1IxMiswEfi67ovoBf8ZH1RV7qVPx1Ks6l'); var blob = response.getBlob(); var file = folder.createFile(blob) file.setName(fileName) file.setDescription("Download from the " + url) return file.getUrl(); } function getFilenameFromURL(url) { //(host-ish)/(path-ish/)(filename) var re = /^https?:\/\/([^\/]+)\/([^?]*\/)?([^\/?]+)/; var match = re.exec(url); if (match) { return unescape(match[3]); } return null; } function doGet(e){ var html = HtmlService.createHtmlOutputFromFile('index.html') return html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL) } /*create index.html on the Google app project and put the below code and remove this comment*/ <!DOCTYPE html> <html> <head> <base target="_top"> <title>Upload Files</title> </head> <body> <h1>Upload files to Google drive from URL</h1> <form> <label>Enter the URL</label> <input type="text" name="myFile" id="url" style="height:5%; width:70%"> <br> <br> <input type="button" id="submitBtn" value="Upload Files"> <label id="resp"><label> </form> <script> document.getElementById('submitBtn').addEventListener('click', function(e){ var url= document.getElementById("url").value; google.script.run.withSuccessHandler(onSuccess).uploadFiles(url) }) function onSuccess(url){ document.getElementById('resp').innerHTML = "File uploaded to path" + url; } </script> </body> </html>

1 个答案:

答案 0 :(得分:3)

我认为,当脚本中输入的URL和文件夹ID均正确时,您的脚本才能正常工作。在这种情况下,请检查以下几点。

  • 在Google Apps脚本的Web Apps中,当您使用https://script.google.com/macros/s/###/exec的URL时,在修改Web Apps的脚本时,通过将Web Apps重新部署为新版本将最新的脚本反映到Web Apps中。 。

这是使用Google Apps脚本的Web Apps的重点。因此,我想提出以上确认。

参考文献: