Google Sheet无法更新:Drive大文件上传器同步数据失败求助
问题分析与修复方案
核心错误点
- 未定义变量导致写入失败:
updateform函数中使用的myFile变量从未定义,且表单提交的formObject无法直接传递文件对象(文件是通过断点续传脚本单独上传的),这会导致函数静默失败(try-catch捕获错误但前端未正确接收反馈)。 - 表单事件未触发:表单
onsubmit里的This拼写错误(应为小写this),且submit按钮的onclick="run(); return false;"会阻止表单提交,导致updatesheet函数根本没被调用。 - 逻辑断层:文件上传完成后,没有将文件信息(名称、URL)和用户名关联起来写入表格的流程。
修正后的代码
Google Scripts 部分
function doGet(e) { return HtmlService.createHtmlOutputFromFile('Form.html'); } function getAuth() { return { accessToken: ScriptApp.getOAuthToken(), folderId: "1sFxs3Ga4xWFCgIXRUnQzCAAp_iRX-wdj" }; } function setDescription({fileId, description}) { DriveApp.getFileById(fileId).setDescription(description); } // 修改为接收明确参数,避免依赖未定义变量 function updateform(fileName, fileUrl, userName) { try { var ss = SpreadsheetApp.openById('1iCTNZ6RERnes1Y-ocfXzPN3jviwdIEK_dBKQ4LIu5KI'); var sheet = ss.getSheets()[0]; // 修正appendRow参数格式:仅需一个数组参数 sheet.appendRow([fileName, fileUrl, userName]); return "表格写入成功"; } catch (error) { return error.toString(); } }
HTML 部分
<form id="myForm" align="center"> <input type="text" name="myName" placeholder="Your name.." required> <input type="file" name="myFile" required> <input type="submit" value="Submit Form" onclick="run(); return false;"> </form> <div id="progress"></div> <div id="output"></div> <script src="https://cdn.jsdelivr.net/gh/tanaikech/ResumableUploadForGoogleDrive_js@master/resumableupload_js.min.js"></script> <script> function onSheetUpdateSuccess(msg) { document.getElementById('output').innerHTML = msg; } function onSheetUpdateFailure(error) { alert('表格写入失败:' + error); } function run() { google.script.run.withSuccessHandler(ResumableUploadForGoogleDrive).getAuth(); } function ResumableUploadForGoogleDrive({accessToken, folderId}) { const myName = document.getElementsByName("myName")[0].value; const file = document.getElementsByName("myFile")[0].files[0]; if (!file || !myName) { alert('请填写姓名并选择文件'); return; } let fr = new FileReader(); fr.fileName = file.name; fr.fileSize = file.size; fr.fileType = file.type; fr.readAsArrayBuffer(file); fr.onload = e => { var id = "p"; var div = document.createElement("div"); div.id = id; document.getElementById("progress").appendChild(div); document.getElementById(id).innerHTML = "Initializing."; const f = e.target; const resource = { fileName: f.fileName, fileSize: f.fileSize, fileType: f.fileType, fileBuffer: f.result, accessToken, folderId }; const ru = new ResumableUploadToGoogleDrive(); ru.Do(resource, function (res, err) { if (err) { console.log(err); alert('上传失败:' + err.message); return; } console.log(res); let msg = ""; if (res.status == "Uploading") { msg = Math.round((res.progressNumber.current / res.progressNumber.end) * 100) + "% (" + f.fileName + ")"; } else { msg = res.status + " (" + f.fileName + ")"; } if (res.status == "Done") { const fileUrl = `https://drive.google.com/file/d/${res.result.id}/view`; // 先设置文件描述,再触发表格写入 google.script.run.withSuccessHandler(() => { google.script.run .withSuccessHandler(onSheetUpdateSuccess) .withFailureHandler(onSheetUpdateFailure) .updateform(f.fileName, fileUrl, myName); document.getElementById('myForm').style.display = 'none'; document.getElementById('p').style.display = 'none'; }).setDescription({fileId: res.result.id, description: "Uploaded by " + myName}); } document.getElementById(id).innerText = msg; }); } } </script>
关键修正说明
updateform函数重构:改为接收明确的fileName、fileUrl、userName参数,避免依赖未定义的myFile,同时修正appendRow的参数格式(该方法仅接受一个数组参数)。- 表单事件调整:移除无效的
onsubmit事件,改为在文件上传完成后,通过成功回调调用updateform,确保文件信息和用户名能正确传递到表格写入函数。 - 错误处理强化:添加前端输入校验(姓名和文件必填),并为表格写入操作添加独立的成功/失败回调,便于排查问题。
内容的提问来源于stack exchange,提问作者Tristan.Beer
相关产品推荐
相关产品推荐

