Google 脚本 - 在自定义表单提交(文件上传字段)上获取 Google 驱动 Link 到 Sheet
Google Script - Get Google Drive Link to Sheet on custom form submit (file upload field)
我是 Google 脚本的新手。我正在尝试在自定义 Google 表单上,在提交表单后将上传的 Google 驱动器文件 link 检索到 Sheet。但是在表单提交时,只有文件名被添加到 sheet。有没有办法在表单提交时将文件的 Google 驱动器 link 传送到 sheet?
这是我目前的情况:
Page.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>
Code.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 表单,它将数据发送到 linked Sheet。现在它确实上传了文件并且文件名在 Sheet 中提交,但是有没有办法让文件的查看 link 插入到 Sheet 而不是文件的姓名?
在此先感谢您,祝您有愉快的一天!
文件的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);
我是 Google 脚本的新手。我正在尝试在自定义 Google 表单上,在提交表单后将上传的 Google 驱动器文件 link 检索到 Sheet。但是在表单提交时,只有文件名被添加到 sheet。有没有办法在表单提交时将文件的 Google 驱动器 link 传送到 sheet?
这是我目前的情况:
Page.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>
Code.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 表单,它将数据发送到 linked Sheet。现在它确实上传了文件并且文件名在 Sheet 中提交,但是有没有办法让文件的查看 link 插入到 Sheet 而不是文件的姓名?
在此先感谢您,祝您有愉快的一天!
文件的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);