首页 > 解决方案 > 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 表单,可将​​数据发送到链接的工作表。现在它确实上传了文件并且文件名在工作表中提交,但是有没有办法让文件的查看链接插入工作表而不是文件名?

提前感谢您,祝您有美好的一天!

标签: htmlgoogle-apps-scriptgoogle-sheetsgoogle-drive-apigoogle-forms

解决方案


文件的 url 由返回Code.gs

  const responseObj = file.getUrl();
  return ContentService.createTextOutput(
    JSON.stringify(responseObj)
  ).setMimeType(ContentService.MimeType.JSON);
  1. 将 url 字段添加到您的自定义 Google 表单和原始 Google 表单中。
<input id="uploadUrl" type="hidden" name="entry.XXX">
  1. 修改函数以将url传递给输入字段
        .then((res) => res.json())
        .then(json => {
          console.log(json);
          uploadUrl.value = JSON.stringify(json);
        })
        .catch(console.log);

推荐阅读