html - Google Script - 在自定义表单提交时获取 Google Drive 链接到工作表(文件上传字段)
问题描述
我是谷歌脚本的新手。我正在尝试在自定义 Google 表单上在表单提交时检索上传到工作表的 Google Drive 文件链接。但是在表单提交时,只有文件名被添加到工作表中。有没有办法让文件的 Google Drive 链接到表单提交时的工作表?
这是我到目前为止所拥有的:
页面.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
</head>
<body>
<form id="form" action="https://docs.google.com/forms/u/0/d/e/####/formResponse" method="POST">
<div class="file-field input-field">
<input type="file" id="uploadfile" name="file">
<input id="upload_MC" name="entry.685812703" class="file-path validate" type="text" placeholder="Upload MC or other reference if applicable" onchange="uploadMC()">
</div>
<input id="submit" type="submit"/>
</form>
<script>
function uploadMC(){
const file = form.file.files[0];
const fr = new FileReader();
fr.readAsArrayBuffer(file);
fr.onload = (f) => {
const url = "https://script.google.com/macros/s/###/exec"; //the URL of Web Apps.
const qs = new URLSearchParams({
filename: file.name,
mimeType: file.type,
});
fetch(`${url}?${qs}`, {
method: "POST",
body: JSON.stringify([...new Int8Array(f.target.result)]),
})
.then((res) => res.json())
.then(console.log)
.catch(console.log);
};
}
</script>
<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
</body>
</html>
代码.gs
function doGet(e){
return HtmlService.createTemplateFromFile("page").evaluate().addMetaTag("viewport", "width=device-width, initial-scale=1.0");
}
function doPost(e) {
const folderId = "###"; // Folder ID which is used for putting the file.
const blob = Utilities.newBlob(
JSON.parse(e.postData.contents),
e.parameter.mimeType,
e.parameter.filename
);
const file = DriveApp.getFolderById(folderId || "root").createFile(blob);
const responseObj = file.getUrl();
return ContentService.createTextOutput(
JSON.stringify(responseObj)
).setMimeType(ContentService.MimeType.JSON);
}
page.html是我的自定义 Google 表单,可将数据发送到链接的工作表。现在它确实上传了文件并且文件名在工作表中提交,但是有没有办法让文件的查看链接插入工作表而不是文件名?
提前感谢您,祝您有美好的一天!
解决方案
文件的 url 由返回Code.gs
const responseObj = file.getUrl();
return ContentService.createTextOutput(
JSON.stringify(responseObj)
).setMimeType(ContentService.MimeType.JSON);
- 将 url 字段添加到您的自定义 Google 表单和原始 Google 表单中。
<input id="uploadUrl" type="hidden" name="entry.XXX">
- 修改函数以将url传递给输入字段
.then((res) => res.json())
.then(json => {
console.log(json);
uploadUrl.value = JSON.stringify(json);
})
.catch(console.log);
推荐阅读
- javascript - if 语句中的 Snackbar 错误消息
- api - Google SDK 中的项目错误
- mysql - 如何将 Spark SQL 与 My SQL 数据库 Scala 连接起来
- javascript - 在选项卡上更改更新本机反应中的状态
- python - 如何在具有键和键变量的python字典中创建一个列表?
- angular - 如何使用 Let's Encrypt 证书解决“错误:06065064:数字信封例程:EVP_DecryptFinal_ex:错误解密”ij Angular 7?
- c - 运算符“!”的未经允许的操作数 [MISRA 2012 规则 10.1,必填]
- python - tensorflow 重新加载模式从图形定义到会话
- vba - 如何添加具有默认值的 NUMBER 列?
- azure - Azure RM 模板 AutomationRunbookServiceUriIsNotValid